《Disney +》 迪士尼、Marvel、彼思、星球大戰…  原神Ver.2.2預告節目將於10月3日「TGS 2021 ONLINE」播出!   《Horizon Forbidden West》的音樂:作曲家相見歡   多功能輕量級MMO遊戲滑鼠「Razer Naga X」將於日本上市   「SAMURAI SPIRITS」季票3的新增角色「查姆查姆」預告公開!下載日確定!   鬼滅之刃x實況野球合作 炭治郎變成可愛棒球手角色   《Heavenly Bodies》美術設計解析(12 月 7 日推出)   《Ratchet & Clank: Rift Apart》神祕新主角蕾薇特首度曝光   「Riot Games Store Japan」開店決定!「英雄聯盟」及「特戰英豪」官方周邊商品可以在日本國內購買! 

PHP 使用 PHPMailer 遇到 SSL operation failed 的解法

商業

使用 PHP 寄信經常會使用 PHPMailer,但是要使用內部網路的 Mail
Server 來寄信,卻一直遇到如下述的錯誤:

Warning: stream_socket_enable_crypto(): SSL operation failed with code 1.
OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

要怎麼解決呢?

PHP 使用 PHPMailer 遇到 SSL operation failed 的解法

PHPMailer 常見的寫法如下:(註:設定 From、To、Subject、Body.. 在此就不寫了)

isSMTP();
$mail->Host = 'relay-hosting.secureserver.net';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->SMTPSecure = 'tls';
?>

現在已經很習慣使用 SMTPSecure 為 tls 或 ssl,但是臨時自己架設的 Mail Server,沒有設定憑證、帳號、密碼,修改範例如下:

SMTPDebug = 2; // Enable verbose debug output (print debug message)

$mail->isSMTP();
$mail->Host = 'relay-hosting.secureserver.net';
$mail->Port = 25;
$mail->SMTPAuth = false;
$mail->SMTPSecure = false;
?>

將 SMTPAuth、SMTPSecure 都設定 false,發信卻還是發生「SSL operation failed」、「certificate verify failed」的問題,要怎麼解決呢?

PHPMailer 遇到問題時,官方網頁有疑難排解可以參考:Troubleshooting · PHPMailer/PHPMailer Wiki

  • 文章找到這個參數:$mail->SMTPAutoTLS = false;

原來現在 PHPMailer 已經預設啟用 TLS,所以程式修改如下:

SMTPDebug = 2;

$mail->isSMTP();
$mail->Host = 'relay-hosting.secureserver.net';
$mail->Port = 25;
$mail->SMTPAuth = false;
$mail->SMTPSecure = false;
$mail->SMTPAutoTLS = false;
?>

於是就可以使用自己架設的 Mail Server 正常寄信囉~

Tsung

隨機商業新聞

Disney+