Jump to content

Forward an email to open a ticket Hook


WM Mods

Recommended Posts

Seen a number of people ask for this in the request community:

 

Many of the large ticketing systems (Zendesk, SupportPal, FreshDesk, etc.) allow the ability for an administrator/ticket operator to forward an email they receive in their personal mailbox to the ticketing system, and it will open the ticket "On Behalf" of the user who emailed them. I would like to see this ability in WHMCS for both existing and new customers who may email a staff member directly, instead of using the ticketing system.

 

I have created a hook which allows this to work. I  have tested on the most recent version of WHMCS and all seems okay.

 

<?

add_hook("TicketPiping",1,"fwd_ticket_pipe");

if(!function_exists("fwd_ticket_pipe"))
{
  function fwd_ticket_pipe($vars)
  {
    $to = $vars['to'];
    $subject = $vars['subject'];

    if (strpos($subject, 'Fwd:') !== false) {

      $adminonly = false; // For admin only import

      $subject = str_replace("Fwd: ", "", $vars['subject']);

      $lines = explode("\n", $vars['body']);
      $message = '';

      for ($i=0; $i < count($lines); $i++) {
      $lines[$i] = str_replace("> ","",$lines[$i]);

      //Split out the sender information portion
      if (preg_match("/From: (.*)<(.*)>/", $lines[$i], $matches)) {

        $name = $matches[1];
        $email = $matches[2];

      } else {

      }
      if($i > 8)
      {
        $message .= $lines[$i].'
        ';
      }
    }

    if($adminonly)
    {
      $adminrow = Illuminate\Database\Capsule\Manager::table("tbladmins")->where("email","=",$vars['email'])->count();
      if($adminrow > 0)
      {
        logActivity('Forwarded email is sent from an admin email account. It has been imported', 0);
        processPipedTicket($to, $name, $email, $subject, $message, $vars['attachments']);
      } else {
      	logActivity('Forwarded email is not sent from an admin email account. It has NOT been imported', 0);
      }

    } else {
      logActivity('Forwarded email is sent from a generic email account. It has been imported', 0);
      processPipedTicket($to, $name, $email, $subject, $message, $vars['attachments']);
    }

    exit();

    }

  }
} 

 

Link to comment
Share on other sites

  • 2 months later...

Unfortunately this no longer works as of WHMCS version 8.4.0

Any emails forwarded by the Admin receive a bounceback error stating that processPipedTicket() requires a 7th parameter.

After some trial and error I was able to work out that it needs an instance of the new WHMCS\Mail\AutoSubmittedHeader class. This seems to have been added to address this feature request: https://requests.whmcs.com/idea/ticketing-system-should-obey-rfc-3834-regarding-auto-responses

 

This can be updated using the following code.

At the top of the file, just above the `add_hook` function call, add:

use ZBateson\MailMimeParser\Message;
use WHMCS\Mail\AutoSubmittedHeader;

 

Just prior to the `if($adminonly)` line, add:

$mailParser = Message::from($vars['body'], false);
$asHeader = new AutoSubmittedHeader( $mailParser );

 

Then add `$asHeader` as another parameter to each instance of `processPipedTicket()`

eg:

processPipedTicket($to, $name, $email, $subject, $message, $vars['attachments'], $asHeader);

 

Link to comment
Share on other sites

  • 7 months later...

so it should now look like this?
I also notice there is code in here to decide if ticket forwarding is allowing according to if($adminonly), but where is this setting defined?

 

<?

use ZBateson\MailMimeParser\Message;
use WHMCS\Mail\AutoSubmittedHeader;
add_hook("TicketPiping",1,"fwd_ticket_pipe");

if(!function_exists("fwd_ticket_pipe"))
{
  function fwd_ticket_pipe($vars)
  {
    $to = $vars['to'];
    $subject = $vars['subject'];

    if (strpos($subject, 'Fwd:') !== false) {

      $adminonly = false; // For admin only import

      $subject = str_replace("Fwd: ", "", $vars['subject']);

      $lines = explode("\n", $vars['body']);
      $message = '';

      for ($i=0; $i < count($lines); $i++) {
      $lines[$i] = str_replace("> ","",$lines[$i]);

      //Split out the sender information portion
      if (preg_match("/From: (.*)<(.*)>/", $lines[$i], $matches)) {

        $name = $matches[1];
        $email = $matches[2];

      } else {

      }
      if($i > 8)
      {
        $message .= $lines[$i].'
        ';
      }
    }
   $mailParser = Message::from($vars['body'], false);
   $asHeader = new AutoSubmittedHeader( $mailParser );
    if($adminonly)
    {
      $adminrow = Illuminate\Database\Capsule\Manager::table("tbladmins")->where("email","=",$vars['email'])->count();
      if($adminrow > 0)
      {
        logActivity('Forwarded email is sent from an admin email account. It has been imported', 0);
        processPipedTicket($to, $name, $email, $subject, $message, $vars['attachments'], $asHeader);
      } else {
      	logActivity('Forwarded email is not sent from an admin email account. It has NOT been imported', 0);
      }

    } else {
      logActivity('Forwarded email is sent from a generic email account. It has been imported', 0);
      processPipedTicket($to, $name, $email, $subject, $message, $vars['attachments'], $asHeader);
    }

    exit();

    }

  }
} 
Link to comment
Share on other sites

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