php 使用 Office365 SMTP 设置 PHPMailer

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24947434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:33:56  来源:igfitidea点击:

Setting up PHPMailer with Office365 SMTP

phpemailsslphpmaileroffice365

提问by JosephGarrone

I am attempting to set up PHPMailer so that one of our clients is able to have the automatically generated emails come from their own account. I have logged into their Office 365 account, and found that the required settings for PHPMailer are:

我正在尝试设置 PHPMailer,以便我们的一位客户能够让自动生成的电子邮件来自他们自己的帐户。我登录了他们的Office 365账号,发现PHPMailer需要的设置是:

Host: smtp.office365.com
Port: 587
Auth: tls

I have applied these settings to PHPMailer, however no email gets sent (The function I call works fine for our own mail, which is sent from an external server (Not the server serving the web pages)).

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

"host"      => "smtp.office365.com",
"port"      => 587,
"auth"      => true,
"secure"    => "tls",
"username"  => "[email protected]",
"password"  => "clientpass",
"to"        => "myemail",
"from"      => "[email protected]",
"fromname"  => "clientname",
"subject"   => $subject,
"body"      => $body,
"altbody"   => $body,
"message"   => "",
"debug"     => false

Does anyone know what settings are required to get PHPMailer to send via smtp.office365.com?

有谁知道让 PHPMailer 通过 smtp.office365.com 发送需要哪些设置?

回答by bgazzera

@nitin's code was not working for me, as it was missing 'tls' in the SMTPSecure param.

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

Here is a working version. I've also added two commented out lines, which you can use in case something is not working.

这是一个工作版本。我还添加了两行注释掉的行,您可以在出现问题时使用它们。

<?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 = '[email protected]';
$mail->Password = 'YourPassword';
$mail->SetFrom('[email protected]', 'FromEmail');
$mail->addAddress('[email protected]', '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';
}

回答by Nitin Muchhadiya

Try this, it works fine for me, i have been using this for so long

试试这个,它对我来说很好用,我用了这么久

$mail = new PHPMailer(true);
$mail->Host = "smtp.office365.com";
$mail->Port       = 587;
$mail->SMTPSecure = '';
$mail->SMTPAuth   = true;
$mail->Username = "email";   
$mail->Password = "password";
$mail->SetFrom('email', 'Name');
$mail->addReplyTo('email', 'Name');
$mail->SMTPDebug  = 2;
$mail->IsHTML(true);
$mail->MsgHTML($message);
$mail->Send();

回答by Martin Stevens

I had the same issue when we moved from Gmail to Office365.

当我们从 Gmail 迁移到 Office365 时,我遇到了同样的问题。

You MUST set up a connector first (either an open SMTP relay or Client Send). Read this and it will tell you everything you need to know about allowing Office365 to send email:

您必须首先设置连接器(开放的 SMTP 中继或客户端发送)。阅读本文,它将告诉您有关允许 Office365 发送电子邮件的所有信息:

https://technet.microsoft.com/en-us/library/dn554323.aspx

https://technet.microsoft.com/en-us/library/dn554323.aspx

回答by Logan Wayne

UPDATE: April 2020

更新:2020 年 4 月

Using the accepted answer for sending email using Office 365has the high chance of not working since Microsoft is pushing for their Microsoft Graph(the only supported PHP framework right now is Laravel). If fortunately you were still able to make it work in your application, email will either go to the recipient's Junk, Trash, or Spam folder, which you don't want to happen.

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

Common errors I encountered were:

我遇到的常见错误是:

Failed to authenticate password. // REALLY FRUSTRATED WITH THIS ERROR! WHY IS MY PASSWORD WRONG?!

or

或者

Failed to send AUTH LOGIN command.

or

或者

Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.

In order to still make it work with the accepted answer, we just have to change a single line, which is the Password parameter line:

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

$mail->Password = 'YourOffice365Password';

Instead of setting the password with the one you use when you login to your Office365 account, you have to use an App Passwordinstead.

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



Create App Password

创建应用密码

  • First, in order to create an App Password, the Multi-Factor Authenticationof your Office 365 account should be enabled (you may have to contact your administrator for this to be enabled).

  • After that, login your Office 365 in your favorite browser

  • Go to My Accountpage (you will see the link to this page when you click your name's initials on the upper right)
  • Choose Security & Privacythen Additional security verification
  • At the top of the page, choose App Passwords
  • Choose createto get an app password
  • If prompted, type a name for your app password, and click Next
  • You will then see the password generated by Office 365 as your App Password
  • Copy the password
  • 首先,为了创建应用程序密码,应启用 Office 365 帐户的多重身份验证(您可能必须联系您的管理员才能启用此功能)。

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

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

After copying the password, go back to your working code and replace the Password parameter with the copied password. Your application should now be able to properly send email using Office 365.

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



Reference:

参考:

Create an app password for Microsoft 365

为 Microsoft 365 创建应用密码

回答by Koushik Mondal

You need an App Password and multi-factor authentication enabled to able to send mails from third party applications using office365 stmp. Google it for further information

您需要启用应用程序密码和多重身份验证才能使用 office365 stmp 从第三方应用程序发送邮件。谷歌它以获取更多信息