Jump to content

Hook to show recent imported ticket failures on support tickets page


websavers

Recommended Posts

There's been a few occasions where we've had spam controls work a bit more aggressively than we'd like and ticket messages were blocked as false positives because of them. This isn't a problem in an email client because you can simply peruse your Spam folder, and while WHMCS does allow you to view the ticket mail import log, it's kind of hidden away and easily forgotten (unlike a Spam folder which is always showing in your email folder listing). 

If you've ever been in a similar situation, this hook will help! It shows the 10 most recent failures to import because of spam controls at the bottom of the support ticket listing page.  It also excludes mailer-daemon bounces in the event you've got those blocked to cut down on useless tickets / email loops.

Enjoy 🙂

add_hook('AdminSupportTicketPagePreTickets', 1, function($vars) {
	
	$output = "<div id='recently-blocked'><h2>Recently Blocked Messages   <small><i class='far fa-eye'></i> <a href='systemmailimportlog.php' target='_blank'>SEE ALL</a></small></h2>
		<table id='sortabletbl1' class='datatable' style='width:100%'>
		<tr><th>Date</th><th>Name/Email</th><th>Subject</th><th>Status</th><th></th></tr>";
	
	foreach (Capsule::table('tblticketmaillog')->where('status', 'like', 'Blocked%')->where('email', 'not like', 'mailer-daemon%')->orderBy('id', 'desc')->limit(10)->get() as $msg){

		/* Name */
		if ($msg->name === $msg->email) $name = $msg->name;
		else $name = "{$msg->name} <{$msg->email}>";
		
		/* Date */
		$today = new DateTime();
		$msg_date = new DateTime($msg->date);
		$interval = $today->diff($msg_date);
		$date_interval = abs($interval->format('%R%a'));
		
		if ($date_interval == 0) $date_interval = 'Today';
		else if ($date_interval == 1) $date_interval .= 'Yesterday';
		else $date_interval .= ' days ago';

		$output .= "
		<tr>
			<td>$date_interval</td><td>$name</td><td>{$msg->subject}</td><td>{$msg->status}</td>
			<td><button class='btn' style='padding:1px 5px;' onclick=\"window.open('/whmcs/admin/systemmailimportlog.php?display=true&id={$msg->id}','','width=650,height=400,scrollbars=yes');return false\">VIEW</button></td>
		</tr>";
		
	}
	
	return  "$output</table><br /></div><script>
jQuery(document).ready(function($){ /* Move to bottom of page */
$('#recently-blocked').appendTo('#contentarea > div:first-child');
});
</script>";
	
});

 

Link to comment
Share on other sites

  • 1 year later...

This is great...despite users having to sort out WHMCS bugs.  Thanks.  But there is some code missing, at least in 8.2.1

<?php 

use Illuminate\Database\Capsule\Manager as Capsule;

Insert it right at the top

Edited by 123host
Link to comment
Share on other sites

There were a couple of things in the original code that need fixing. 

The missing object (above) and the hard coded path to  systemmailimportlog.php

Here is an updated version.

<?php

use Illuminate\Database\Capsule\Manager as Capsule; 

add_hook('AdminSupportTicketPagePreTickets', 1, function($vars) {
	
	$output = "<div id='recently-blocked'><h2>Recently Blocked Messages   <small><i class='far fa-eye'></i> <a href='systemmailimportlog.php' target='_blank'>SEE ALL</a></small></h2>
		<table id='sortabletbl1' class='datatable' style='width:100%'>
		<tr><th>Date</th><th>Name/Email</th><th>Subject</th><th>Status</th><th></th></tr>";
	
	foreach (Capsule::table('tblticketmaillog')->where('status', 'like', 'Blocked%')->where('email', 'not like', 'mailer-daemon%')->orderBy('id', 'desc')->limit(10)->get() as $msg){

		/* Name */
		if ($msg->name === $msg->email) $name = $msg->name;
		else $name = "{$msg->name} <{$msg->email}>";
		
		/* Date */
		$today = new DateTime();
		$msg_date = new DateTime($msg->date);
		$interval = $today->diff($msg_date);
		$date_interval = abs($interval->format('%R%a'));
		
		if ($date_interval == 0) $date_interval = 'Today';
		else if ($date_interval == 1) $date_interval .= 'Yesterday';
		else $date_interval .= ' days ago';

		$output .= "
		<tr>
			<td>$date_interval</td><td>$name - $customadminpath</td><td>{$msg->subject}</td><td>{$msg->status}</td>
			<td><button class='btn' style='padding:1px 5px;' onclick=\"window.open('systemmailimportlog.php?display=true&id={$msg->id}','','width=650,height=400,scrollbars=yes');return false\">VIEW</button></td>
		</tr>";
		
	}
	
	return  "$output</table><br /></div>
	<script>
        jQuery(document).ready(function($){ /* Move to bottom of page */
        $('#recently-blocked').appendTo('#contentarea > div:first-child');
        });
    </script>";
	
});

 

