使用PHP从sendmail管道创建目录并将文件写入程序

时间:2011-05-11 04:38:00

标签: php pipe sendmail

我有一个脚本从管道读取电子邮件(附件),我正在尝试将附件保存到磁盘以供进一步处理。我从一些网站拼凑了一些代码,而对于我的生活,我无法保存文件。我使用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。

1 个答案:

答案 0 :(得分:0)

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

一些调试提示:获取完整的电子邮件并将其保存到文件中。找出此脚本运行的用户(例如sendmail)。然后从命令行手动执行脚本并查看错误。 E.g:

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

确保您已启用错误报告等。

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

检入解码器的输入和输出。确保邮件按照您期望的方式进行解码。