2009-09-14 83 views
0

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

//define the receiver of the email 
$to = '[email protected]'; 
//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: [email protected] <[email protected]>"."\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"; 

在此先感谢!

+0

我注意到你现在将你的电子邮件和MIME头文件分开,只有一个“\ n”,这与你最后一个问题的不同。您是否尝试过为电子邮件标题做一个“\ n”,而对于mime标题则是“\ r \ n”? – Inshallah 2009-09-14 15:45:55

+0

感谢您的建议,但如果我这样做就会中断。 – Drew 2009-09-14 15:59:15

回答

-1

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

你有没有考虑过使用像SwiftMailer或PHPMailer这样的库?在几乎所有情况下,您都可以获得更好的格式化,更少的头痛,并且性能往往更好。

+0

感谢这一点,但如果我使用这些,我永远不会学习如何建立一个更好的。另外我已经尝试了几种不同的品种,并且他们都不在这台服务器上工作。 – Drew 2009-09-14 17:34:14

0

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