使用Office365 SMTP设置PHPMailer

Jos*_*one 10 php email ssl phpmailer office365

我正在尝试设置PHPMailer,以便我们的一个客户能够自动生成的电子邮件来自他们自己的帐户.我已登录到他们的Office 365帐户,发现PHPMailer所需的设置是:

Host: smtp.office365.com
Port: 587
Auth: tls
Run Code Online (Sandbox Code Playgroud)

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

"host"      => "smtp.office365.com",
"port"      => 587,
"auth"      => true,
"secure"    => "tls",
"username"  => "clientemail@office365.com",
"password"  => "clientpass",
"to"        => "myemail",
"from"      => "clientemail@office365.com",
"fromname"  => "clientname",
"subject"   => $subject,
"body"      => $body,
"altbody"   => $body,
"message"   => "",
"debug"     => false
Run Code Online (Sandbox Code Playgroud)

有谁知道让PHPMailer通过smtp.office365.com发送所需的设置?

小智 18

@ nitin的代码对我不起作用,因为它在SMTPSecure参数中缺少'tls'.

这是一个工作版本.我还添加了两个注释掉的行,您可以使用这些行以防万一.

<?php
require 'vendor/phpmailer/phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.office365.com';
$mail->Port       = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth   = true;
$mail->Username = 'somebody@somewhere.com';
$mail->Password = 'YourPassword';
$mail->SetFrom('somebody@somewhere.com', 'FromEmail');
$mail->addAddress('recipient@domain.com', 'ToEmail');
//$mail->SMTPDebug  = 3;
//$mail->Debugoutput = function($str, $level) {echo "debug level $level; message: $str";}; //$mail->Debugoutput = 'echo';
$mail->IsHTML(true);

$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';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}
Run Code Online (Sandbox Code Playgroud)

  • 嗨,这段代码不适合我.当我启用调试时,它说:"SMTP - >错误:服务器不接受密码:"我显然双重检查了凭据. (2认同)

Log*_*yne 8

更新:2020 年 4 月

使用已接受的答案使用Office 365发送电子邮件很有可能无法正常工作,因为 Microsoft 正在推动他们的Microsoft Graph(目前唯一支持的 PHP 框架是Laravel)。如果幸运的是您仍然能够在您的应用程序中使用它,电子邮件将转到收件人的垃圾邮件、垃圾邮件或垃圾邮件文件夹,这是您不希望发生的。

我遇到的常见错误是:

Failed to authenticate password. // REALLY FRUSTRATED WITH THIS ERROR! WHY IS MY PASSWORD WRONG?!
Run Code Online (Sandbox Code Playgroud)

或者

Failed to send AUTH LOGIN command.
Run Code Online (Sandbox Code Playgroud)

或者

Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.
Run Code Online (Sandbox Code Playgroud)

为了仍然使用已接受的答案,我们只需要更改一行,即 Password 参数行:

$mail->Password = 'YourOffice365Password';
Run Code Online (Sandbox Code Playgroud)

您不必使用登录 Office365 帐户时使用的密码来设置密码,而是必须使用应用程序密码


创建应用密码

  • 首先,为了创建应用程序密码,应启用 Office 365 帐户的多重身份验证(您可能必须联系您的管理员才能启用此功能)。

  • 之后,在您喜欢的浏览器中登录您的 Office 365

  • 转到“我的帐户”页面(当您单击右上角的姓名首字母缩写时,您将看到此页面的链接)
  • 选择安全和隐私,然后选择附加安全验证
  • 在页面顶部,选择应用密码
  • 选择创建以获取应用密码
  • 如果出现提示,请键入应用密码的名称,然后单击下一步
  • 然后您将看到 Office 365 生成的密码作为您的应用程序密码
  • 复制密码

复制密码后,返回您的工作代码并将密码参数替换为复制的密码。您的应用程序现在应该能够使用 Office 365 正确发送电子邮件。


参考:

为 Microsoft 365 创建应用密码


小智 6

更新:2020 年 8 月

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

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

还是行不通?我让你知道了,这是我如何让它完全工作的。

  1. 使用PHP composer,实际上节省了很多工作。
  2. 用我的代码替换您的代码并在测试后更改它
<?php
//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer; //important, on php files with more php stuff move it to the top
use PHPMailer\PHPMailer\SMTP; //important, on php files with more php stuff move it to the top

//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');

require 'path/to/vendor/autoload.php'; //important

//Enable SMTP debugging
// SMTP::DEBUG_OFF = off (for production use)
// SMTP::DEBUG_CLIENT = client messages
// SMTP::DEBUG_SERVER = client and server messages
//$mail->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 = 'yourpassword';

//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 = "</p>This is a <b>body</b> message in html</p>" 
//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 {
}
Run Code Online (Sandbox Code Playgroud)
  1. 这可能看起来像你的文件,没关系,但现在是容易棘手的部分。与 Google 一样,Microsoft 为 SMTP 内容实施了“开关”。只需从您的企业帐户转到您的管理中心,或请有权限的人执行该部分:
  • 导航到https://admin.microsoft.com/AdminPortal/Home#/users
  • 选择要发送电子邮件的用户
  • 在“电子邮件”选项卡下搜索“电子邮件应用程序”,单击“管理电子邮件应用程序”
  • 在这里您可以选择“经过身份验证的 SMTP”,确保选中该选项并保存更改
  1. 如果您使用 MFA,请确保使用/sf/answers/4295140531/ 中提到的应用程序密码

  2. 运行脚本

我希望这可以帮助别人。我花了很长时间才找到这个选项。

  • 如果您通过 GoDaddy 使用 Office 365,则没有 Microsoft“管理”页面,但有一个用于管理此设置的 GoDaddy 页面。有关如何打开或关闭它的说明可以在此处找到:[启用 SMTP 身份验证](https://nl.godaddy.com/help/enable-smtp-authentication-40981?lc=en-US) (2认同)