";
$content .= "感謝您註冊會員!
";
$content .= "您可以享受以下服務:
";
$content .= "- 線上購物優惠
";
$content .= "- 專屬客服支援
";
$content .= "會員團隊
";
$content .= "{$date}
";
$content .= 'example.com';
// 發送
$result = mail($to, $subject, $content, $headers);
if ($result) {
echo "郵件發送成功!";
} else {
echo "郵件發送失敗,請檢查郵件伺服器設定。";
}
?>
格式:=?charset?encoding?encoded-text?=
charset:字元集(UTF-8、Big5 等)encoding:B = Base64,Q = Quoted-Printableencoded-text:編碼後的文字範例:=?UTF-8?B?5b2V54uZ5L2g5a6J?=
MIME-Version: 1.0
Content-Type: text/html; charset=UTF-8
宣告郵件遵循 MIME 標準,支援多部分內容、非 ASCII 字元、附件等。
| 類型 | 用途 | 範例 |
|---|---|---|
| text/plain | 純文字郵件 | 一般通知信 |
| text/html | HTML 郵件 | 行銷信、通訊 |
| multipart/mixed | 混合內容(含附件) | 帶附件郵件 |
| multipart/alternative | 替代內容(純文字+HTML) | 相容舊郵件客戶端 |
| application/pdf | PDF 附件 | 報表、發票 |
| image/jpeg | JPEG 圖片 | 內嵌圖片 |
| application/octet-stream | 任意二進位檔案 | 通用附件 |
您好,附件為本月報表\r\n";
$body .= "--{$boundary}\r\n";
// 附件部分
$filename = "report.pdf";
$filepath = "/var/www/files/report.pdf";
$filedata = chunk_split(base64_encode(file_get_contents($filepath)));
$body .= "Content-Type: application/pdf; name=\"{$filename}\"\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "Content-Disposition: attachment; filename=\"{$filename}\"\r\n\r\n";
$body .= "{$filedata}\r\n";
$body .= "--{$boundary}--\r\n";
mail($to, $subject, $body, $headers);
?>
\r\n(RFC 規範),單獨 \n 可能導致某些 MTA 拒收。mail() 迴圈,建議使用隊列(如 RabbitMQ、Redis)+ 背景處理程序,或改用 SMTP 庫。