Page 1 of 1

Customer emails automatically turn into a ticket

Posted: Thu Nov 09, 2006 2:39 am
by fluffyturtle
I would love to see an add-on that would allow all emails to a specific address to automatically be turned into a ticket. Is this possible?

By the way, I LOVE, LOVE, LOVE this script. Awesome!

Posted: Thu Nov 09, 2006 4:43 pm
by Klemen
That's called e-mail piping, will be added someday but most likely not yet in the next version as I have a lot of things to consider first before making this work (handling SPAM, integrating piping, handling HTML e-mails, ...).

I have done this

Posted: Mon Oct 22, 2007 7:37 am
by pwillia6
1. Create a file in the support directory called process_email.php (note needs pecl package 'mailparse'). This script emulates the entry of the email:

Code: Select all

<?php

$filename = "php://stdin";
$_SERVER['REMOTE_ADDR'] = 'Via Email';

/* if we were called to display a part, do so now */
function getContent($message)
  {
    $mime = $mime = mailparse_msg_create ();
    mailparse_msg_parse($mime, $message);
    $struct = mailparse_msg_get_structure($mime);
    $parts = array();
    foreach($struct as $st)
      {
        $section = mailparse_msg_get_part($mime, $st);
        $info = mailparse_msg_get_part_data($section);
        ob_start();
        mailparse_msg_extract_part($section, $message);
        $content = ob_get_contents();
        ob_end_clean();

        if (ini_get('magic_quotes_gpc'))
            $info['content'] = mysql_escape_string($content);
        else
            $info['content'] = $content;
        $parts[$st] = $info;
      }
    return $parts;
  }

  $message = getContent(file_get_contents($filename));
  
  $_POST['subject'] = $message[1]['headers']['subject'];
  $addresses = mailparse_rfc822_parse_addresses($message[1]['headers']['from']);
  extract($addresses[0]);

  $name = $display == '' ? "UnKnown" : $display;
  $email = $address;
  
  $_POST['name'] = $name;
  $_POST['email'] = $email;
  $_POST['category'] = 1;
  $_POST['priority'] = 2;
  
  $custom1 = $custom2 = $custom3 = $custom4 = $custom5 = '';
  define('MAILUPLOAD',1);
  $_FILES = array();
  $_FILES['attachment']['tmp_name'] = array();
  $_FILES['attachment']['name'] = array();
  $_FILES['attachment']['size'] = array();

  $i = 1;
  
  foreach ($message as $index => $part)
    {
      if (preg_match('#^multipart/alternative#', $part['content-type']))
          $mIndex = $index;

      if (isset($mIndex) && preg_match('/^' . $mIndex . '/', $index))
        {
          if (preg_match('#text/plain#',$part['content-type']))
              $body = $part['content'];
        }
      else
          unset($mIndex);
          
      if (!isset($body) && preg_match('#text/plain#',$part['content-type']))
          $body = $part['content'];

      if (!isset($body) && preg_match('#text/html#',$part['content-type']))
          $body = $part['content'];

      if (isset($part['content-name']))
        {
          $tempFile  = tempnam('/tmp','mime_');
          file_put_contents($tempFile, $part['content']);
          $_FILES['attachment']['tmp_name'][$i] = $tempFile;
          $_FILES['attachment']['name'][$i] = $part['content-name'] ;
          $_FILES['attachment']['size'][$i] = strlen($part['content']);
          $i++;
        }
    }

  $_POST['message'] = $body;
  
  include("submit_ticket.php");
?>
2. If you want to process attachments then modify inc/attachements.php.inc as follows:

Change:

Code: Select all

    if (!move_uploaded_file($_FILES['attachment']['tmp_name'][$i], $hesk_settings['server_path'].'/attachments/'.$file_name)) {
        hesk_error($hesklang['cannot_move_tmp']);
   }
to:

Code: Select all

if (defined('MAILUPLOAD')) {
    if (!rename($_FILES['attachment']['tmp_name'][$i], $hesk_settings['server_path'].'/attachments/'.$file_name)) {
        hesk_error($hesklang['cannot_move_tmp']);
   }
} else {
    if (!move_uploaded_file($_FILES['attachment']['tmp_name'][$i], $hesk_settings['server_path'].'/attachments/'.$file_name)) {
        hesk_error($hesklang['cannot_move_tmp']);
   }
}
3. Set up you email filter to pipe the email content to this script making sure the output goes to null. e.g. Use the following script:

