配置 PHPMailer 以使用 Off ice365 SMTP
P粉510127741
P粉510127741 2023-10-16 10:39:47
0
2
471

我正在尝试设置 PHPMailer,以便我们的一位客户能够从他们自己的帐户中自动生成电子邮件。我登录了他们的Of  fice 365帐户,发现PHPMailer所需的设置是:

Host: smtp.off   ice365.com
Port: 587
Auth: tls

我已将这些设置应用于 PHPMailer,但是没有发送电子邮件(我调用的函数适用于我们自己的邮件,该邮件是从外部服务器(不是服务网页的服务器)发送的)。

"host"      => "smtp.of   fice365.com",
"port"      => 587,
"auth"      => true,
"secure"    => "tls",
"username"  => "clientemail@off  ice365.com",
"password"  => "clientpass",
"to"        => "myemail",
"from"      => "clientemail@of  fice365.com",
"fromname"  => "clientname",
"subject"   => $subject,
"body"      => $body,
"altbody"   => $body,
"message"   => "",
"debug"     => false

有谁知道PHPMailer通过smtp.offi   ce365.com发送需要什么设置?

P粉510127741
P粉510127741

全部回复(1)
P粉176151589

更新:2022 年 5 月

所以我在这个问题上苦苦挣扎。对于具有 Exchange Online 和 Microsoft 管理中心访问权限的企业帐户,我可以提供此问题的答案。

TLDR:转到管理中心并选择您要发送邮件的用户。然后在设置“经过身份验证的 SMTP”之后查看“电子邮件”和“电子邮件应用程序”后的设置,只需启用它即可。

仍然无法工作?我已经为您提供了帮助,这就是我如何让它完全发挥作用。

  1. 使用PHP Composer,实际上节省了很多工作。
  2. 将您的代码替换为我的代码,并在测试后更改
SMTPDebug = SMTP::DEBUG_off;

//SMTP
$mail = new PHPMailer(true); //important
$mail->CharSet = 'UTF-8';  //not important
$mail->isSMTP(); //important
$mail->Host = 'smtp.office365.com'; //important
$mail->Port       = 587; //important
$mail->SMTPSecure = 'tls'; //important
$mail->SMTPAuth   = true; //important, your IP get banned if not using this

//Auth
$mail->Username = 'yourname@mail.org';
$mail->Password = 'your APP password';//Steps mentioned in last are to create App password

//Set who the message is to be sent from, you need permission to that email as 'send as'
$mail->SetFrom('hosting@mail.org', 'Hosting Group Inc.'); //you need "send to" permission on that account, if dont use yourname@mail.org

//Set an alternative reply-to address
$mail->addReplyTo('no-reply@mail.com', 'First Last');

//Set who the message is to be sent to
$mail->addAddress('customer@othermail.com', 'SIMON MÜLLER');
//Set the subject line
$mail->Subject = 'PHPMailer SMTP test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('replace-with-file.html'), __DIR__);  //you can also use $mail->Body = "This is a body message in html" 
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//$mail->addAttachment('../../../images/phpmailer_mini.png');

//send the message, check for errors
if (!$mail->send()) {
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
}
  1. 这可能看起来像您的文件,没关系,但现在是简单棘手的部分。与 Google 一样,Microsoft 也为 SMTP 实现了“开关”。只需从您的企业帐户转到您的管理中心,或者请有权限的人执行该部分操作即可:
  • 导航至https://admin.microsoft.com/AdminPortal/Home#/users
  • 选择您要从中发送电子邮件的用户
  • 在“电子邮件”标签下搜索“电子邮件应用程序”,点击“管理电子邮件应用程序”
  • 您可以在此处选择“经过身份验证的 SMTP”,确保选中该选项并保存更改
  1. 如果您使用 MFA,请确保使用 https://stackoverflow.com/ 中提到的应用密码a/61359150/14148981

  2. 运行脚本

我希望这对某人有帮助。我花了很长时间才找到这个选项。

获取应用密码和身份验证的所有步骤摘要:

  1. 使用管理员帐户:
  • Active Directory AD -> 属性 -> 安全默认值:关闭。
  • Active Directory 门户 -> 条件访问 -> 配置 MFA 可信 IP -> 允许用户应用程序密码:启用
  • 管理页面用户列表 -> 目标用户的“多重身份验证”:启用然后强制
  • 管理页面用户列表 -> 用户详细信息 -> 邮件 -> “管理电子邮件应用程序” -> “经过身份验证的 SMTP”:启用
  1. 使用用户帐户:
  • 用户帐户配置文件 -> 安全 -> 添加登录方法:应用密码
  1. PHP 邮件程序设置:
  • smtp.office365.com、587、tls、电子邮件、appPassword
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!