PHPMailer 是 PHP 發送郵件的標準庫,支持 SMTP、HTML、附件、嵌入圖片、多語言錯誤信息等。本文整理其核心類 PHPMailer 及 SMTP 的常用屬性與方法,供快速查閱。
| 屬性 | 類型 | 默認值 | 說明 |
|---|---|---|---|
$Priority | int | 3 | 郵件優先級:1=高、3=正常、5=低 |
$CharSet | string | 'iso-8859-1' | 郵件編碼,中文環境建議 'UTF-8' |
$ContentType | string | 'text/plain' | 郵件 MIME 類型,HTML 郵件設為 'text/html' |
$Encoding | string | '8bit' | 內容傳輸編碼:8bit、7bit、binary、base64、quoted-printable |
$ErrorInfo | string | '' | 最近一次錯誤信息(只讀) |
$From | string | 'root@localhost' | 發件人郵箱地址 |
$FromName | string | 'Root User' | 發件人顯示名稱 |
$Sender | string | '' | Return-Path 地址(退信接收),SMTP 模式下作為 MAIL FROM |
$Subject | string | '' | 郵件主題 |
$Body | string | '' | 郵件正文(HTML 或純文本) |
$AltBody | string | '' | 純文本替代正文(非 HTML 客戶端顯示) |
$WordWrap | int | 0 | 自動換行字符數,0 為不換行 |
$Mailer | string | 'mail' | 發送方式:'mail'、'sendmail'、'smtp' |
$Sendmail | string | '/usr/sbin/sendmail' | sendmail 路徑 |
$Host | string | 'localhost' | SMTP 服務器地址,多個用分號分隔:'smtp1.example.com:25;smtp2.example.com:25' |
$Port | int | 25 | SMTP 端口 |
$SMTPAuth | bool | false | 是否啟用 SMTP 認證 |
$Username | string | '' | SMTP 用戶名 |
$Password | string | '' | SMTP 密碼 |
$SMTPDebug | int | 0 | 調試級別:0=關閉、1=客戶端、2=客戶端+服務端、3=連接信息、4=詳細 |
$SMTPKeepAlive | bool | false | 保持 SMTP 連接不關閉,批量發送時提升性能,需手動調用 SmtpClose() |
$Timeout | int | 300 | SMTP 操作超時秒數(Windows 下無效) |
$Helo | string | '' | SMTP HELO 域名,默認 $Hostname |
$Hostname | string | '' | Message-ID 和 Received 頭域使用的主機名 |
$Version | string | '5.2.x' | 庫版本號(只讀) |
| 方法 | 參數 | 返回 | 說明 |
|---|---|---|---|
SetLanguage($lang, $path='') | 語言代碼(如 'zh_cn')、語言包路徑 | bool | 設置錯誤信息語言,需對應 language/ 目錄下文件 |
IsSMTP() | 無 | void | 使用 SMTP 發送 |
IsMail() | 無 | void | 使用 PHP mail() 函數發送 |
IsSendmail() | 無 | void | 使用 sendmail 程序發送 |
IsHTML($bool=true) | bool | void | 設置正文為 HTML 格式 |
AddAddress($address, $name='') | 郵箱、名稱 | void | 添加收件人(可多次調用) |
AddCC($address, $name='') | 郵箱、名稱 | void | 添加抄送(僅 SMTP 模式有效) |
AddBCC($address, $name='') | 郵箱、名稱 | void | 添加密送(僅 SMTP 模式有效) |
AddReplyTo($address, $name='') | 郵箱、名稱 | void | 設置回復地址 |
AddAttachment($path, $name='', $encoding='base64', $type='application/octet-stream') | 文件路徑、顯示名、編碼、MIME類型 | bool | 添加附件 |
AddEmbeddedImage($path, $cid, $name='', $encoding='base64', $type='application/octet-stream') | 文件路徑、Content-ID、顯示名、編碼、類型 | bool | 添加嵌入圖片,HTML 中用 <img src="cid:cid_value"> 引用 |
AddStringAttachment($string, $filename, $encoding='base64', $type='application/octet-stream') | 字符串內容、文件名、編碼、類型 | bool | 直接添加字符串/二進制作為附件 |
Send() | 無 | bool | 發送郵件,失敗返回 false,錯誤見 $ErrorInfo |
ClearAddresses() | 無 | void | 清空所有收件人 |
ClearAttachments() | 無 | void | 清空所有附件 |
SmtpClose() | 無 | void | 關閉 SMTP 連接(配合 SMTPKeepAlive) |
| 方法 | 說明 |
|---|---|
Connect($host, $port, $timeout) | 建立 TCP 連接 |
Hello($host) | 發送 EHLO/HELO |
Authenticate($username, $password) | SMTP 認證(LOGIN/PLAIN/CRAM-MD5) |
Mail($from) | 發送 MAIL FROM |
Recipient($to) | 發送 RCPT TO |
Data($msg_data) | 發送郵件數據 |
Quit() | 發送 QUIT 並關閉連接 |
Reset() | 發送 RSET 重置會話 |
Verify($name) | 驗證用戶是否存在 (VRFY) |
Expand($list) | 展開郵件列表 (EXPN) |
<?php
require 'PHPMailerAutoload.php'; // 或 composer autoload
$mail = new PHPMailer(true); // true 開啟異常模式
try {
$mail->isSMTP();
$mail->Host = 'smtp.exmail.qq.com';
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = 'sender@domain.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'ssl'; // 或 'tls' (587 端口)
$mail->CharSet = 'UTF-8';
$mail->setFrom('sender@domain.com', '發件人名稱');
$mail->addAddress('recipient@example.com', '收件人');
$mail->addReplyTo('reply@domain.com', '回復地址');
$mail->isHTML(true);
$mail->Subject = '測試郵件';
$mail->Body = '<h1>HTML 內容</h1><p>支持中文</p>';
$mail->AltBody = '純文本備用內容';
// $mail->addAttachment('/path/to/file.pdf');
// $mail->addEmbeddedImage('/path/to/logo.png', 'logo_id');
$mail->send();
echo '發送成功';
} catch (Exception $e) {
echo "發送失敗: {$mail->ErrorInfo}";
}
?>
$mail->CharSet = 'UTF-8'; 且文件保存為 UTF-8 無 BOM。$mail->SMTPOptions = ['ssl' => ['verify_peer' => false, 'verify_peer_name' => false]];,生產環境請配置正確 CA 證書。$mail->Timeout,分片發送或使用雲郵件服務(SendGrid、Mailgun、阿里云郵件推送)。$mail->SMTPKeepAlive = true;,循環調用 addAddress/send/clearAddresses,最後 SmtpClose()。