PHP傳送郵件:如何自定義reply-to頭部以及附件

2020-07-16 10:05:46
9.jpg

雖然有現成的類庫(如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傳送郵件:如何自定義reply-to頭部以及附件的詳細內容,更多請關注TW511.COM其它相關文章!