我已經(jīng)在幾個(gè)專案中使用了 PHPMailer,但現(xiàn)在我陷入了困境。它給了我錯(cuò)誤:
SMTP 錯(cuò)誤:無(wú)法連線到 SMTP 主機(jī)。
# 我嘗試過(guò)從 Thunderbird 發(fā)送電子郵件,它有效!但不是透過(guò) PHPMailer ...以下是 Thunderbird 的設(shè)定:
伺服器名稱:mail.exampleserver.com
連接埠:587
使用者名稱:user@exampleserver.com
安全性驗(yàn)證:否
連線安全性:STARTTLS
我將它們與我上一個(gè)使用 PHPMailer 的專案中的伺服器進(jìn)行了比較,它們是:
伺服器名稱:mail.exampleserver2.com
連接埠:465
使用者名稱:user@exampleserver2.com
安全性驗(yàn)證:否
連線安全性:SSL/TLS
我的 php 程式碼是:
$mail = new PHPMailer(); $mail->IsSMTP(); // send via SMTP $mail->Host = SMTP_HOST; // SMTP servers $mail->Port = SMTP_PORT; // SMTP servers $mail->SMTPAuth = true; // turn on SMTP authentication $mail->Username = SMTP_USER; // SMTP username $mail->Password = SMTP_PASSWORD; // SMTP password $mail->From = MAIL_SYSTEM; $mail->FromName = MAIL_SYSTEM_NAME; $mail->AddAddress($aSecuredGetRequest['email']); $mail->IsHTML(true); // send as HTML
我哪裡錯(cuò)了?
由於這個(gè)問(wèn)題在 google 中出現(xiàn)率很高,因此我想在這裡分享我針對(duì) PHP 剛剛升級(jí)到版本 5.6(具有更嚴(yán)格的 SSL 行為)的情況的解決方案。
PHPMailer wiki 有一個(gè)關(guān)於此的部分:
https://github.com/PHPMailer/ PHPMailer/wiki/Troubleshooting#php-56-certificate-verification-failure
建議的解決方法包括以下程式碼:
$mail->SMTPOptions = array( 'ssl' => array( 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true ) );
這應(yīng)該適用於 PHPMailer 5.2.10(及更高版本)。
注意:顯然,正如該 wiki 中所建議的,這應(yīng)該是一個(gè)臨時(shí)解決方案!