DEV Community

Sending Emails in Rails with Action Mailer and Gmail

Felice Forby on May 19, 2019

In this post, I will be setting up a mailer in Rails that will send out an email to the website owner when a customer submits an order. (I was usin...
Collapse
 
godsloveady profile image
Derrick Amenuve

thank you for writing this detailed tutorial.

Collapse
 
matthewchao profile image
matthewchao

I just wanted to thank you for writing out this detailed tutorial. I'm going through the Rails Tutorial (which uses SendGrid) but was able to get everything working with Gmail thanks to your instructions. It was kind of hard finding recent, still-accurate information!

Collapse
 
damonbauer profile image
Damon Bauer

Felice -

This is such a nice post. Thanks for taking the time to break things down like you did. I'd never built a mailer in Rails, but following your post I was able to with no problems at all.

Keep up the great work!

Collapse
 
amit_savani profile image
Amit Patel

If you don't use 2-step verification, you will have to allow your account to be accessed by "less secure apps". In your Google settings under the "Security" tab, look for the "Less secure app access" section and click "Turn on access".

If this is not done, google will block you and mail won't be delivered. You may get Net::SMTPAuthenticationError: 535-5.7.8 Username and Password not accepted error

Also google may block you.

Gmail Notification

Collapse
 
morinoko profile image
Felice Forby

Honestly, I ended up running into problems even after allowing "Less secure app access" (it just stopped sending emails) so I definitely recommend using the two-factor authentication!

Collapse
 
sarasabet profile image
Sara Sabet

Hi, I'm following this article to setup mailer, and get the following error ==>
Template is missing
Missing template event_mailer/new_event_request_email with "mailer". Searched in: * "event_mailer"
would you be able taking a look on it ? Do u have any idea why I'm getting this error ?

Collapse
 
francoisfitz profile image
FITZ PATRICK

Hi there,
Thank you very much for the great tutorial.
On Docker in dev mode I had this error:
OpenSSL::SSL::SSLError (SSL_connect returned=1 errno=0 peeraddr=172.253.122.108:587 state=error: certificate verify failed (self signed certificate in certificate chain))
To fix it I added to config.action_mailer.smtp_settings:
:enable_starttls_auto => true
Maybe it will help out someone
Cheers

Collapse
 
marklocklear profile image
J. Mark Locklear

Rather than "allowing less secure apps" in gmail you can create a specific app password: support.google.com/accounts/answer...

Collapse
 
reiz profile image
Robert Reiz

Thanks man! That helped me to make it work!

Collapse
 
mahanmashoof profile image
Mahan Mashoof • Edited

Thanks a lot, the 2-step method really helped!
*just need to restart the server for it to actually work (rails s)

Collapse
 
elissonmichael profile image
Élisson Michael

Thanks for sharing this.

Collapse
 
rod_osoriobarra profile image
r-osoriobarra

Thank you bro!!!, you saved me in a part of my project. We were with my colleagues stuck in this part. Very good article. You are awesome!

Collapse
 
michael profile image
Michael Lee 🍕

This was very helpful @morinoko! Thank you so much for writing this article.

Collapse
 
morinoko profile image
Felice Forby

Thanks for your comment, Michael! I'm glad the article helped :D

Collapse
 
gnmithun profile image
Mithun Nair • Edited

Hi Felice, This is a great tutorial and it help me setup and use Action Mailer. Thank you. One question.
When I used Gmails SMTP server i.e "smtp.gmail.com", everything worked fine for me. But when I tried using a different SMTP server i.e a service called sendinblue.com/ whose SMTP server is "smtp-relay.sendinblue.com", the SMTP server returns a 501 which essentially means sender address syntax is bad. After some investigation I discovered that the reason was the sender address should be in "<>" . Oddly enough, in another part of my app where I use Devise Gem, the mail confirmation for registering a new user works fine with this SMTP server and without the <>. So the SMTP configuration and mailer is perfectly fine. Its only when I explicitly call the Action Mailer with this particular SMTP server that it fails. As I mentioned above, the mailer works fine with Gmail SMTP server also.
Any thoughts on this?

Collapse
 
asolab profile image
asolab

I'm having a issue with my action mailer functionalities, it works fine with open letter gem but with smtp config token only run on my terminal it won’t deliver to mail, 421 server too busy error.
stackoverflow.com/questions/737354...

Collapse
 
moriarty83 profile image
Chris

Oh man thank you so much for this! I was having some real trouble figuring this out and you really cover all the bases. Thanks a ton!!!

Collapse
 
johnpitchko profile image
John Pitchko

Worked great; just what I needed; thank you!

Collapse
 
thecodetrane profile image
Michael Cain

THANK YOU for this. I was beating my head against a wall all morning trying to solve this. So glad for DEV...

Collapse
 
morinoko profile image
Felice Forby

Awesome, glad this is still relevant and helped you figure our your issue!

Collapse
 
mth0158 profile image
Mathieu EUSTACHY

Thanks @morinoko for this article, it was thoroughly explained, and it still works in July 2021!
I used 2-step verification + app password method and it worked on the first try ;)

Collapse
 
morinoko profile image
Felice Forby

Great! I'm glad it helped (and still works) ;)

Collapse
 
xitarps profile image
Rodrigo Pimentel Sátyro Xita

Thank you very much :D! helped me a lot!

Collapse
 
m_ahmad profile image
Muhammad Ahmad

Thanks for this wonderful article