Using php’s swiftmailer with gmail

Don’t mean to resurrect an old post, but just in case others are looking for the answer, and because this post came up during my search for a solution despite the age.

When using PHP SwiftMailer to connect to Gmail or Google Apps email accounts you need to use the following

$transporter = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
  ->setUsername($this->username)
  ->setPassword($this->password);

$this->mailer = Swift_Mailer::newInstance($transporter);

This works fine for me. In the original code, you are using port 587 not 465 and you are not specifying the protocol (ssl). Don’t know if that bit matters, but for me port 587 failed and 465 worked fine.

Hope that helps.

Leave a Comment