【问题标题】:PHP mail() attachment not opening in Mac mailPHP mail() 附件未在 Mac 邮件中打开
【发布时间】:2010-11-28 04:21:52
【问题描述】:

我正在使用 PHP mail() 发送电子邮件,并且几乎可以在我可以访问的每个程序中成功接收并打开附件(在本例中为 pdf)。除了在 mac 邮件中,我被告知文件已损坏。有没有其他人遇到过这个问题? 以下是我正在使用的脚本:

//define the receiver of the email
$to = 'bionic.comms@hotmail.com';
//define the subject of the email
$subject = 'Email with Attachment';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \n
$mime_boundary = "<<<--==+X[".md5(time())."]";

$path = $_SERVER['DOCUMENT_ROOT'].'/two/php/';
$fileContent =  chunk_split(base64_encode(file_get_contents($path.'CTF_brochure.pdf')));


$headers .= "From: info@poundsandpennies.org.uk <info@poundsandpennies.org.uk>"."\n";

    $headers .= "MIME-Version: 1.0\n" .
            "Content-Type: multipart/mixed;\n" .
            " boundary=\"{$mime_boundary}\"";   

$message = "This is a multi-part message in MIME format.\n";

$message .= "\n";
$message .= "--".$mime_boundary."\n";

$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "\n";
$message .= "Email content and what not: \n";
$message .= "This is the file you asked for! \n";
$message .= "--".$mime_boundary."" . "\n";

$message .= "Content-Type: application/octet-stream;\n";
$message .= " name=\"CTF-brochure.pdf\"" . "\n";
$message .= "Content-Transfer-Encoding: base64 \n";
$message .= "Content-Disposition: attachment;\n";
$message .= " filename=\"CTF_brochure.pdf\"\n";
$message .= "\n";
$message .= $fileContent;
$message .= "\n";
$message .= "--".$mime_boundary."--\n";

//send the email
$mail_sent = mail($to, $subject, $message, $headers);
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";

提前致谢!

【问题讨论】:

  • 我注意到您现在只用一个“\n”分隔您的电子邮件和 mime 标头,这在您上一个问题中有所不同。您是否尝试过为电子邮件标题做一个“\n”,而为 mime 标题做一个“\r\n”?
  • 感谢您的建议,但如果我这样做,它就会中断。

标签: php email


【解决方案1】:

不知道那里发生了什么,但我总是很惊讶有多少人尝试推出自己的邮件构造代码。

您是否考虑过只使用 SwiftMailer 或 PHPMailer 之类的库?在几乎所有情况下,您都会获得更好的格式、更少的麻烦和更好的性能。

【讨论】:

  • 感谢您,但如果我使用它们,我将永远无法学习如何打造更好的产品。另外,我尝试了几种不同的品种,但它们都不能在这台服务器上工作。
【解决方案2】:

为避免 Mac 的邮件不打开附件,请确保将其 Content-Disposition 设置为内联。

【讨论】:

    猜你喜欢
    • 2015-03-22
    • 1970-01-01
    • 2012-12-24
    • 2016-01-15
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 2010-12-09
    • 1970-01-01
    相关资源
    最近更新 更多