我一直在努力嘗試使用PHP發送附件的電子郵件。它曾經工作,但消息正文被炒作。現在我得到了消息正文,但附件已損壞。我曾經使用base64編碼的消息正文,但現在使用7位。誰能告訴我我做錯了什麼?PHP郵件()附件損壞
PS請不要告訴我,我應該使用預製課程來做到這一點。我試了幾次,但都沒有成功。如果我沒有克服這些問題,我將永遠無法學會如何正確地做到這一點。由於
//define the receiver of the email
$to = '[email protected]';
//define the subject of the email
$subject = 'Your Disneyland Paris entry';
//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: 7bit\n";
$message .= "\n";
$message .= "messagebody \n";
$message .= "--".$mime_boundary."" . "\n";
$message .= "Content-Type: application/octet-stream;\n";
$message .= " name=\"CTF-brochure.pdf\"" . "\n";
$message .= "Content-Transfer-Encoding: 7bit \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";
可以理解的是,你希望自己做這件事,但除了僅僅理解如何正確編碼郵件......你如果要學習一些東西,我會移動對我的項目有趣的部分,沒有重新發明輪... – Palantir 2009-09-17 12:41:44
感謝您的意見。這是該項目唯一需要解決的問題。 :) – Drew 2009-09-17 13:08:52