I had an implementation with email hook setup with postfix in ubuntu server to pipe the incoming email to a script file. The implementation worked fine with apache and PHP 5.6. Recently I migrated the application to nginx with Fast CGI and PHP 7.1 and the email parser stopped working. Digging into I could only gather that when execution reaches at following code then it stops there for indefinite period of time:
$Parser->setStream(fopen("php://stdin", "r"));
I tested fopen("php://stdin","r") in plain php file and behavior was same, it got stuck there for indefinite period of time.
I am using PHP Mime Email Parser with mailparse. I don't think it is issue with Mail Parser as I ran fopen("php://stdin", "r")
in separate php file and stops there too.
Here is my complete code:
<?php
include('src/Attachment.php');
include('src/CharsetManager.php');
include('src/Charset.php');
//include ('src/Exception.php');
include('src/Middlewares.php');
include('src/Middleware.php');
include('src/MiddlewareStack.php');
include('src/MimePart.php');
include('src/Parser.php');
$attach_dir = dirname(__FILE__) . '/attachments';
$Parser = new Parser();
$Parser->setStream(fopen("php://stdin", "r"));//Execution hangs here
$attachments = $Parser->getAttachments();
if (count($attachments)) {
$from = $Parser->getHeader('from');
$fromParts = imap_rfc822_parse_adrlist($from, null);
$from_email = $fromParts[0]->mailbox . "@" . $fromParts[0]->host;
$attachmentPaths=array();
foreach ($attachments as $attachmentObj)
{
array_push($attachmentPaths,$attach_dir.DIRECTORY_SEPARATOR.$attachmentObj->getFilename());
}
$data=array(
'from_name'=>$fromParts[0]->personal,
'from'=>$from_email,
'attachments'=>$attachmentPaths
);
$Parser->saveAttachments($attach_dir);
$Parser->saveAttachments($attach_dir);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"MY_SITE_URL");//MY_SITE_URL is replaced for privacy
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "data=".json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
}