Link to home
Start Free TrialLog in
Avatar of Steve Marin
Steve MarinFlag for United States of America

asked on

Email Piping | Postfix-Maildrop | ISPConfig | OSTicket - Not working

Hey all,

I have an Ubuntu Server running ISPConfig on it. I just installed OSTicket on it and am trying to get email piping working so it automatically creates a ticket in the system. Here is the error I get when I send an email to the support email address.

Also attached is the command I have in ISPConfig for the pipe and the pipe.php script, and permissions I have set right now.


pipe command I'm using
============================================================================
to "|/var/www/clients/client1/web1/web/helpdesk/api/pipe.php"


Error when email sent to support@mydomain.com
==============================================================================

The mail system

<support@mydomaine.com>: Command died with status 126: "/usr/bin/maildrop".
    Command output: ERR: authdaemon: s_connect() failed: Permission denied sh:
    /var/www/clients/client1/web1/web/helpdesk/api/pipe.php: Permission denied

Permissions on helpdesk dir.
=====================================================================================
root@pebkac:/var/www/clients/client1/web1/web# ls -l helpdesk/
total 84
drwxrwxrwx 2 web1 client1 4096 2010-01-28 07:21 api
-rw-r--r-- 1 web1 client1 2698 2010-01-26 20:23 attachment.php
-rw-r--r-- 1 web1 client1 1783 2010-01-26 20:23 client.inc.php
drwxrwxrwx 2 root root    4096 2010-01-26 20:32 files
drwxr-xr-x 3 web1 client1 4096 2010-01-26 20:23 images
drwxr-xr-x 5 web1 client1 4096 2010-01-26 20:23 include
-rw-r--r-- 1 web1 client1 2384 2010-01-26 20:23 index.php
-rw-r--r-- 1 web1 client1 4517 2010-01-26 20:23 login.php
-rw-r--r-- 1 web1 client1  729 2010-01-26 20:23 logout.php
-rw-r--r-- 1 web1 client1 6198 2010-01-26 20:23 main.inc.php
-rw-r--r-- 1 web1 client1 1221 2010-01-26 20:23 offline.php
-rw-r--r-- 1 web1 client1 1386 2010-01-26 20:23 open.php
drwxr-xr-x 5 web1 client1 4096 2010-01-26 20:23 scp
drwxr-xr-x 2 web1 client1 4096 2010-01-26 20:23 scripts
-rw-r--r-- 1 web1 client1  841 2010-01-26 20:23 secure.inc.php
drwxr-xr-x 3 web1 client1 4096 2010-01-26 20:42 setup
drwxr-xr-x 2 web1 client1 4096 2010-01-26 20:23 styles
-rw-r--r-- 1 web1 client1 3513 2010-01-26 20:23 tickets.php
-rw-r--r-- 1 web1 client1  120 2010-01-26 20:23 view.php
root@pebkac:/var/www/clients/client1/web1/web#

Permissions on pipe.php
=================================================================
root@pebkac:/var/www/clients/client1/web1/web/helpdesk/api# ls -l
total 28
-rwxrwxrwx 1 web1 client1 3023 2010-01-26 20:23 api.inc.php
-rwxrwxrwx 1 web1 client1  716 2010-01-26 20:23 cron.php
-rwxrwxrwx 1 web1 client1   34 2010-01-26 20:23 index.php
-rwxrwxrwx 1 web1 client1 4971 2010-01-27 16:08 pipe.php
-rwxr-xr-x 1 root root    4968 2010-01-27 16:03 pipe.php~

pipe.php file
==============================================================
#!/usr/bin/php -q
<?php
/*********************************************************************
    pipe.php

    Converts piped emails to ticket. Both local and remote!

    Peter Rotich <peter@osticket.com>
    Copyright (c)  2006,2007,2008,2009 osTicket
    http://www.osticket.com

    Released under the GNU General Public License WITHOUT ANY WARRANTY.
    See LICENSE.TXT for details.

    vim: expandtab sw=4 ts=4 sts=4:
    $Id: $
**********************************************************************/
@chdir(realpath(dirname(__FILE__)).'/'); //Change dir.
ini_set('memory_limit', '256M'); //The concern here is having enough mem for emails with attachments.
require('api.inc.php');
require_once(INCLUDE_DIR.'class.mailparse.php');
require_once(INCLUDE_DIR.'class.email.php');

//Make sure piping is enabled!
if(!$cfg->enableEmailPiping())
    api_exit(EX_UNAVAILABLE,'Email piping not enabled - check MTA settings.');
