Search code examples
bashemailpipepostfix-mta

Piping emails from postfix under Plesk to Ostickets helpdesk


I'm trying to pipe support emails from my production mail server (Plesk 12 and Postfix) to send them to another VPS hosting my helpdesk installation, I have chosen ostickets helpdesk for many reasons (Open source,PHP/MySQL...)

So, the idea is to trigger email reception on some adresses such as contact@company.com, support@company.com... then resend them to ostickets via its API in order to create a new ticket there.

I tried this way http://blog.absolutedisaster.co.uk/osticket-plesk-9-postfix-pipe-mail-to-a-progr/

It explains how to create a pipe to trigger email, lunch php script ...etc

Because of some permission issues, I kept all this configuration and change just the last thing : php script

So I've replaced this php script by a simple bash script doing the same thing : send a request to API to create a new ticket via CURL.

Now In my production mail server the pipe is recognized and the email is trigged successfully :

 output /usr/local/psa/var/log/maillog in mail server

The same in the other side, osticket API is receiving the request:

 output /var/log/apache2/access.log in ostickets VPS

The problem is that the http response is 400 as you can see, which means ans error code #66 in ostickets (malformed email).

So, I think the problem is in my bash script :

exec curl --header 'X-API-Key: API_KEY_HERE' --user-agent 'osTicket API Client v1.7' --data - 'http://support.company.com/api/tickets.email'

the original PHP script I have replaced by this is :

#!/usr/bin/php -q
<?php

# Configuration: Enter the url and key. That is it.
#  url => URL to api/tickets.email e.g http://yourdomain.com/support/api/tickets.email
#  key => API's Key (see admin panel on how to generate a key)
#   

$config = array(
        'url'=>'http://support.company.com/api/tickets.email',
        'key'=>'API_KEY_HERE'
        );

#pre-checks
function_exists('file_get_contents') or die('upgrade php >=4.3');
function_exists('curl_version') or die('CURL support required');
#read stdin (piped email)
$data=file_get_contents('php://stdin') or die('Error reading stdin. No message');

#set timeout
set_time_limit(10);

#curl post
$ch = curl_init();        
curl_setopt($ch, CURLOPT_URL, $config['url']);        
curl_setopt($ch, CURLOPT_POST, 1);        
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_USERAGENT, 'osTicket API Client v1.7');
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Expect:', 'X-API-Key: '.$config['key']));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
$result=curl_exec($ch);        
curl_close($ch);

//Use postfix exit codes...expected by MTA.
$code = 75;
if(preg_match('/HTTP\/.* ([0-9]+) .*/', $result, $status)) {
    switch($status[1]) {
        case 201: //Success
            $code = 0;
            break;
        case 400:
            $code = 66;
            break;
        case 401: /* permission denied */
        case 403:
            $code = 77;
            break;
        case 415:
        case 416:
        case 417:
        case 501:
            $code = 65;
            break;
        case 503:
            $code = 69;
            break;
        case 500: //Server error.
        default: //Temp (unknown) failure - retry 
            $code = 75;
    }
}

exit($code);
?>

What is missing in my bash script ? (especially the stdin input)

thank you so much,

UPDATE The problem was indeed in the bash script, here is the solution I came up with:

#!/bin/bash
data=`cat -`
exec curl --header 'X-API-Key: API_KEY_HERE' --user-agent 'osTicket API Client v1.7' --data "$data" 'http://support.company.com/api/tickets.email'

Solution

  • The problem was indeed in the bash script, here is the solution I came up with:

    #!/bin/bash
    data=`cat -`
    exec curl --header 'X-API-Key: API_KEY_HERE' --user-agent 'osTicket API Client v1.7' --data "$data" 'http://support.company.com/api/tickets.email'