PHP mail() 函數雖簡單,但正確構建 Header 可實現 CC/BCC、HTML 郵件、優先級、自定義 Return-Path 等企業級功能。本文提供完整表單與發送腳本,並說明避坑要點。
<?php
// sendmail.php
if ($_SERVER['REQUEST_METHOD'] !== 'POST') exit('僅支援 POST');
$toName = trim($_POST['mailtoname'] ?? '');
$toEmail = trim($_POST['mailtomail'] ?? '');
$cc = trim($_POST['mailcc'] ?? '');
$bcc = trim($_POST['mailbcc'] ?? '');
$priority = (int)($_POST['mailpriority'] ?? 3);
$subject = trim($_POST['mailsubject'] ?? '');
$body = $_POST['mailbody'] ?? '';
if (!$toName || !$toEmail) die('收件人不可為空');
// 構建 Header
$headers = [];
$headers[] = "From: {$toName} <{$toEmail}>";
$headers[] = "Reply-To: {$toEmail}";
$headers[] = "X-Mailer: PHP/" . phpversion();
$headers[] = "X-Priority: {$priority}"; // 1=最高 3=正常 5=最低
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-Type: text/html; charset=UTF-8"; // HTML 郵件
if ($cc) $headers[] = "Cc: {$cc}";
if ($bcc) $headers[] = "Bcc: {$bcc}";
$headerStr = implode("\r\n", $headers);
// 發送
$result = mail($toEmail, $subject, $body, $headerStr);
if ($result) {
echo "郵件發送成功!<br/>";
echo "收件: {$toName} <{$toEmail}><br/>";
if ($cc) echo "抄送: {$cc}<br/>";
if ($bcc) echo "密件抄送: {$bcc}<br/>";
} else {
echo "郵件發送失敗,請檢查郵件伺服器日誌";
}
?>
<form method="post" action="sendmail.php">
<div><label>收件人姓名:</label> <input name="mailtoname" required></div>
<div><label>收件人郵箱:</label> <input type="email" name="mailtomail" required></div>
<div><label>抄送 (CC):</label> <input type="email" name="mailcc" placeholder="多個用逗號分隔"></div>
<div><label>密件抄送 (BCC):</label> <input type="email" name="mailbcc" placeholder="收件人不可見"></div>
<div><label>優先級:</label>
<select name="mailpriority">
<option value="1">最高</option>
<option value="2">高</option>
<option value="3" selected>正常</option>
<option value="4">低</option>
<option value="5">最低</option>
</select>
</div>
<div><label>主旨:</label> <input name="mailsubject" required></div>
<div><label>內容 (HTML):</label><br/>
<textarea name="mailbody" rows="10" cols="60"></textarea>
</div>
<button type="submit">發送郵件</button>
</form>
<?php
function sendMimeMail($to, $subject, $textBody, $htmlBody, $headers = []) {
$boundary = '==MULTIPART_BOUNDARY_' . md5(time() . mt_rand()) . '==';
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-Type: multipart/alternative; boundary=\"{$boundary}\"";
$message = "--{$boundary}\r\n";
$message .= "Content-Type: text/plain; charset=UTF-8\r\n";
$message .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
$message .= $textBody . "\r\n\r\n";
$message .= "--{$boundary}\r\n";
$message .= "Content-Type: text/html; charset=UTF-8\r\n";
$message .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
$message .= $htmlBody . "\r\n\r\n";
$message .= "--{$boundary}--\r\n";
return mail($to, $subject, $message, implode("\r\n", $headers));
}
// 使用
sendMimeMail(
'user@example.com',
'測試郵件',
'純文字內容給不支援 HTML 的客戶端',
'<h1>HTML 內容</h1><p>支援 <strong>富文本</strong></p>',
['From: system@example.com']
);
?>
建議使用 PHPMailer 或 Symfony Mailer 等成熟庫,手動構建 MIME 易出錯:
composer require phpmailer/phpmailer
<?php
use PHPMailer\PHPMailer\PHPMailer;
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'user';
$mail->Password = 'pass';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('from@example.com', '系統');
$mail->addAddress('to@example.com');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');
$mail->Subject = '帶附件';
$mail->Body = '<h1>HTML 內容</h1>';
$mail->AltBody = '純文字備份';
$mail->addAttachment('/path/to/file.pdf');
$mail->send();
?>
mail() 返回 true 僅表示「已提交給本地 MTA」,不代表送達。