php mail()有效,但随机空附件出现了

时间:2016-03-01 03:43:33

标签: php exchange-server php-5.5

我正在从我的网络服务器发送一封自动发送的电子邮件,它工作正常......除了我收到的电子邮件(在Microsoft Outlook中)有一个随机的空白文本文件(ATT00001.txt)显示。我不想在我的出站电子邮件中附加任何内容。我的所有搜索都显示了如何附加......我想要相反的......如何不附加任何东西!我究竟做错了什么?谢谢!

$uid = md5(uniqid(time()));
        $eol = PHP_EOL;

        $header = "From: Website Admin <".$MessageFrom.">".$eol;
        $header .= "Reply-To: ".$MessageFrom.$eol;

        //now add (optional) CC and BCC
        if($CcMessageTo!=''){ $header .= "Cc: $CcMessageTo".$eol; }
        if($BccMessageTo!=''){ $header .= "Bcc: $BccMessageTo".$eol; }

        //the next line is to request a read reciept
        if($RequestReadReceipt=='yes'){ $header .= "Disposition-Notification-To: ".$MessageFrom.$eol; }

        $header .= "MIME-Version: 1.0".$eol;
        $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"";

        $message = "--".$uid.$eol;
        $message .= "Content-Type: text/html; charset=ISO-8859-1".$eol;
        $message .= "Content-Transfer-Encoding: 8bit".$eol;
        $message .= $MessageBody.$eol;
        $message .= "--".$uid.$eol;

        //add an additional parameter to help not get blocked as spam
        $AdditionalParameters = "-f$MessageFrom";

        mail($MessageTo, $MessageSubject, $message, $header, $AdditionalParameters);

我发现唯一有用的是显然ExchangeServer不喜欢在文本之前看到附件(虽然我不认为我发送附件)。这是我找到的链接: http://kb.mit.edu/confluence/pages/viewpage.action?pageId=4981187

1 个答案:

答案 0 :(得分:1)

原来问题在于标题顶部的Content-Type:multipart / mixed。这显然告诉电子邮件客户端期望的不仅仅是电子邮件内容......因此它正在为缺少的附件组成一些填充物。我一改变了

  

内容类型:multipart / mixed

  

内容类型:text / html

一切都开始奏效了。当然,我还在整个过程中进行了一些调整,包括能够在需要时添加PDF附件(仅作为故障排除的一部分) - 所以这是我最终调整后的结果。希望有一天这会帮助某人。

if( $Attachment )
{
    $filename = $AttachmentName;
    $file = $_SERVER['DOCUMENT_ROOT'].$AttachmentSrc;
    $file_size = filesize($file);
    if($file_size > 0)
    {
        $handle = fopen($file, "r");
        $filecontent = fread($handle, $file_size);
        fclose($handle);
        $attachment = chunk_split(base64_encode($filecontent));
        $IncludeAttachment = 'yes';
    }
}

$uid = md5(uniqid(time()));

$header = "From: ".$MessageFromName." <".$MessageFromEmail.">".$eol;
$header .= "Reply-To: ".$MessageFromEmail.$eol;
if($CcMessageTo!=''){ $header .= "Cc: $CcMessageTo".$eol; }
if($BccMessageTo!=''){ $header .= "Bcc: $BccMessageTo".$eol; }
if($RequestReadReceipt=='yes'){ $header .= "Disposition-Notification-To: ".$MessageFromEmail.$eol; }
$header .= "MIME-Version: 1.0".$eol;
if($IncludeAttachment == 'yes')
{
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"";
    $message = "--".$uid.$eol;
    $message .= "Content-Type: text/html; charset=UTF-8".$eol;
    $message .= "Content-Transfer-Encoding: quoted-printable".$eol.$eol;
    $message .= quoted_printable_encode($MessageBody).$eol;
    $message .= "--".$uid.$eol;
    $message .= "Content-Type: application/pdf; name=\"".$filename."\"".$eol;
    $message .= "Content-Transfer-Encoding: base64".$eol;
    $message .= "Content-Disposition: attachment; filename=\"".$filename."\"".$eol.$eol;
    $message .= $attachment.$eol;
    $message .= "--".$uid."--";
}
else
{
    $header .= "Content-Type: text/html; charset=UTF-8".$eol;
    $header .= "Content-Transfer-Encoding: quoted-printable".$eol.$eol;
    $message .= quoted_printable_encode($MessageBody).$eol;
}

$AdditionalParameters = "-f$MessageFromEmail";

mail($MessageTo, $MessageSubject, $message, $header, $AdditionalParameters);