[英]PHPmailer not connecting to office365 SMTP
我有一个使用 phpmailer 通过 office 365 smtp 发送电子邮件的脚本。 它在过去一年左右一直有效。 现在我在连接到服务器时遇到错误。
PHP Fatal error: Uncaught PHPMailer\PHPMailer\Exception: SMTP Error: Could not connect to SMTP host. in C:\inetpub\wwwroot\Parishes_staging\ChristTheKing\vendor\phpmailer\phpmailer\src\PHPMailer.php:1898
Stack trace:
#0 C:\inetpub\wwwroot\Parishes_staging\ChristTheKing\vendor\phpmailer\phpmailer\src\PHPMailer.php(1725): PHPMailer\PHPMailer\PHPMailer->smtpConnect(Array)
#1 C:\inetpub\wwwroot\Parishes_staging\ChristTheKing\vendor\phpmailer\phpmailer\src\PHPMailer.php(1481): PHPMailer\PHPMailer\PHPMailer->smtpSend('Date: Fri, 14 F...', 'This is a multi...')
#2 C:\inetpub\wwwroot\Parishes_staging\ChristTheKing\vendor\phpmailer\phpmailer\src\PHPMailer.php(1320): PHPMailer\PHPMailer\PHPMailer->postSend()
#3 C:\inetpub\wwwroot\Parishes_staging\ChristTheKing\submitform2.php(195): PHPMailer\PHPMailer\PHPMailer->send()
#4 {main}
thrown in C:\inetpub\wwwroot\Parishes_staging\ChristTheKing\vendor\phpmailer\phpmailer\src\PHPMailer.php on line 1898
脚本如下:
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.office365.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "user@company.org";
$mail->Password = "password";
$mail->SetFrom('sentfromemail@company.org', 'FromEmail');
//$mail->addAddress('user@company.org', 'ToEmail');
$mail->addAddress('user2@company.org', 'ToEmail');
$mail->addBCC('user2@company.org', 'ToBCCEmail');
$mail->SMTPDebug = 3;
$mail->Debugoutput = function($str, $level) {echo "debug level $level; message: $str";};
$mail->Debugoutput = 'echo';
$mail->IsHTML(true);
$mail->Subject = 'Parish Registration Form';
$mail->Body = $body;
$mail->AltBody = $body;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
else {
echo 'Message has been sent';
}
我在 Internet 上搜索了与 Office 365 关联的连接设置,但没有发现任何问题。 Microsoft 服务器上是否发生了我应该知道但不知道的更改?
你的结局没有任何问题。
检查您的防火墙是否阻止了端口 587。 如果不是,那么问题可能来自 SMTP 服务器端。 联系 Microsoft 支持。
结果证明是证书错误。 当 php.ini 中没有指定证书文件时,应该使用根证书。 但它没有被使用。
我必须添加以下代码才能使其正常工作:
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.