PHP电子邮件拦截不发送带附件的电子邮件

发布于 2024-11-10 02:32:20 字数 3323 浏览 0 评论 0原文

我在我的服务器上设置了电子邮件拦截。

以下是我在服务器上设置的电子邮件转发器

[电子邮件受保护],"/home /server/php_pipe_mail.php”

以下是我的 php_pipe_mail.php 代码

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

require_once('mimeDecode.php');
include('sql-connect.php');
error_reporting(E_ALL);


ob_start();


$raw_email = '';



if (!$stdin = fopen("php://stdin", "R"))
{
    echo "ERROR: UNABLE TO OPEN php://stdin \n";
}

// ABLE TO READ THE MAIL
else
{
    while (!feof($stdin))
    {
        $raw_email .= fread($stdin, 4096);
    }
    fclose($stdin);
}



$raw_email = preg_replace('/ +/', ' ', $raw_email);


var_dump($raw_email);


$buf = ob_get_contents();

$params['include_bodies'] = true;
$params['decode_bodies'] = true;
$params['decode_headers'] = true;
$params['input'] = $buf;
$params['crlf'] = "\r\n"; 

//Creating temp file on server 
$myFile = "amail.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $buf);
fclose($fh);


//Generating mail structure in object format
$structure = Mail_mimeDecode::decode($params); 
$attachment = array();

$mail_date= date( 'Y-m-d H:i:s', strtotime($structure->headers['date']) );
$from = $structure->headers['from'];
$to = $structure->headers['to'];
$subject = htmlentities($structure->headers['subject'],ENT_QUOTES);
if($structure->ctype_primary == "multipart")
{
    $body_text = $structure->parts[0]->parts[0]->body;
$body_html = $structure->parts[0]->parts[1]->body;

$x = 0;
//fetch attachment
foreach ($structure->parts as $part) {
    // only save if an attachment
    if (isset($part->disposition) and ($part->disposition=='attachment')) {
        $attachment[$x]["filename"] = $part->d_parameters['filename'];
        $attachment[$x]["content_type"] = $part->ctype_primary . "/" .          $part->ctype_secondary;
        $attachment[$x]["body"] = addslashes($part->body);
        $x++;
    }
}
}
else 
{
$body_text = $structure->parts[0]->body;
$body_html = $structure->parts[1]->body;
}


$qry1 = "insert into mail_buffer(mail_date,mail_from,     mail_to,mail_subject,mail_text_body,mail_html_body) Values('". $mail_date ."','".$from."','".$to."','".$subject."','".$body_text."','".$body_html."')";

mysql_query($qry1) or die(mysql_error($con));

$last_id = mysql_insert_id();

if(count($attachment) > 0)
{
for($i=0; $i < count($attachment); $i++)
{
    $qry = "insert into mail_attachment(email_id,content_type, file_name,body) Values('". $last_id ."','".$attachment[$i]['content_type']."','".$attachment[$i]['filename']."','".$attachment[$i]['body']."')";
    mysql_query($qry) or die(mysql_error($con));
}
}



mysql_close($con);

ob_end_clean();

?>

现在上面的脚本工作得很好。

我能够获取邮件标题、正文和附件,并将它们存储在数据库中,没有任何问题。

当没有附件的电子邮件到来时,一切正常,并且电子邮件被发送到我正在拦截的电子邮件地址。

但以下不起作用。

当带有附件的电子邮件发送时,电子邮件内容存储在数据库中,但电子邮件未发送到我正在拦截的电子邮件地址,并且在退回电子邮件中收到以下错误消息。

您发送的消息无法发送至其一个或多个 收件人。这是一个永久性错误。以下地址失败:

管道到 |/home/server/php_pipe_mail.php 由 [email protected] 生成

任何人都可以帮助我解决此问题。

谢谢。

I have set email interception on my server.

following is my email forwarder set on server

[email protected],"/home/server/php_pipe_mail.php"

