Using php's swiftmailer with gmail

拥有回忆 提交于 2019-11-26 13:06:48
fullybaked

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.

I found this question doing a google search and used the code in the answer provided by fullybaked. It got me to the point where I could see in the Exceptions thrown that the Google smtp server was responding with an error code indicating password and username not accepted. Upon further investigation I found the following steps. Now it works great and I can continue to flesh out the code for production.

Not sure when Google made the change but in addition to the configuring your code with your Google username, password and port (465/ssl or 587/tls) you need to do the following steps to be able to use the Google smtp server.

To use the the gmail smtp server with your gmail account you need to:

1) In Google "Account settings" enable "Access for less secured apps" by setting it to "Allow".

2) In gmail settings under the "Forwarding and POP/IMAP" tab, check the IMAP status, it needs to be enabled. (This will allow the emails sent to be saved in the sent folder.)

3) If after steps 1 and 2 you still throw the Exception Google's smtp server is not accepting the user name and password you need to open a browser, go to gmail and log in, then open another tab in the same browser andgo to this address:
https://accounts.google.com/DisplayUnlockCaptcha

According to Google you may have to enter a captcha.

4) Immediately send an email from your code as this will allow it to be authorized to use your gmail account.

Hope this helps.

I found that what evad said was true, but I had to change his work a little bit for the current version of Swift Mailer. Now it is:

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

$mailer = Swift_Mailer::newInstance($transporter);

Should work fine.

The GMail SMTP system has it's issues given the SSL and ports. I find it hard to get it to work with PHP nicely.

The best thing I have found, that does work is phpGMailer. You may be able to sift through that code to see how they got it to work, but that has always worked flawlessly for me.

I know this does not address the SwiftMail issue, just figured I would point that out :)

465 is the port for ssl, 587 is used for tls encryption (look at http://swiftmailer.org/docs/sending.html#encrypted-smtp)

I also came here after hours of research. I finally found the true answer. I wanted to send email FROM google mail servers and not from my host with smtp authentication.

You can trace an email with its full headers inside of gmail, follow this guide.

https://support.google.com/mail/answer/29436?hl=en

@fullybaked had the right answer for authenticating your email via smtp but you would still be sending via your host.

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

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

@ducin was correct with the different ports and encryption types.

$transporter = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')

OR more modern and more secure, use;

$transporter = Swift_SmtpTransport::newInstance('smtp.gmail.com', 587, 'tls')

If you want to send directly from a gmail server you need to set the authorization mode. Plain is default.

$transporter = Swift_SmtpTransport::newInstance('smtp.gmail.com', 587, 'tls')
->setAuthMode('login')
->setUsername('username@gmail.com')
->setPassword('password');

$mailer = Swift_Mailer::newInstance($transporter);

@gene is also correct, you do have to authenticate your app, so follow his instruction.

Further to prevent your email from landing in the spam folder, you will also have to add dns text records with DKIM (domain keys)

https://support.google.com/a/answer/174124?hl=en

SPF records

https://support.google.com/a/answer/33786?hl=en

and DMARC

https://support.google.com/a/answer/2466580?hl=en

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!