phpmailer 发送Outlook邮件时535 5.7.3 authentication unsuccessful错误的解决办法
shadowfish phpmailer
在尝试使用我的office 365 E5开发者账号使用phpmailer进行发件时,被535 5.7.3 authentication unsuccessful这个错误困扰了很久,在国内外网站找了非常多的资料,可用的很少。这个错误很容易被理解为用户名密码错误,但是若十分确定用户名密码准确无误,极可能是微软默认开启的多重验证机制的原因!我们要做的就是把这个多重验证机制手动关掉。
# 亲测可用的方法:关闭安全默认值
用管理员身份登录Azure属性,点击最下面的"管理安全默认值",右边弹出"启用安全默认值",默认应为是,请改成否:!
问题解决.重试发件看看!
# 我没有尝试过的方法:保留多重验证,创建应用密码
这是来自Stack Overflow上一个热心老哥新提出来的方法.如果你需要在保留多重验证的同时允许其他应用如phpmailer访问smtp,可以尝试这种方法.
我对照微软给出的文档进行操作,发现在
登录到 "其他安全验证" 页,然后选择 "应用密码"。
这一步就遇到了问题,我并没有看到这个"应用密码"的选项.如果读者老哥知道这种方法怎么处理,欢迎在评论区指出.
# 附:phpmailer 进行 outLook的smtp发件的参考代码
我使用了Composer进行phpmailer的部署,如果你对此有疑问,可以参考phpmailer的Github主页指引.
代码中$mail->Username
和$mail->Password
的值应替换为你的用户名密码.
代码中
$mail->SMTPSecure = '';
$mail->Port = 25;
可以替换为
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
// Load Composer's autoloader
require 'vendor/autoload.php';
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'smtp.office365.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'yourusername@something.com'; // 带域名的完整用户名
$mail->Password = 'yourpassword'; //如果你选择第一种方法,这里就填你的登录密码,如果你选择第二种方法,这里就填你创建的应用密码
$mail->SMTPSecure = ''; // Enable TLS encryption;
$mail->Port = 25;
//Recipients
$mail->setFrom('shadowfish@shadowfish0.top', 'shadowfish');
$mail->addAddress('shadowfish@foxmail.com', 'shadowfish@foxmail.com'); // Add a recipient
// 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. Mailer Error: {$mail->ErrorInfo}";
}
?>