following is my code for php_pipe_mail.php

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

require_once('mimeDecode.php');
include('sql-connect.php');
error_reporting(E_ALL);


ob_start();


$raw_email = '';



if (!$stdin = fopen("php://stdin", "R"))
{
    echo "ERROR: UNABLE TO OPEN php://stdin \n";
}

// ABLE TO READ THE MAIL
else
{
    while (!feof($stdin))
    {
        $raw_email .= fread($stdin, 4096);
    }
    fclose($stdin);
}



$raw_email = preg_replace('/ +/', ' ', $raw_email);


var_dump($raw_email);


$buf = ob_get_contents();

$params['include_bodies'] = true;
$params['decode_bodies'] = true;
$params['decode_headers'] = true;
$params['input'] = $buf;
$params['crlf'] = "\r\n"; 

//Creating temp file on server 
$myFile = "amail.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $buf);
fclose($fh);


//Generating mail structure in object format
$structure = Mail_mimeDecode::decode($params); 
$attachment = array();

$mail_date= date( 'Y-m-d H:i:s', strtotime($structure->headers['date']) );
$from = $structure->headers['from'];
$to = $structure->headers['to'];
$subject = htmlentities($structure->headers['subject'],ENT_QUOTES);
if($structure->ctype_primary == "multipart")
{
    $body_text = $structure->parts[0]->parts[0]->body;
$body_html = $structure->parts[0]->parts[1]->body;

$x = 0;
//fetch attachment
foreach ($structure->parts as $part) {
    // only save if an attachment
    if (isset($part->disposition) and ($part->disposition=='attachment')) {
        $attachment[$x]["filename"] = $part->d_parameters['filename'];
        $attachment[$x]["content_type"] = $part->ctype_primary . "/" .          $part->ctype_secondary;
        $attachment[$x]["body"] = addslashes($part->body);
        $x++;
    }
}
}
else 
{
$body_text = $structure->parts[0]->body;
$body_html = $structure->parts[1]->body;
}


$qry1 = "insert into mail_buffer(mail_date,mail_from,     mail_to,mail_subject,mail_text_body,mail_html_body) Values('". $mail_date ."','".$from."','".$to."','".$subject."','".$body_text."','".$body_html."')";

mysql_query($qry1) or die(mysql_error($con));

$last_id = mysql_insert_id();

if(count($attachment) > 0)
{
for($i=0; $i < count($attachment); $i++)
{
    $qry = "insert into mail_attachment(email_id,content_type, file_name,body) Values('". $last_id ."','".$attachment[$i]['content_type']."','".$attachment[$i]['filename']."','".$attachment[$i]['body']."')";
    mysql_query($qry) or die(mysql_error($con));
}
}



mysql_close($con);

ob_end_clean();

?>

Now above script works perfectly fine.

I am able to fetch message header, body and attachments and can store them in database without any problems.

When email without attachments come everything works fine and email is delivered to email address I am intercepting.

But following is not working.

When email with attachments comes than email content is being stored in database but email is not delivering to email address I am intercepting and I am getting following error message in bounce back email.

A message that you sent could not be delivered to one or more of its
recipients. This is a permanent error. The following address(es) failed:

pipe to |/home/server/php_pipe_mail.php
generated by [email protected]

Can anyone help me regarding the matter.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

予囚 2024-11-17 02:32:20

当存在附件时,您的脚本是否会回显某些内容?我之前在管道电子邮件方面遇到过问题,并且看到返回给发件人的失败消息,这是由于管道脚本产生某种输出造成的。也许您的 error_reporting(E_ALL); 允许脚本生成输出 - 尝试 error_reporting(0);

Could it be that, when an attachment is present, your script is echoing something? I have had problems piping emails before, and seen failure messages returned to senders, and they have been due to the piping script producing some kind of output. Maybe your error_reporting(E_ALL); is allowing the script to produce an output - try error_reporting(0);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文