我正在尝试设置PHPMailer,以便我们的一个客户能够自动生成的电子邮件来自他们自己的帐户。我已登录到他们的Office 365帐户,并发现PHPMailer所需的设置为:
Host: smtp.office365.com
Port: 587
Auth: tls
我已将这些设置应用于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
有谁知道让PHPMailer通过smtp.office365.com发送所需的设置?
答案 0 :(得分:13)
@ nitin的代码对我不起作用,因为它缺少了&#t; ts'在SMTPSecure参数中。
这是一个工作版本。我还添加了两条已注释掉的行,您可以使用这些行以防万一。
<?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';
}
答案 1 :(得分:7)
使用接受的答案通过 Office 365 发送电子邮件很有可能无法正常工作,因为Microsoft正在推动其 Microsoft Graph (目前唯一受支持的PHP框架)是 Laravel )。幸运的是,如果您仍然能够在应用程序中使用它,则电子邮件将转到收件人的“垃圾邮件”,“垃圾箱”或“垃圾邮件”文件夹中。
我遇到的常见错误是:
Failed to authenticate password. // REALLY FRUSTRATED WITH THIS ERROR! WHY IS MY PASSWORD WRONG?!
或
Failed to send AUTH LOGIN command.
或
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.
为了仍能使它与已接受的答案一起使用,我们只需更改一行,即Password参数行:
$mail->Password = 'YourOffice365Password';
您不必使用登录Office365帐户时使用的密码来设置密码,而必须使用应用密码。
首先,为了创建应用密码,应启用Office 365帐户的Multi-Factor Authentication(您可能必须与管理员联系才能启用此功能) )。
然后,在您喜欢的浏览器中登录Office 365
复制密码后,返回工作代码,并用复制的密码替换Password参数。您的应用程序现在应该能够使用Office 365正确发送电子邮件。
参考:
答案 2 :(得分:4)
试试这个,它适用于我,我已经使用了这么久
-
答案 3 :(得分:4)
所以我真的很努力地解决这个问题。对于具有Exchange Online的企业帐户并访问Microsoft Admin Center,我可以为此提供答案。
TLDR:转到管理中心,然后选择要发送邮件的用户。然后在“身份验证的SMTP”设置之后查看“电子邮件”和“电子邮件应用程序”之后的设置,只需启用它即可。
仍然无法正常工作吗?我被您覆盖了,这就是我如何使其完全正常工作的方式。
<?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 {
}
如果您使用MFA,请确保使用https://stackoverflow.com/a/61359150/14148981
中所述的应用密码运行脚本
我希望这对某人有帮助。很久以来,我一直无法在自己身上找到此选项。
答案 4 :(得分:2)
当我们从Gmail迁移到Office365时,我遇到了同样的问题。
您必须先设置连接器(打开SMTP中继或客户端发送)。阅读本文,它将告诉您有关允许Office365发送电子邮件的所有信息:
答案 5 :(得分:0)
您需要启用应用密码和多重身份验证,才能使用office365 stmp从第三方应用程序发送邮件。谷歌获取更多信息
答案 6 :(得分:0)
更新:2020年12月
如果Security info上没有“应用密码”选项,请尝试以下操作: