PHP邮件()附件未在Mac邮件中打开

时间:2009-09-14 15:31:46

标签: php email

我正在使用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";

提前致谢!

2 个答案:

答案 0 :(得分:0)

要避免Mac的邮件未打开附件,请确保其内容处置设置为内联。

答案 1 :(得分:-1)

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

您是否考虑过使用SwiftMailer或PHPMailer这样的库?在几乎所有情况下,您都可以获得更好的格式化,更少的麻烦,并且通常可以获得更好的性能。