雖然有現成的類別庫(如PEAR)可以很方便地實現附件添加和發送,但是對於一些小站點(伺服器硬體、網站規模都不理想),安裝PEAR可能會帶來不必要的負擔,降低WEB程式運作效率。
透過對郵件格式的認識,我們可以寫一個腳本來傳送附件。程式碼不長:
[php]
function mailSend($to, $subject, $message, $attach, $from, $replyto) { //定义边界线 $boundary = uniqid(); //生成邮件头 $header = "From: $from Reply-to:$replyto Content-type: multipart/mixed; boundary=\"$boundary\""; //获取附件文件的MIME类型 $mimeType = mime_content_type($attach); //对附件文件进行编码和切分 $fp = fopen($attach, "r"); if ($fp) { $content = fread($fp, filesize($attach)); $content = chunk_split(base64_encode($content)); fclose($fp); } else { die("Failed to open file…"); } //生成邮件主体 $body = " –$boundary Content-type: text/plain; charset=utf-8; Content-transfer-encoding: 8bit $message –$boundary Content-Type: $mimeType; name=$attach Content-Disposition: attachment; filename=$attach Content-Transfer-Encoding: base64 $content –$boundary–"; //发送邮件 mail($to, $subject, $body, $header) or die("Failed to send mail…"); }
#[/php]
更多PHP相關知識,請訪問PHP中文網!
以上是PHP發送郵件:如何自訂reply-to頭部以及附件的詳細內容。更多資訊請關注PHP中文網其他相關文章!