Code: Select all

#!/bin/bash
cd wwwdir/support
/usr/local/bin/php wwwdir/support/process_email.php > /dev/null
I would recommend that you copy the email to another mailbox in case the script looses it. It does not have any error handling if something goes wrong.

This still needs a bit of refinement.

Posted: Sun Oct 28, 2007 6:44 pm
by bluenix
Hi,

I created the script process_email.php in the support directory, and installed the mailparse package.

I piped the mail to the script using

Code: Select all

"|/usr/local/bin/php /home/.../process_email.php > /dev/null"
When sending an email to the mail address, I get "Mail delivery failed", together with:

Code: Select all

A message that you sent could not be delivered to one or more of its
recipients. This is a permanent error. The following address(es) failed:

 pipe to |/usr/local/bin/php /home/.../process_email.php > /dev/null
   generated by test@domain.com
   local delivery failed
 pipe to |/usr/local/bin/php /home/.../process_email.php > /dev/null
   generated by test@domain.com
   local delivery failed

The following text was generated during the delivery attempts:

------ pipe to |/usr/local/bin/php /home/.../process_email.php > /dev/null
      generated by test@domain.com ------

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>HelpDesk</title>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1" />
<link href="hesk_style.css" type="text/css" rel="stylesheet">
<script language="Javascript" type="text/javascript" src="hesk_javascript.js"><!--
// -->
</script>
</head>
<body marginwidth="0" leftmargin="0">

PHP Warning:  main(header.txt): failed to open stream: No such file or directory in /home/.../inc/header.inc.php on line 45
PHP Warning:  main(): Failed opening 'header.txt' for inclusion (include_path='.:/usr/local/lib/php') in /home/.../inc/header.inc.php on line 45

<div align="center">
<center>
<table border="0" cellspacing="0" cellpadding="5" width="700" class="enclosing">
<tr>
<td>
PHP Warning:  hesk_error(inc/header.inc.php): failed to open stream: No such file or directory in /home/.../inc/common.inc.php on line 356
PHP Fatal error:  hesk_error(): Failed opening required 'inc/header.inc.php' (include_path='.:/usr/local/lib/php') in /home/.../inc/common.inc.php on line 356
My exim log is telling me:

Code: Select all

Child process of virtual_address_pipe transport returned 255 (could mean shell command ended by signal 127 (Unknown signal 127)) from command: /usr/local/bin/php
Any suggestions on what I'm doing wrong?
Thanks in advance!

Posted: Mon Oct 29, 2007 8:33 am
by pwillia6
Your current directory needs to be the support directory. See the bash script above.

Also in the code above replace the line with the mysql_escape with:

$info['content'] = str_replace("'","\\'",$content);

The mysql escapes to much.

Posted: Mon Oct 29, 2007 8:56 am
by Klemen
Thanks for sharing pwillia6.

Just a heads-up if you are getting errors like "undefined function", this mod requires the Mailparse extension, see http://php.net/mailparse

Posted: Mon Oct 29, 2007 2:14 pm
by bluenix
pwillia6 wrote:Your current directory needs to be the support directory. See the bash script above.

Also in the code above replace the line with the mysql_escape with:

$info['content'] = str_replace("'","\\'",$content);

The mysql escapes to much.
Thanks for the quick reply!
Maybe a stupid question, but what exactly shoul I do with the bash script?... Should I pipe the mail to that script instead of to the process_email.php, so that the pipe rule would be:

Code: Select all

"|/home/.../bashscript"
?

---
edit:

Yes, that way it works! The only problem now is that for each message received, 2 seperate tickets are created... Any suggestions?

---
another edit:

Fixed that too, by commenting out virtual_aliases_nostar in exim.conf

Do I Need To Specify A E-Mail Address?

Posted: Sat Dec 01, 2007 7:29 pm
by JacobRocks
Do I Need To Specify A E-Mail Address? And If So How?

Posted: Wed Mar 26, 2008 6:48 pm
by adamparadigm
are there any solutions that do not require "Mailparse"... I'm on a shared hosting plan and I'm not sure my host will install it. Is there any way to use "mailparse" on a shared hosting plan?

Posted: Wed Mar 26, 2008 7:53 pm
by Klemen
Unless your host has it installed or can install it then no.