損毀的附件???
我花了很多時間處理損毀的附件(各種檔案類型) - 答案:在 $msg.=$file \r\n \r\n 之後需要一個空行[令人難以置信但真實]。
這是一些用於傳送附件的實用程式碼,並根據使用者的電子郵件閱讀器顯示 html 或文字。
我使用許多不同的系統,所以...
<?php if (strtoupper(substr(PHP_OS,0,3)=='WIN')) {
$eol="\r\n";
} elseif (strtoupper(substr(PHP_OS,0,3)=='MAC')) {
$eol="\r";
} else {
$eol="\n";
} ?>
<?php
$f_name="../../letters/".$letter; $handle=fopen($f_name, 'rb');
$f_contents=fread($handle, filesize($f_name));
$f_contents=chunk_split(base64_encode($f_contents)); $f_type=filetype($f_name);
fclose($handle);
$emailaddress="user@example.com";
$emailsubject="這是一封帶有 PDF 附件的郵件".date("Y/m/d H:i:s");
ob_start();
require("emailbody.php"); $body=ob_get_contents(); ob_end_clean();
$headers .= 'From: Jonny <jon@example.com>'.$eol;
$headers .= 'Reply-To: Jonny <jon@example.com>'.$eol;
$headers .= 'Return-Path: Jonny <jon@example.com>'.$eol; $headers .= "Message-ID:<".$now." TheSystem@".$_SERVER['SERVER_NAME'].">".$eol;
$headers .= "X-Mailer: PHP v".phpversion().$eol; $mime_boundary=md5(time());
$headers .= 'MIME-Version: 1.0'.$eol;
$headers .= "Content-Type: multipart/related; boundary=\"".$mime_boundary."\"".$eol;
$msg = "";
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: application/pdf; name=\"".$letter."\"".$eol; $msg .= "Content-Transfer-Encoding: base64".$eol;
$msg .= "Content-Disposition: attachment; filename=\"".$letter."\"".$eol.$eol; $msg .= $f_contents.$eol.$eol;
$msg .= "Content-Type: multipart/alternative".$eol;
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: text/plain; charset=iso-8859-1".$eol;
$msg .= "Content-Transfer-Encoding: 8bit".$eol;
$msg .= "這是一封 MIME 格式的多部分郵件。".$eol;
$msg .= "如果您看到此訊息,請更新您的電子郵件軟體。".$eol;
$msg .= "+ + 來自天才 Jon 的純文字電子郵件 + +".$eol.$eol;
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: text/html; charset=iso-8859-1".$eol;
$msg .= "Content-Transfer-Encoding: 8bit".$eol;
$msg .= $body.$eol.$eol;
$msg .= "--".$mime_boundary."--".$eol.$eol; ini_set(sendmail_from,'from@example.com'); mail($emailaddress, $emailsubject, $msg, $headers);
ini_restore(sendmail_from);
?>
希望這對您有幫助。
Jon Webb [馬德里&倫敦]
[編輯者 thiago 註:程式碼已修正來自 o.straesser AT gmx DOT de 的錯誤]