//Get the input
$data=isset($_SERVER['HTTP_HOST'])?file_get_contents('php://input'):file_get_contents('php://stdin');
if(empty($data)){
    api_exit(EX_NOINPUT,'No data');
}

//Parse the email.
$parser= new Mail_Parse($data);
if(!$parser->decode()){ //Decode...returns false on decoding errors
    api_exit(EX_DATAERR,'Email parse failed ['.$parser->getError()."]\n\n".$data);    
}

//Check from address. make sure it is not a banned address.
$fromlist = $parser->getFromAddressList();
//Check for parsing errors on FROM address.
if(!$fromlist || PEAR::isError($fromlist)){
    api_exit(EX_DATAERR,'Invalid FROM address ['.$fromlist?$fromlist->getMessage():''."]\n\n".$data);
}

$from=$fromlist[0]; //Default.
foreach($fromlist as $fromobj){
    if(!Validator::is_email($fromobj->mailbox.'@'.$fromobj->host))
        continue;
    $from=$fromobj;
    break;
}

//TO Address:Try to figure out the email associated with the message.
$tolist = $parser->getToAddressList();
foreach ($tolist as $toaddr){
    if(($emailId=Email::getIdByEmail($toaddr->mailbox.'@'.$toaddr->host))){
        //We've found target email.
        break;
    }
}
if(!$emailId && ($cclist=$parser->getCcAddressList())) {
    foreach ($cclist as $ccaddr){
        if(($emailId=Email::getIdByEmail($ccaddr->mailbox.'@'.$ccaddr->host))){
            break;
        }
    }
}
//TODO: Options to reject emails without a matching To address in db? May be it was Bcc? Current Policy: If you pipe, we accept policy

require_once(INCLUDE_DIR.'class.ticket.php'); //We now need this bad boy!

$var=array();
$deptId=0;
$name=trim($from->personal,'"');
if($from->comment && $from->comment[0])
    $name.=' ('.$from->comment[0].')';
$subj=utf8_encode($parser->getSubject());
if(!($body=Format::stripEmptyLines($parser->getBody())) && $subj)
    $body=$subj;

$var['mid']=$parser->getMessageId();
$var['email']=$from->mailbox.'@'.$from->host;
$var['name']=$name?utf8_encode($name):$var['email'];
$var['emailId']=$emailId?$emailId:$cfg->getDefaultEmailId();
$var['subject']=$subj?$subj:'[No Subject]';
$var['message']=utf8_encode(Format::stripEmptyLines($body));
$var['header']=$parser->getHeader();
$var['pri']=$cfg->useEmailPriority()?$parser->getPriority():0;

$ticket=null;
if(ereg ("[[][#][0-9]{1,10}[]]",$var['subject'],$regs)) {
    $extid=trim(preg_replace("/[^0-9]/", "", $regs[0]));
    $ticket= new Ticket(Ticket::getIdByExtId($extid));
    //Allow mismatched emails?? For now hell NO.
    if(!is_object($ticket) || strcasecmp($ticket->getEmail(),$var['email']))
        $ticket=null;
}        
$errors=array();
$msgid=0;
if(!$ticket){ //New tickets...
    $ticket=Ticket::create($var,$errors,'email');
    if(!is_object($ticket) || $errors){
        api_exit(EX_DATAERR,'Ticket create Failed '.implode("\n",$errors)."\n\n");
    }
    $msgid=$ticket->getLastMsgId();
}else{
    $message=$var['message'];
    //Strip quoted reply...TODO: figure out how mail clients do it without special tag..
    if($cfg->stripQuotedReply() && ($tag=$cfg->getReplySeparator()) && strpos($var['message'],$tag))
        list($message)=split($tag,$var['message']);
    //post message....postMessage does the cleanup.
    if(!($msgid=$ticket->postMessage($message,'Email',$var['mid'],$var['header']))) {
        api_exit(EX_DATAERR,"Unable to post message \n\n $message\n");
    }
}
//Ticket created...save attachments if enabled.
$struct=$parser->getStruct();       
if($struct && $struct->parts && $cfg->allowEmailAttachments()) {                   
    for($i = 0; $i < count($struct->parts); $i++) {
        $part=$struct->parts[$i];
        if($part->disposition 
                && (!strcasecmp($part->disposition,'attachment') || !strcasecmp($part->disposition,'inline') || !strcasecmp($part->ctype_primary,'image'))){
            $filename=$part->d_parameters['filename'];
            if($filename && $cfg->canUploadFileType($filename)) {
                $ticket->saveAttachment($filename,$part->body,$msgid,'M');
            }
        }
    }
}
//print_r($var);
api_exit(EX_SUCCESS);
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Steve Marin
Steve Marin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial