shadeed9
shadeed9

Reputation: 1826

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 "[email protected]" 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('[email protected]')
      ->setPassword('password');

    $mailer = Swift_Mailer::newInstance($transport);
    $message = Swift_Message::newInstance('Wonderful Subject')
      ->setFrom(array('[email protected]' => 'Ahmad Shadeed'))
      ->setTo(array('[email protected]' => '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,

Upvotes: 3

Views: 5238

Answers (1)

e03050
e03050

Reputation: 1542

go to

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

while log in to SMPT configured gmail account

here

->setUsername('[email protected]')
->setPassword('password');

And enable "Access for less secure apps"

Upvotes: 6

Related Questions