简体   繁体   中英

Send email from gmail using Swiftmailer

I'm trying to send an email from Gmail using Swiftmailer and it print this error:

Fatal error: Uncaught exception 'Swift_TransportException' with message 'Failed to authenticate on SMTP server with username "username@gmail.com" using 2 possible authenticators' in /home/websitename/public_html/cv/Swift-5.1.0/lib/classes/Swift/Transport/Esmtp/AuthHandler.php:184 Stack trace: #0 /home/websitename/public_html/cv/Swift-5.1.0/lib/classes/Swift/Transport/EsmtpTransport.php(312): Swift_Transport_Esmtp_AuthHandler->afterEhlo(Object(Swift_SmtpTransport)) #1 /home/websitename/public_html/cv/Swift-5.1.0/lib/classes/Swift/Transport/AbstractSmtpTransport.php(120): Swift_Transport_EsmtpTransport->_doHeloCommand() #2 /home/websitename/public_html/cv/Swift-5.1.0/lib/classes/Swift/Mailer.php(80): Swift_Transport_AbstractSmtpTransport->start() #3 /home/websitename/public_html/cv/profile2.php(69): Swift_Mailer->send(Object(Swift_Message)) #4 {main} thrown in /home/websitename/public_html/cv/Swift-5.1.0/lib/classes/Swift/Transport/Esmtp/AuthHandler.php on line 184

My PHP code:

require_once 'Swift-5.1.0/lib/swift_required.php';
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
      ->setUsername('username@gmail.com')
      ->setPassword('password');

    $mailer = Swift_Mailer::newInstance($transport);
    $message = Swift_Message::newInstance('Wonderful Subject')
      ->setFrom(array('username@gmail.com' => 'Ahmad Shadeed'))
      ->setTo(array('receiver@gmail.com' => 'YOU'))
      ->setBody('This is the text of the mail send by Swift using SMTP transport.');
    $numSent = $mailer->send($message); 

Anyhelp to solve thie issue please?

Thanks,

go to

https://www.google.com/settings/security/lesssecureapps

while log in to SMPT configured gmail account

here

->setUsername('username@gmail.com')
->setPassword('password');

And enable "Access for less secure apps"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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