简体   繁体   English

如何使用 PHPMailer 通过 smtp.office365 发送电子邮件

[英]How to send email with smtp.office365 using PHPMailer

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.相同的代码适用于 smtp.gmail.com,但不适用于 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.更改文件编码,因为在这个问题smtplib.SMTPAuthenticationError: (535, '5.7.3 Authentication unsuccessful')问题是密码的编码。

I tried using this, as in PHPMailer Authentication unsuccessful :我尝试使用它,如PHPMailer 身份验证不成功

$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.任何想法,任何帮助都会非常感激。

It just looks like you have the wrong ID and password.看起来您的 ID 和密码错误。 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.它们很容易从 SMTP 抄本中解码(我已经删除了它们 - 您现在可能想要更改您的密码),但是您的密码仅包含 ASCII 字符,因此这不是编码问题。

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;不调用isSMTP()意味着它默认使用mail() ,它通过本地邮件服务器中继您的消息; 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).要检查这一点,请查看您的邮件服务器日志(通常是/var/log/mail.log或附近)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM