我已經(jīng)在幾個(gè)項(xiàng)目中使用了 PHPMailer,但現(xiàn)在我陷入了困境。它給了我錯(cuò)誤:
SMTP 錯(cuò)誤:無法連接到 SMTP 主機(jī)。
我嘗試過從 Thunderbird 發(fā)送電子郵件,它有效!但不是通過 PHPMailer ...以下是 Thunderbird 的設(shè)置:
服務(wù)器名稱:mail.exampleserver.com
端口:587
用戶名:user@exampleserver.com
安全身份驗(yàn)證:否
連接安全:STARTTLS
我將它們與我上一個(gè)使用 PHPMailer 的項(xiàng)目中的服務(wù)器進(jìn)行了比較,它們是:
服務(wù)器名稱: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è)問題在 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í)解決方案!