PHPMailer 是一個廣泛使用的 PHP 郵件發送庫,彌補了 PHP 內建 mail() 函數功能不足的問題。它支援 SMTP 認證、多種郵件伺服器、HTML/純文字格式、附件、嵌入圖片、自訂標頭等豐富功能,採用 LGPL 授權。
require 'class.phpmailer.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = 'smtp1.example.com;smtp2.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'user';
$mail->Password = 'pass';
$mail->From = 'from@example.com';
$mail->FromName = 'Mailer';
$mail->AddAddress('josh@example.com', 'Josh Adams');
$mail->AddAddress('ellen@example.com');
$mail->AddReplyTo('info@example.com', 'Information');
$mail->WordWrap = 50;
$mail->AddAttachment('/var/tmp/file.tar.gz');
$mail->AddAttachment('/tmp/image.jpg', 'new.jpg');
$mail->IsHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the <b>HTML body</b>';
$mail->AltBody = 'This is the text-only body';
if (!$mail->Send()) {
echo 'Message was not sent';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';