如何在不启用雅虎“安全性较低的应用”权限的情况下使用PHPMailer发送邮件?

时间:2017-01-06 22:05:28

标签: authentication smtp phpmailer yahoo

我正在尝试创建一个PHP联系表单并将消息发送到多个地址。 我从默认地址发送它,我自己的,这是一个雅虎地址,它是SMTP服务器,PHPMailer

我正在使用PHP 5.6的主机。我在示例脚本中使用了最多更新设置:

session_start();
date_default_timezone_set('Etc/UTC');
require_once('../phpmailer/PHPMailerAutoload.php');

echo '<pre>';
$mail = new PHPMailer;
$mail->CharSet = 'UTF-8';
$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = 'smtp.mail.yahoo.com';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'my_mail@yahoo.com';
$mail->Password = 'my_password';

$mail->setFrom('my_mail@yahoo.com', 'From Íñtërnâtiônàl form');
$mail->addAddress('target@gmail.com', 'ラーメン丸 (ramen-maru)');
$mail->isHTML(true);    
$mail->Subject = '山本's demo mail (yamamoto)';
$mail->Body    = 'Delicious HTML message with extra umami <em>うま味</em>';
$mail->AltBody = 'Savoury plain text note with extra umami うま味, too.';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}
echo '</pre>';
die("ok\n");

确实输出了这个:

2017-01-06 21:25:35 Connection: opening to smtp.mail.yahoo.com:587, timeout=300, options=array (
)
2017-01-06 21:25:35 Connection: opened 
2017-01-06 21:25:35 SERVER -> CLIENT: 220 smtp.mail.yahoo.com ESMTP ready 
2017-01-06 21:25:35 CLIENT -> SERVER: EHLO myhosting.com
2017-01-06 21:25:35 SERVER -> CLIENT: 250-smtp.mail.yahoo.com
                                      250-PIPELINING
                                      250-SIZE 41697280
                                      250-8 BITMIME
                                      250 STARTTLS
2017-01-06 21:25:35 CLIENT -> SERVER: STARTTLS
2017-01-06 21:25:35 SERVER -> CLIENT: 220 2.0.0 Start TLS 
2017-01-06 21:25:36 CLIENT -> SERVER: EHLO myhosting.com
2017-01-06 21:25:36 SERVER -> CLIENT: 250-smtp.mail.yahoo.com
                                      250-PIPELINING
                                      250-SIZE 41697280
                                      250-8 BITMIME
                                      250 AUTH PLAIN LOGIN XOAUTH2 XYMCOOKIE
2017-01-06 21:25:36 SMTP Error: Could not authenticate.
2017-01-06 21:25:36 CLIENT -> SERVER: QUIT
2017-01-06 21:25:36 SERVER -> CLIENT: 221 2.0.0 Bye
2017-01-06 21:25:36 Connection: closed
2017-01-06 21:25:36 SMTP connect() failed.

Message could not be sent. Mailer Error: SMTP connect() failed.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

显然,尽管经过了良好的身份验证,它仍然存在身份验证错误,并且使用了正确的用户名和密码。

如果我在Yahoo Account Security中启用“安全性较低的应用”,那么它确实可以正常工作,显示:

2017-01-06 21:44:22 Connection: opening to smtp.mail.yahoo.com:587, timeout=300, options=array ()
2017-01-06 21:44:22 Connection: opened
2017-01-06 21:44:22 SERVER -> CLIENT: 220 smtp.mail.yahoo.com ESMTP ready
2017-01-06 21:44:22 CLIENT -> SERVER: EHLO myhosting.com
2017-01-06 21:44:22 SERVER -> CLIENT: 250-smtp.mail.yahoo.com
                                  250-PIPELINING
                                  250-SIZE 41697280
                                  250-8 BITMIME
                                  250 STARTTLS
2017-01-06 21:44:22 CLIENT -> SERVER: STARTTLS
2017-01-06 21:44:22 SERVER -> CLIENT: 220 2.0.0 Start TLS
2017-01-06 21:44:23 CLIENT -> SERVER: EHLO myhosting.com
2017-01-06 21:44:23 SERVER -> CLIENT: 250-smtp.mail.yahoo.com
                                  250-PIPELINING
                                  250-SIZE 41697280
                                  250-8 BITMIME
                                  250 AUTH PLAIN LOGIN XOAUTH2 XYMCOOKIE
