我正在使用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";
在此先感谢!
我注意到你现在将你的电子邮件和MIME头文件分开,只有一个“\ n”,这与你最后一个问题的不同。您是否尝试过为电子邮件标题做一个“\ n”,而对于mime标题则是“\ r \ n”? – Inshallah 2009-09-14 15:45:55
感谢您的建议,但如果我这样做就会中断。 – Drew 2009-09-14 15:59:15