Edited by 123host
Link to comment
Share on other sites

In typical WHMCS style, it seems you can't delete a post or edit it after a certain period - WTF???

[UPDATED] @WHMCS John the code tags don't work properly.  Everything wrapped in those tags should be ignored as real code, yet a post is blocked if it includes a global variable such as $ GLOBALS ['customadminpath'] (note added spaces to make it work) .  How are we supposed to share code?

Anyway, for bonus points: the cool original hook from @websavers had hard coded the path of  the  systemmailimportlog file. If WHMCS displays an error when you try to view the failed email's contents when you click on the "view" icon, it may be because it isn't finding your custom admin directory - you did change it from the default, right ;o)

so change

window.open('systemmailimportlog.php

to

window.open('" . $*GLOBALS*['customadminpath'] . "/systemmailimportlog.php - remove the two * characters - it was the only way to get it to post.

and it will include your custom admin path

Edited by 123host
Link to comment
Share on other sites

Another minor bug

The date of the email is shown as "Today", "1Yesterday", "2 Days ago" etc.    That "1" shouldn't precede "Yesterday"

so change

else if ($date_interval == 1) $date_interval .= 'Yesterday';	

to

else if ($date_interval == 1) $date_interval = 'Yesterday';	
Link to comment
Share on other sites

Here is the latest version - with a version number to help keep track

<?php
// version 1.2 July 30 2021

use Illuminate\Database\Capsule\Manager as Capsule; 

add_hook('AdminSupportTicketPagePreTickets', 1, function($vars) {
	
	$output = "<div id='recently-blocked'><h2>Recently Blocked Messages   <small><i class='far fa-eye'></i> <a href='systemmailimportlog.php' target='_blank'>SEE ALL</a></small></h2>
		<table id='sortabletbl1' class='datatable' style='width:100%'>
		<tr><th>Date</th><th>Name/Email</th><th>Subject</th><th>Status</th><th></th></tr>";
	
	foreach (Capsule::table('tblticketmaillog')->where('status', 'like', 'Blocked%')->where('email', 'not like', 'mailer-daemon%')->orderBy('id', 'desc')->limit(10)->get() as $msg){

		/* Name */
		if ($msg->name === $msg->email) $name = $msg->name;
		else $name = "{$msg->name} <{$msg->email}>";
		
		/* Date */
		$today = new DateTime();
		$msg_date = new DateTime($msg->date);
		$interval = $today->diff($msg_date);
		$date_interval = abs($interval->format('%R%a'));
		
		if ($date_interval == 0) $date_interval = 'Today';
		else if ($date_interval == 1) $date_interval = 'Yesterday';
		else $date_interval .= ' days ago';

		$output .= "
		<tr>
			<td>$date_interval</td><td>$name</td><td>{$msg->subject}</td><td>{$msg->status}</td>
			<td><button class='btn' style='padding:1px 5px;' onclick=\"window.open('/" . $GLOBALS['customadminpath'] . "/systemmailimportlog.php?display=true&id={$msg->id}','','width=650,height=400,scrollbars=yes');return false\">VIEW</button></td>
		</tr>";
		
	}
	
	return  "$output</table><br /></div>
	<script>
        jQuery(document).ready(function($){ /* Move to bottom of page */
        $('#recently-blocked').appendTo('#contentarea > div:first-child');
        });
    </script>";
	
});

 

Edited by 123host
I wish I could edit the source code but this is so buggy I can't
Link to comment
Share on other sites

9 hours ago, 123host said:

In typical WHMCS style, it seems you can't delete a post or edit it after a certain period - WTF???

put the code on GitHub (or similar) and post the link to it in this thread - then you can edit and update the code as often as you want, when you want and how you want to. 💡

for some bizarre reason in the last year or two here, you often can't include PHP predefined variables in your posted code - I don't think it's intentional (if it is, then it's a bloody silly one), I suspect it's more a configuration issue that nobody at WHMCS noticed or really cares about (as they don't post code, they never come across it themselves or understand the inconvenience)... the point being that you end up wasting time rewriting your own code, just so you can post it here without getting a 403 Forbidden error message.