2017-01-06 21:44:23 CLIENT -> SERVER: AUTH LOGIN
2017-01-06 21:44:23 SERVER -> CLIENT: 334 ************
2017-01-06 21:44:23 CLIENT -> SERVER: ********************************
2017-01-06 21:44:23 SERVER -> CLIENT: 334 ************
2017-01-06 21:44:23 CLIENT -> SERVER: ************
2017-01-06 21:44:23 SERVER -> CLIENT: 235 2.0.0 OK
2017-01-06 21:44:23 CLIENT -> SERVER: MAIL FROM:
2017-01-06 21:44:23 SERVER -> CLIENT: 250 OK , completed
2017-01-06 21:44:23 CLIENT -> SERVER: RCPT TO:
2017-01-06 21:44:23 SERVER -> CLIENT: 250 OK , completed
2017-01-06 21:44:23 CLIENT -> SERVER: DATA
2017-01-06 21:44:23 SERVER -> CLIENT: 354 Start Mail. End with CRLF.CRLF
2017-01-06 21:44:23 CLIENT -> SERVER: Date: Fri, 6 Jan 2017 21:44:22 +0000
2017-01-06 21:44:23 CLIENT -> SERVER: To: =?UTF-8?B?44Op44O844Oh44Oz5Li4IChyYW1lbi1tYXJ1KQ==?= 
2017-01-06 21:44:23 CLIENT -> SERVER: From: =?UTF-8?B?UGFyYSBJw7F0w6tybsOidGnDtG7DoGxpesOmdGnDuG4=?= 
2017-01-06 21:44:23 CLIENT -> SERVER: Subject: =?UTF-8?Q?=E5=B1=B1=E6=9C=AC_(yamamoto)_presenta:_II?=
2017-01-06 21:44:23 CLIENT -> SERVER: Message-ID: 
2017-01-06 21:44:23 CLIENT -> SERVER: X-Mailer: PHPMailer 5.2.21 (https://github.com/PHPMailer/PHPMailer)
2017-01-06 21:44:23 CLIENT -> SERVER: MIME-Version: 1.0
2017-01-06 21:44:23 CLIENT -> SERVER: Content-Type: multipart/alternative;
2017-01-06 21:44:23 CLIENT -> SERVER:   boundary="b1_ebc9bc65ff4eee7ae217e5d602991794"
2017-01-06 21:44:23 CLIENT -> SERVER: Content-Transfer-Encoding: 8bit
2017-01-06 21:44:23 CLIENT -> SERVER:
2017-01-06 21:44:23 CLIENT -> SERVER: This is a multi-part message in MIME format.
2017-01-06 21:44:23 CLIENT -> SERVER:
2017-01-06 21:44:23 CLIENT -> SERVER: --b1_ebc9bc65ff4eee7ae217e5d602991794
2017-01-06 21:44:23 CLIENT -> SERVER: Content-Type: text/plain; charset=UTF-8
2017-01-06 21:44:23 CLIENT -> SERVER: Content-Transfer-Encoding: 8bit
2017-01-06 21:44:23 CLIENT -> SERVER:
2017-01-06 21:44:23 CLIENT -> SERVER: Mensaje de prueba 2 con extra de umami うま味
2017-01-06 21:44:23 CLIENT -> SERVER:
2017-01-06 21:44:23 CLIENT -> SERVER:
2017-01-06 21:44:23 CLIENT -> SERVER: --b1_ebc9bc65ff4eee7ae217e5d602991794
2017-01-06 21:44:23 CLIENT -> SERVER: Content-Type: text/html; charset=UTF-8
2017-01-06 21:44:23 CLIENT -> SERVER: Content-Transfer-Encoding: 8bit
2017-01-06 21:44:23 CLIENT -> SERVER:
2017-01-06 21:44:23 CLIENT -> SERVER: Mensaje de prueba 2 con extra de umami うま味
2017-01-06 21:44:23 CLIENT -> SERVER:
2017-01-06 21:44:23 CLIENT -> SERVER:
2017-01-06 21:44:23 CLIENT -> SERVER:
2017-01-06 21:44:23 CLIENT -> SERVER: --b1_ebc9bc65ff4eee7ae217e5d602991794--
2017-01-06 21:44:23 CLIENT -> SERVER:
2017-01-06 21:44:23 CLIENT -> SERVER: .
2017-01-06 21:44:23 SERVER -> CLIENT: 250 OK , completed
2017-01-06 21:44:23 CLIENT -> SERVER: QUIT
2017-01-06 21:44:23 SERVER -> CLIENT: 221 Service Closing transmission
2017-01-06 21:44:23 Connection: closed
Message has been sent

那么,在不启用“不太安全的应用”的情况下,我需要做什么来发送邮件?

在Gmail中,有很多教程可以使用OAuth2和创建应用密钥。雅虎的工作方式是否相同? 如果是这样的话,我需要做什么来使用PHPMailerOAuth? 我在哪里申请钥匙?它是关键所在;乱舞,YQL,双子座?

1 个答案:

答案 0 :(得分:0)

在PHPMailer 5.2中,仅支持gmail。在PHPMailer 6.0中,支持许多其他功能。 6.0尚未发布,但很快就会发布,所以试一试。