How can I send mail with PHPMailer without enabling 'less secure app' access in Yahoo?

1.1k views Asked by At

I'm trying to make a PHP contact form and send the messages to several addresses. I'm sending it from the default address, my own, which is a Yahoo address, and it's SMTP server, with PHPMailer.

I'm using a host with PHP 5.6. I've used the most update settings in a sample script:

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 = '[email protected]';
$mail->Password = 'my_password';

$mail->setFrom('[email protected]', 'From Íñtërnâtiônàl form');
$mail->addAddress('[email protected]', 'ラーメン丸 (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");

It does output this:

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

Obviously, it fails with an authentication error, despite being well authenticated, with a right username and password.

If I enable the "less secure apps" in Yahoo Account Security, then it does work correctly, showing:

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

Well, what do I need to do to send mail without having to enable 'less secure apps'?

In Gmail there are plenty of tutorials to do it with OAuth2 and creating an app key. Does it work in the same way for Yahoo? If it is like that, what do I need to do to use PHPMailerOAuth? Where do I apply for the key? And which key is it; Flurry, YQL, Gemini?

1

There are 1 answers

0
Synchro On

In PHPMailer 5.2, only gmail is supported. In PHPMailer 6.0, many others are supported. 6.0 is not yet released, but it will be soon, so give it a try.