SMTP Error: Could not connect to SMTP host. Connection failed. stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error
2024-12-06
PHPMailer 要透過 AWS SES 發信時,出現錯誤訊息:
SMTP Error: Could not connect to SMTP host.
Connection failed. stream_socket_enable_crypto(): SSL operation failed with code 1.
OpenSSL Error messages:error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed
快速解法:
參考 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting#certificate-verification-failure
1.
下載 https://curl.haxx.se/ca/cacert.pem
放到 /etc/ssl
(相關資訊: CA certificates extracted from Mozilla )
2.
/etc/php.ini 新增兩行設定:
openssl.cafile = /etc/ssl/cacert.pem
curl.cainfo = /etc/ssl/cacert.pem
重啟 php-fpm 即可
SES 範例程式
<?php
date_default_timezone_set("Asia/Taipei");
#維吉尼亞Virginia
$ses_server = "email-smtp.us-east-1.amazonaws.com";
$ses_port = 587;
$ses_username = "AKIAYYIC6RQ....";
$ses_password = "BKJOjEBC5SvBWyQPAX....";
$FROM="[email protected]";
$FROM_NAME="myname";
$MAILTO ='[email protected]';
$MSUBJECT = "EMAIL SUBJECT";
$MAILBODY = "EMAIL TEST";
#----------------------------------------------------------------
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
$phpmail = new PHPMailer();
$phpmail->SMTPAuth = true;
$phpmail->SMTPSecure = "tls";
$phpmail->Host = $ses_server;
$phpmail->Port = $ses_port;
$phpmail->Username = $ses_username;
$phpmail->Password = $ses_password;
$phpmail->IsSMTP(true);
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$phpmail->SMTPDebug = 2;
#$phpmail->IsHTML(true);
$phpmail->SMTPKeepAlive = true;
$phpmail->CharSet = "utf-8";
$phpmail->Subject = $MSUBJECT;
$phpmail->MsgHTML($MAILBODY);
$phpmail->SetFrom ($FROM, $FROM_NAME);
$phpmail->AddReplyTo($FROM, $FROM_NAME);
$phpmail->AddAddress($MAILTO, '');
echo "to: $MAILTO \n";
if(!$phpmail->Send()) {
echo " Mailer Error: " . $phpmail->ErrorInfo;
} else {
echo " Message sent!\n\n";
}
$phpmail->ClearAllRecipients();
其它參考:
留言
相關文章