doulin1867 2017-06-06 18:56
浏览 333

如何使用PHPMailer发送带有smtp.office365的电子邮件

I'm using code below to send mail but I get Authentication unsuccessful. The same code works with smtp.gmail.com, but not with smtp.office365.com.

<?php
/**
 * This example shows settings to use when sending via Google's Gmail servers.
 */

//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 '../PHPMailerAutoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer;

//Tell PHPMailer to use SMTP
$mail->isSMTP();

//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 4;

//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';

//Set the hostname of the mail server
$mail->Host = 'tls://smtp.office365.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6

//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;

//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "anonymized@anonymized.com.br";

//Password to use for SMTP authentication
$mail->Password = "anonymized";

//Set who the message is to be sent from
$mail->setFrom('anonymized@anonymized.com.br', 'First Last');

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

//Set who the message is to be sent to
$mail->addAddress('anonymized@anonymized.com.br', 'John Doe');

//Set the subject line
$mail->Subject = 'PHPMailer GMail 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('contents.html'), dirname(__FILE__));

//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 {
    echo "Message sent!";
}

I get the following output:

2017-06-06 18:24:34 SERVER -> CLIENT: 220 FR1PR80CA0087.outlook.office365.com Microsoft ESMTP MAIL Service ready at Tue, 6 Jun 2017 18:24:25 +0000
2017-06-06 18:24:34 CLIENT -> SERVER: EHLO desenv
2017-06-06 18:24:34 SERVER -> CLIENT: 250-FR1PR80CA0087.outlook.office365.com Hello [201.47.213.100]250-SIZE 157286400250-PIPELINING250-DSN250-ENHANCEDSTATUSCODES250-STARTTLS250-8BITMIME250-BINARYMIME250 CHUNKING
2017-06-06 18:24:34 CLIENT -> SERVER: STARTTLS
2017-06-06 18:24:34 SERVER -> CLIENT: 220 2.0.0 SMTP server ready
2017-06-06 18:24:35 CLIENT -> SERVER: EHLO desenv
2017-06-06 18:24:35 SERVER -> CLIENT: 250-FR1PR80CA0087.outlook.office365.com Hello [201.47.213.100]250-SIZE 157286400250-PIPELINING250-DSN250-ENHANCEDSTATUSCODES250-AUTH LOGIN250-8BITMIME250-BINARYMIME250 CHUNKING
2017-06-06 18:24:35 CLIENT -> SERVER: AUTH LOGIN
2017-06-06 18:24:35 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2017-06-06 18:24:35 CLIENT -> SERVER: xxx
2017-06-06 18:24:35 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2017-06-06 18:24:35 CLIENT -> SERVER: xxx
2017-06-06 18:24:40 SERVER -> CLIENT: 535 5.7.3 Authentication unsuccessful [FR1PR80CA0087.lamprd80.prod.outlook.com]
2017-06-06 18:24:40 SMTP ERROR: Password command failed: 535 5.7.3 Authentication unsuccessful [FR1PR80CA0087.lamprd80.prod.outlook.com]
SMTP Error: Could not authenticate.
2017-06-06 18:24:40 CLIENT -> SERVER: QUIT
2017-06-06 18:24:40 SERVER -> CLIENT: 221 2.0.0 Service closing transmission channel
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

I tried many things. Changing file encoding because in this question smtplib.SMTPAuthenticationError: (535, '5.7.3 Authentication unsuccessful') the problem was the encoding of the password.

I tried using this, as in PHPMailer Authentication unsuccessful:

$mail->SMTPAuth = False;

But I got:

2017-06-06 18:28:59 SERVER -> CLIENT: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [RO1P215CA0017.LAMP215.PROD.OUTLOOK.COM]
2017-06-06 18:28:59 SMTP ERROR: MAIL FROM command failed: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [RO1P215CA0017.LAMP215.PROD.OUTLOOK.COM]
The following From address failed: rrjuridico@grupopetropolis.com.br : MAIL FROM command failed,5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [RO1P215CA0017.LAMP215.PROD.OUTLOOK.COM],530,SMTP server error: MAIL FROM command failed Detail: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [RO1P215CA0017.LAMP215.PROD.OUTLOOK.COM] SMTP code: 530
Mailer Error: The following From address failed: rrjuridico@grupopetropolis.com.br : MAIL FROM command failed,5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [RO1P215CA0017.LAMP215.PROD.OUTLOOK.COM] ,530,SMTP server error: MAIL FROM command failed Detail: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [RO1P215CA0017.LAMP215.PROD.OUTLOOK.COM] SMTP code: 530SMTP server error: MAIL FROM command failed Detail: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [RO1P215CA0017.LAMP215.PROD.OUTLOOK.COM] SMTP code: 5302017-06-06 18:28:59 CLIENT -> SERVER: QUIT
2017-06-06 18:28:59 SERVER -> CLIENT:
2017-06-06 18:28:59 SMTP ERROR: QUIT command failed: 

I tried removing line below:

$mail->isSMTP();

I got Message sent!, but email wasn't sent.

Any Idea, any help would be very much apreciated.

  • 写回答

1条回答 默认 最新

  • douji1077 2017-06-06 20:32
    关注

    It just looks like you have the wrong ID and password. They were easily decoded from the SMTP transcript (I have removed them - you might want to change your PW now), but your password only contained ASCII characters, so it's not an encoding problem.

    You do need to authenticate in order to send on most systems, so your second example makes sense.

    Not calling isSMTP() means that it defaults to using mail(), which relays your message via a local mail server; the local delivery may succeed, but the onward delivery many still fail - this matches what you're seeing. To check that, look in your mail server log (usually /var/log/mail.log or nearby).

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog