使用 PHP 从发送邮件管道创建目录和写入文件到程序


Create directories and write files using PHP from a sendmail pipe to program

我有一个脚本,可以从管道读取电子邮件(带有附件),我正在尝试将附件保存到磁盘以供进一步处理。 我已经从几个站点拼凑了一些代码,在我的一生中,我无法保存文件。 我使用 777 作为 chmod 值,所以权限似乎不是问题,但我想知道在使用命令行处理器而不是浏览器时,我是否仅限于某些 PHP 命令。 此外,我什至对"include"目录进行了硬编码,以防文件未从其所在的目录执行。 任何帮助将不胜感激!

#!/usr/bin/php
<?php
//debug
#ini_set ("display_errors", "1");
#error_reporting(E_ALL);
include('/var/www/simple_html_dom.php');
//include email parser
require_once('/var/www/rfc822_addresses.php');
require_once('/var/www/mime_parser.php');
// read email in from stdin
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd)) {
    $email .= fread($fd, 1024);
}
fclose($fd);
//create the email parser class
$mime=new mime_parser_class;
$mime->ignore_syntax_errors = 1;
$parameters=array(
    'Data'=>$email,
);
$mime->Decode($parameters, $decoded);
//---------------------- GET EMAIL HEADER INFO -----------------------//
//get the name and email of the sender
$fromName = $decoded[0]['ExtractedAddresses']['from:'][0]['name'];
$fromEmail = $decoded[0]['ExtractedAddresses']['from:'][0]['address'];
//get the name and email of the recipient
$toEmail = $decoded[0]['ExtractedAddresses']['to:'][0]['address'];
$toName = $decoded[0]['ExtractedAddresses']['to:'][0]['name'];
//get the subject
$subject = $decoded[0]['Headers']['subject:'];
$removeChars = array('<','>');
//get the message id
$messageID = str_replace($removeChars,'',$decoded[0]['Headers']['message-id:']);
//get the reply id
//$replyToID = str_replace($removeChars,'',$decoded[0]['Headers']['in-reply-to:']);
//---------------------- FIND THE BODY -----------------------//
//get the message body
if(substr($decoded[0]['Headers']['content-type:'],0,strlen('text/plain')) == 'text/plain' && isset($decoded[0]['Body'])){
    $body = $decoded[0]['Body'];
} elseif(substr($decoded[0]['Parts'][0]['Headers']['content-type:'],0,strlen('text/plain')) == 'text/plain' && isset($decoded[0]['Parts'][0]['Body'])) {
    $body = $decoded[0]['Parts'][0]['Body'];
} elseif(substr($decoded[0]['Parts'][0]['Parts'][0]['Headers']['content-type:'],0,strlen('text/plain')) == 'text/plain' && isset($decoded[0]['Parts'][0]['Parts'][0]['Body'])) {
    $body = $decoded[0]['Parts'][0]['Parts'][0]['Body'];
}
    $my_dir = base64_encode($fromEmail);
    shell_exec('mkdir -p /var/www/tmp/' . $my_dir . ' -m 777');
    //mkdir($_SERVER['DOCUMENT_ROOT'] . "tmp/" . $my_dir, 0777, true);
    $target_path = "var/www/tmp/" . $my_dir;
    //chdir($target_path);
    //------------------------ ATTACHMENTS ------------------------------------//
//loop through email parts
foreach($decoded[0]['Parts'] as $part){
    //check for attachments
    if($part['Content-Disposition'] == 'attachment'){
        //format file name (change spaces to underscore then remove anything that isn't a letter, number or underscore)
        $filename = preg_replace('/[^0-9,a-z,'.,_]*/i','',str_replace(' ','_', $part['FileName']));
        // write the data to the file
        $fp = fopen($target_path . "/" . $filename, 'w');
        $written = fwrite($fp,$part['Body']);
        fclose($fp);
        //add file to attachments array
        $attachments[] = $part['FileName'];
    }
}
$html = file_get_html($attachments);

更新:感谢您的信息回复...我一直在试图弄清楚如何从命令行运行。 我现在遇到了一些错误,但它们仍然没有多大意义:

PHP Notice:  Undefined index: name in /var/www/catcher.php on line 38
PHP Notice:  Undefined index: Content-Disposition in /var/www/catcher.php on line 80
PHP Notice:  Undefined index: Content-Disposition in /var/www/catcher.php on line 80
PHP Notice:  Undefined variable: attachments in /var/www/catcher.php on line 97
PHP Warning:  file_get_contents(): Filename cannot be empty in /var/www/simple_html_dom.php on line 39

我已经指定了其他文件的完整包含路径,smmta 用户应该具有读取访问权限,因为/var/www/目录为 755。

如果运行脚本的用户无法写入目标目录,则对脚本中的 mkdir 命令使用0777权限根本不重要。因此,例如,如果此脚本按sendmail运行,请确保此用户可以写入/var/www/tmp

一些调试技巧:获取完整的电子邮件并将其保存到文件中。找出此脚本以哪个用户身份运行(例如 sendmail)。然后从命令行手动执行脚本并观察错误。例如:

sudo -u sendmail /path/to/script.php < /path/to/saved-email.eml

确保您打开了错误报告等。

编辑:查看您发布的错误,似乎 MIME 解码器无法按照您期望的方式正确解析您的消息。您似乎没有进行任何错误检查,因此您会收到有关未定义索引的通知和警告。

签入解码器的输入和输出。确保消息按照预期的方式解码。