許多 PHP 開發人員需要從他們的程式碼發送電子郵件。唯一直接支援此功能的 PHP 函數是mail()
。但是,它不為使用加密、身份驗證、HTML 訊息和附件等流行功能提供任何幫助。
正確設定電子郵件格式非常困難。有無數重疊(和衝突)的標準,需要嚴格遵守極其複雜的格式和編碼規則——您在網路上找到的絕大多數直接使用mail()
函數的程式碼即使不是不安全,也是完全錯誤的!
PHP mail()
函數通常透過本機郵件伺服器傳送,在 Linux、BSD 和 macOS 平台上通常以sendmail
二進位檔案開頭,但 Windows 通常不包含本機郵件伺服器; PHPMailer 的整合式 SMTP 用戶端允許在所有平台上傳送電子郵件,而無需本機郵件伺服器。但請注意,應盡量避免使用mail()
函數;使用 SMTP 到本機既更快又更安全。
請不要嘗試自己動手 - 如果您不使用 PHPMailer,那麼在推出自己的程式庫之前,您應該先了解許多其他優秀的程式庫。試試 SwiftMailer 、 Laminas/Mail 、 ZetaComponents 等。
該軟體根據 LGPL 2.1 授權以及 GPL 合作承諾進行分發。請閱讀許可證以獲取有關軟體可用性和分發的資訊。
PHPMailer 可在 Packagist 上使用(使用語意版本控制),建議透過 Composer 安裝 PHPMailer。只需將此行添加到您的composer.json
檔案中:
"phpmailer/phpmailer" : " ^6.9.2 "
或運行
composer require phpmailer/phpmailer
注意vendor
資料夾和vendor/autoload.php
腳本是由Composer產生的;它們不是 PHPMailer 的一部分。
如果您想使用 XOAUTH2 驗證,您還需要在您的composer.json
中新增對league/oauth2-client
和適當的服務適配器套件的依賴項,或者查看 @decomplexity 的 SendOauth2 包裝器,特別是如果您使用微軟服務。
或者,如果您不使用 Composer,則可以將 PHPMailer 作為 zip 檔案下載(請注意,zip 檔案中不包含文件和範例),然後將 PHPMailer 資料夾的內容複製到中指定的include_path
目錄之一您的PHP 配置並手動載入每個類文件:
<?php
use PHPMailer PHPMailer PHPMailer ;
use PHPMailer PHPMailer Exception ;
require ' path/to/PHPMailer/src/Exception.php ' ;
require ' path/to/PHPMailer/src/PHPMailer.php ' ;
require ' path/to/PHPMailer/src/SMTP.php ' ;
如果您沒有明確使用SMTP
類別(您可能沒有),則不需要 SMTP 類別的use
行。即使您不使用異常,您仍然需要載入Exception
類,因為它是在內部使用的。
PHPMailer 5.2(與 PHP 5.0 - 7.0 相容)不再受支持,即使是安全性更新也是如此。您將在 5.2-stable 分支中找到最新版本的 5.2。如果您使用的是 PHP 5.5 或更高版本(您應該使用),請切換到 6.x 版本。
最大的變化是來源檔案現在位於src/
資料夾中,而 PHPMailer 現在聲明名稱空間PHPMailerPHPMailer
。這有幾個重要的影響 - 請閱讀升級指南以獲取更多詳細資訊。
雖然手動或使用 Composer 安裝整個套件簡單、方便且可靠,但您可能只想在專案中包含重要檔案。至少你需要 src/PHPMailer.php。如果您使用 SMTP,則需要 src/SMTP.php,如果您使用 POP-before SMTP(不太可能!),則需要 src/POP3.php。如果您不向使用者顯示錯誤並且可以解決僅英語錯誤,則可以跳過語言資料夾。如果您使用 XOAUTH2,您將需要 src/OAuth.php 以及您希望進行身份驗證的服務的 Composer 依賴項。確實,使用 Composer 會比較簡單!
<?php
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer PHPMailer PHPMailer ;
use PHPMailer PHPMailer SMTP ;
use PHPMailer PHPMailer Exception ;
//Load Composer's autoloader
require ' vendor/autoload.php ' ;
//Create an instance; passing `true` enables exceptions
$ mail = new PHPMailer ( true );
try {
//Server settings
$ mail -> SMTPDebug = SMTP :: DEBUG_SERVER ; //Enable verbose debug output
$ mail -> isSMTP (); //Send using SMTP
$ mail -> Host = ' smtp.example.com ' ; //Set the SMTP server to send through
$ mail -> SMTPAuth = true ; //Enable SMTP authentication
$ mail -> Username = ' [email protected] ' ; //SMTP username
$ mail -> Password = ' secret ' ; //SMTP password
$ mail -> SMTPSecure = PHPMailer:: ENCRYPTION_SMTPS ; //Enable implicit TLS encryption
$ mail -> Port = 465 ; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
//Recipients
$ mail -> setFrom ( ' [email protected] ' , ' Mailer ' );
$ mail -> addAddress ( ' [email protected] ' , ' Joe User ' ); //Add a recipient
$ mail -> addAddress ( ' [email protected] ' ); //Name is optional
$ mail -> addReplyTo ( ' [email protected] ' , ' Information ' );
$ mail -> addCC ( ' [email protected] ' );
$ mail -> addBCC ( ' [email protected] ' );
//Attachments
$ mail -> addAttachment ( ' /var/tmp/file.tar.gz ' ); //Add attachments
$ mail -> addAttachment ( ' /tmp/image.jpg ' , ' new.jpg ' ); //Optional name
//Content
$ mail -> isHTML ( true ); //Set email format to HTML
$ mail -> Subject = ' Here is the subject ' ;
$ mail -> Body = ' This is the HTML message body <b>in bold!</b> ' ;
$ mail -> AltBody = ' This is the body in plain text for non-HTML mail clients ' ;
$ mail -> send ();
echo ' Message has been sent ' ;
} catch ( Exception $ e ) {
echo " Message could not be sent. Mailer Error: { $ mail -> ErrorInfo }" ;
}
您會在範例資料夾中找到許多可以使用的內容,其中涵蓋了許多常見場景,包括透過 Gmail 發送、建立聯絡表單、傳送到郵件清單等等。
如果您重複使用該實例(例如,當您傳送至郵件清單時),您可能需要清除收件者清單以避免傳送重複的訊息。請參閱郵件清單範例以獲得進一步的指導。
就是這樣。現在您應該可以使用 PHPMailer 了!
PHPMailer 預設為英語,但在語言資料夾中,您會發現許多可能遇到的 PHPMailer 錯誤訊息的翻譯。它們的檔案名稱包含翻譯的 ISO 639-1 語言代碼,例如fr
表示法語。要指定一種語言,您需要告訴 PHPMailer 使用哪種語言,如下所示:
//To load the French version
$ mail -> setLanguage ( ' fr ' , ' /optional/path/to/language/directory/ ' );
我們歡迎更正和新語言 - 如果您正在尋找更正,請執行測試資料夾中的 Language/TranslationCompletenessTest.php 腳本,它將顯示任何缺少的翻譯。
從 GitHub wiki 開始閱讀。如果您遇到問題,請參閱故障排除指南,因為它會經常更新。
有關如何在常見場景中使用 PHPMailer 的範例可以在範例資料夾中找到。如果您正在尋找一個好的起點,我們建議您從 Gmail 範例開始。
為了減少 PHPMailer 的部署程式碼佔用量,如果您透過 Composer 或透過 GitHub 的 zip 檔案下載載入 PHPMailer,則不會包含範例,因此您需要複製 git 儲存庫或使用上面的連結直接取得範例。
完整產生的 API 文件可線上取得。
您可以透過在頂級資料夾中執行phpdoc
來產生完整的 API 級文檔,並且文檔將出現在docs
資料夾中,但您需要安裝 PHPDocumentor。您可能會發現單元測試對於如何執行加密等各種操作是一個很好的參考。
如果文件未涵蓋您需要的內容,請在 Stack Overflow 上搜尋許多問題,並且在提出有關「SMTP 錯誤:無法連接到 SMTP 主機」的問題之前,請閱讀故障排除指南。
PHPMailer 測試使用 PHPUnit 9,並附帶一個 polyfill,可以讓 9 樣式的測試在較舊的 PHPUnit 和 PHP 版本上執行。
如果這沒有通過,您可以做些什麼來提供幫助嗎?
請負責任地揭露發現的任何漏洞—私下向維護人員報告安全問題。
請參閱 GitHub 上的 SECURITY 和 PHPMailer 的安全建議。
請向 GitHub 問題追蹤器提交錯誤報告、建議和拉取請求。
我們對修復邊緣情況、擴大測試覆蓋範圍和更新翻譯特別感興趣。
如果您在文件中發現錯誤,或想要新增某些內容,請繼續修改 wiki - 任何人都可以編輯它。
如果您在轉移到 PHPMailer GitHub 組織之前擁有 git 克隆,則需要在克隆中使用以下命令來更新引用舊 GitHub 位置的所有遠端 URL:
git remote set-url upstream https://github.com/PHPMailer/PHPMailer.git
請不要再使用 SourceForge 或 Google Code 專案;它們已經過時並且不再維護。
PHPMailer 的開發時間和資源由 Smartmessages.net 提供,Smartmessages.net 是世界上唯一的隱私第一電子郵件行銷系統。
我們非常歡迎捐款,無論是啤酒、T 卹或現金。透過 GitHub 贊助是向 PHPMailer 的維護者和貢獻者說「謝謝」的一種簡單便捷的方式 - 只需單擊專案頁面上的「贊助」按鈕即可。如果您的公司使用 PHPMailer,請考慮參加 Tidelift 的企業支援計畫。
作為 Tidelift 訂閱的一部分提供。
PHPMailer 和數千個其他軟體包的維護者正在與 Tidelift 合作,為您用來建立應用程式的開源軟體包提供商業支援和維護。節省時間、降低風險並改善程式碼運作狀況,同時向您使用的確切軟體包的維護人員付費。了解更多。
請參閱變更日誌。
coolbru
)和 Andy Prevost( codeworxtech
)於 2004 年接管了該專案。