《Disney +》 迪士尼、Marvel、彼思、星球大戰…  「PS VR亮點」挾精采更新回歸   「GUILTY GEAR -STRIVE-」初期角色的最後1人!「伊諾」決定參戰!   《Oddworld: Soulstorm》——結局多種,還能提高Quarmic分數   睽違19年!KOF XV 新角色宣傳影片「七枷 社」!   尊爵不凡!重現《勇者鬥惡龍 達伊的大冒險》大魔王巴恩的西洋棋軍隊「哈德拉禁衛騎團 銀製西洋棋組」發售確定!   《Among Us》預計在12月15日登陸PS4 / PS5   《Marvel’s Spider-Man 2》偕《Marvel’s Wolverine》一同曝光   觀看1月22日「Resident Evil Showcase」直播節目 

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

隨機商業新聞

Gearbest