while i'm here looking at this, you should be able to use Admin Language Overrides to have most of the text shown in the admin's own language - granted the docs don't make any mention that you can use them for the admin area, but you can... I think there should be language string equivalents (or similar) for everything (other than the "Recently Blocked Messages" heading) in the $output array....

if ($date_interval == 0) $date_interval = AdminLang::trans('calendar.today');

of course, if you used Carbon for the dates, then you shouldn't need such strings on the dates.... but I probably wouldn't suggest spending a lot of time on doing that... this is just one of those many things that WHMCS might fix in a hotfix tomorrow, or you could still be waiting when v10 gets released in 2025 - the thread's already 2 years old. 🙄

Link to comment
Share on other sites

  • WHMCS Support Manager

Cool contribution @123host!

Sorry about the trouble posting code on the community, I've reported this with our community manager to check into.

EDIT: What's the specific  error message you encounter please?

Post editing is time-limited, to prevent users from changing their posts after other users have responded. This is pretty common for messages boards in my experience.

 

@sol2010 we can certainly consider adding this to the core in future. I've opened a feature request for you to track demand: https://requests.whmcs.com/idea/show-recently-blocked-tickets-in-support-ticket-list

Link to comment
Share on other sites

  • 2 months later...

This 

On 7/30/2021 at 2:14 PM, 123host said:

Here is the latest version - with a version number to help keep track


<?php
// version 1.2 July 30 2021

use Illuminate\Database\Capsule\Manager as Capsule; 

add_hook('AdminSupportTicketPagePreTickets', 1, function($vars) {
	
	$output = "<div id='recently-blocked'><h2>Recently Blocked Messages   <small><i class='far fa-eye'></i> <a href='systemmailimportlog.php' target='_blank'>SEE ALL</a></small></h2>
		<table id='sortabletbl1' class='datatable' style='width:100%'>
		<tr><th>Date</th><th>Name/Email</th><th>Subject</th><th>Status</th><th></th></tr>";
	
	foreach (Capsule::table('tblticketmaillog')->where('status', 'like', 'Blocked%')->where('email', 'not like', 'mailer-daemon%')->orderBy('id', 'desc')->limit(10)->get() as $msg){

		/* Name */
		if ($msg->name === $msg->email) $name = $msg->name;
		else $name = "{$msg->name} <{$msg->email}>";
		
		/* Date */
		$today = new DateTime();
		$msg_date = new DateTime($msg->date);
		$interval = $today->diff($msg_date);
		$date_interval = abs($interval->format('%R%a'));
		
		if ($date_interval == 0) $date_interval = 'Today';
		else if ($date_interval == 1) $date_interval = 'Yesterday';
		else $date_interval .= ' days ago';

		$output .= "
		<tr>
			<td>$date_interval</td><td>$name</td><td>{$msg->subject}</td><td>{$msg->status}</td>
			<td><button class='btn' style='padding:1px 5px;' onclick=\"window.open('/" . $GLOBALS['customadminpath'] . "/systemmailimportlog.php?display=true&id={$msg->id}','','width=650,height=400,scrollbars=yes');return false\">VIEW</button></td>
		</tr>";
		
	}
	
	return  "$output</table><br /></div>
	<script>
        jQuery(document).ready(function($){ /* Move to bottom of page */
        $('#recently-blocked').appendTo('#contentarea > div:first-child');
        });
    </script>";
	
});

 

 

 

After updating to 8.3.0 this is no longer working.

It was working beautifully,  but now this code and the latest code on the GitHub page - are not working...

Instead of showing Blocked messages or failed imports - now I just get a list of recent emails in the "Recently Blocked Messages" footer section..... what gives ?

 

 

 

 

Edited by sol2010
Link to comment
Share on other sites

  • 1 month later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated