美好的一天,
我的phpmailer smtp配置错误到了smtp.office365.com
这里是我的剧本
require __DIR__ .'/vendor/phpmailer/phpmailer/src/Exception.php';
require __DIR__ .'/vendor/phpmailer/phpmailer/src/PHPMailer.php';
require __DIR__ .'/vendor/phpmailer/phpmailer/src/SMTP.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 4; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = gethostbyname('smtp.office365.com'); // Specify main and backup SMTP servers
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'office365 username'; // SMTP username
$mail->Password = 'office365 password'; // SMTP password
$mail->SMTPOptions = array (
'ssl' => array(
// 'verify_peer' => false,
// 'verify_peer_name' => false,
// 'allow_self_signed' => true
));
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->From = $mail->Username;
$mail->addAddress('recipient@gmail.com'); // Name is optional
//Attachments
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
我收到了这个错误
stream_socket_enable_crypto(): Peer certificate CN=`servername.com' did not match expected CN=`smtp.office365.com'
如果我取消注释
$mail->SMTPOptions = array (
'ssl' => array(
// 'verify_peer' => false,
'peer_name' => 'smtp.office365.com',
// 'verify_peer_name' => false,
// 'allow_self_signed' => true
));
我仍然认证失败..
由于有人正在进行服务器设置,我对服务器配置并不是很好。但我的网站是https。
答案 0 :(得分:1)
想想这意味着什么。您要求连接到命名主机,但证书上的名称不匹配。这意味着服务器配置错误(Office365不太可能),或者您被重定向到使用其他名称的其他服务器。这极有可能,因为它在大型托管服务提供商中非常常见。如果您将SMTPDebug = 2
设置为错误消息链接到建议的故障排除指南,则会显示所有内容。
发生这种情况是好的事情 - 这是使用TLS的主要原因之一 - 它不仅加密了您的传输流量,还确保您连接的服务器是一个你期望的,即它正在正常工作。