✓ I'm available for hire! Check out my open source work on Github or drop me an email

Douglas F Shearer

GMail SMTP with Ruby on Rails and ActionMailer

Trying to use GMail SMTP (or some other SMTP authenticated with TLS) with ActiveRecord to send email in Ruby on Rails? Up till now this involved requiring smtp_tls.rb in your environment.

Thankfully, Kyle Maxwell has come up with a neater solution.

Install

It’s a plugin, which you can get with this command in your Rails root…

./script/plugin install http://svn.kylemaxwell.com/rails_plugins/action_mailer_optional_tls/trunk/

Due to continued downtime of Kyle’s site please use the following URl:

./script/plugin install git://github.com/collectiveidea/action_mailer_optional_tls.git

Setup

You can turn TLS authentication on and off as you need in your environment with a new key that is added to the smtp config, tls. To use with gmail, the following should be in your environment.rb


ActionMailer::Base.smtp_settings = {
    :tls => true,
    :address => "smtp.gmail.com",
    :port => "587",
    :domain => "YOURDOMAIN",
    :authentication => :plain,
    :user_name => "GOOGLEUSERNAME",
    :password => "GOOGLEPASSWORD"
  }

And that’s it. Enjoy.

 
 

Comments

Gravatar

jesse

January 31 2008 21:24

Hey,
could you be so kind and mail it to me also?

Thanks in advance ;-)

Gravatar

Mark S

April 22 2008 02:23

Good lad, well done ;-)

Gravatar

Mike

February 02 2008 04:07

Any chance I could get in on that e-mail? I'd really appreciate it.

Gravatar

Douglas F Shearer

February 05 2008 00:44

This plugin is now hosted on my SVN until Kyle's site reappears.

Thanks.

Gravatar

Hugo

January 29 2008 20:52

Hi,
I couldnt access the plugin URL.
Can you send it to me?
Is it the same plugin as this:
http://www.stephe...

Tks

Gravatar

Douglas F Shearer

January 29 2008 21:32

Looks like Kyle's site is down, I shall email you the plugin.

This plugin is a new one that does the same thing as the one you link to, but is almost 2 years newer and Rails 2.0 compatible.

Gravatar

Alan

February 14 2008 19:43

Thanks for this! Saved me a lot of time and effort.

Gravatar

Ori

July 28 2009 18:07

thanks for this plugin, really helpful

Gravatar

Miguel C

May 13 2008 17:38

Thanks.

It works when sending e-mails to gmail accounts.

But e-mails sent to hotmail accounts will not even reach the spam box.

Is it a wrong setting?
Is it that hotmail discards every mail send via gmail smtp relay?

If that is the case, this mailer setup cannot be used in production mode.

Gravatar

Calvin C

May 05 2008 02:53

Rocks.
Quick and easy plugin installation and implementation.
Thanks for hosting it.

Gravatar

Steve Enzer

March 09 2009 03:34

I'm unable to get this working with gmail, ubuntu, rails 2.1.2 and ruby 1.8.7, this looks similar to a glitch that occurred before, but I couldn't fix it using the fix that had worked before.

wrong number of arguments (2 for 1)

vendor/plugins/action_mailer_optional_tls/lib/smtp_tls.rb:10:in `initialize'
vendor/plugins/action_mailer_optional_tls/lib/smtp_tls.rb:10:in `new'
vendor/plugins/action_mailer_optional_tls/lib/smtp_tls.rb:10:in `start'
vendor/plugins/action_mailer_optional_tls/lib/action_mailer_tls.rb:9:in `perform_delivery_smtp'
vendor/rails/actionmailer/lib/action_mailer/base.rb:520:in `__send__'
vendor/rails/actionmailer/lib/action_mailer/base.rb:520:in `deliver!'
vendor/rails/actionmailer

Gravatar

JACoulter

March 17 2009 17:01

Yet another thank you - got me up and running in no time after spending hours trying to track down the plug-in. FYI, I'm running Ruby 1.8.6 and Rails 2.2.2 and did not need to change anything - worked as advertised out of the box.

Gravatar

Chris Luebcke

March 18 2009 18:06

Let me add my thanks. The fact that this comment thread is still active 14 months after the original post shows what a need there is for this.

Gravatar

Douglas F Shearer

March 18 2009 22:38

Rails 2.3 released this week doesn't need all this stuff, I'll write a post for what it does need, and put a warning at the top of this one.

Gravatar

Taurus

October 07 2008 19:16

Thanks for the plugin - great work! One quick question. I'm able to get actionmailer to send via gmail from the console - but not from the app itself. Very odd - is there some sort of port or file owner difference I need to account for?

Gravatar

Bob

October 10 2008 20:27

I have followed the steps for this blog, and now instead of (3 for 2)

it gives me (7 for 6)?

any advice?

thank you in advance

Gravatar

yann

October 01 2009 23:15

Hi,
I've downloaded Bitnami Redmine for windows, install everything, try to configure my smtp with you solution, and unfortunatly, after that i am not able to acces my server anymore. Do you know this problem, do you know a fix (or a wawy to go back on you solution) ?

FYI, My server error is :

The proxy server received an invalid response from an upstream server.
The proxy server could not handle the request GET /redmine.
Reason: Error reading from remote server

regards,

Yann

Gravatar

Roman

December 12 2008 09:32

Nice work. Thanks!

Gravatar

Mike Bradford

December 14 2008 01:55

Great plugin. Thanks for your work.

Gravatar

Chris Koehler

December 18 2008 02:50

You're the man, thanks a lot! It works perfect.

Gravatar

Orlando

November 03 2009 15:05

Thank! for this great plugin it was not working at the beggining because I was using Ruby 1.8.7 but I followed the folloing link: http://filipteppe... and that fixed the problem I think it could be nice to add that as a note at the beginning so people can check the Ruby version first. THANKS AGAIN!!!

Gravatar

Arun Jacob

January 13 2009 18:49

Hi,

for rails 2.0.2 I was getting 'Uninitialized constant ActionMailer' at load time using the following:

ActionMailer::Base.smtp_settings = {
:tls => true,
:address => "smtp.gmail.com",
:port => "587",
:domain => "YOURDOMAIN",
:authentication => :plain,
:user_name => "GOOGLEUSERNAME",
:password => "GOOGLEPASSWORD"
}

I changed this over to use the config_action_mailer as follows.

config.action_mailer.smtp_settings = {
:address=>"zimbra.evri.com",
:port=>25,
:user_name => "analytics-consume...",
:password => "@p3vr1",
:authentication => :login

}

thanks to this link: http://blog.9elem...

Gravatar

Aditya Raj

January 22 2009 04:21

Thanks for your efforts on sharing this important information and taking efforts of hosting the plugin on your repository.

Regards,
Aditya Raj

Gravatar

Ram Kumar

February 22 2009 08:02

Great Man.. You saved me a lot of my time.
Installing the plugin worked like a magic.

Gravatar

GreenAsJade

February 25 2009 14:13

Dumb question, sorry :S

What is supposed to go in "YOURDOMAIN"?

What is the meaning of this "domain"?

Gravatar

Douglas F Shearer

February 25 2009 23:03

YOURDOMAIN would be the address at which the rails app is hosted.

Gravatar

GreenAsJade

February 25 2009 23:31

Thanks for the fast answer.

In parallel I tried it without specifying the domain, and it worked like a charm!

Thanks!!!!

so... what is the domain used for?

Gravatar

Douglas F Shearer

February 26 2009 11:56

Looking at the documentation, and digging into some mail server stuff; the domain is used for the HELO SMTP command, as used by some older mail servers.

So probably not entirely necessary as you have shown. Glad to have been of help.

Gravatar

Rob Britton

March 05 2009 03:03

I got the same "(wrong number of arguments (3 for 2))" error, fixing it is easy though.

Go to line 33 of lib/smtp_tls.rb in the plugin folder and change
check_auth_args user, secret, authtype if user or secret
to
check_auth_args user, secret if user or secret

This works, however it might be good to have the check in there too. So add this after that line:
check_auth_method authtype

Gravatar

Updates?

April 29 2009 05:19

Hi, do you mean Rails 2.3 means we don't need this plugin? What if we stay on ruby 1.8.6?

Gravatar

TF

April 29 2009 05:23

Hi, installed the plugin and got this error...?

530 5.7.0 Must issue a STARTTLS command first.

Gravatar

Douglas F Shearer

April 29 2009 11:14

Rails 2.3 apparently has support for TLS out of the box, but unfortunately there was no documentation for this when I last looked.

1.8.6 and 1.8.7 work great with this plugin. I have not had the chance to test 1.9 with it yet.

TF - What Rails version are you running?

Gravatar

somebody

May 01 2009 13:34

Many thanks! Works for me.
I am using Ruby 1.8.6 and Rails 2.3.2

Gravatar

somebody

May 01 2009 13:51

@TF: Are you sure you set the :tls => true, in the settings? I missed it the first time and got same error as you.

By the way, I am using Rails 2.3.2 and i find I still need to use this plugin. It does not work otherwise.

Gravatar

Ales

May 09 2009 01:21

I am getting the following error:

wrong number of arguments (7 for 6)

vendor/plugins/action_mailer_optional_tls/lib/action_mailer_tls.rb:10:in `start'
vendor/plugins/action_mailer_optional_tls/lib/action_mailer_tls.rb:10:in `perform_delivery_smtp'
vendor/rails/actionmailer/lib/action_mailer/base.rb:526:in `__send__'
vendor/rails/actionmailer/lib/action_mailer/base.rb:526:in `deliver!'
vendor/rails/actionmailer/lib/action_mailer/base.rb:392:in `method_missing'
app/controllers/contact_messages_controller.rb:7:in `create'

I'm running Ruby 1.8.7 and Rails 2.2.2. I removing the 'authtype' argument from line 33 of lib/smtp_tls.rb, but I still get the same error. Help??

Gravatar

Lisan

May 15 2009 11:42

Hi!

Just installed new version of Redmine (0.8.3) and Rails 2.2.2 (Ruby is still 1.8.6)

Redmine is working up to point i'm trying to install this plugin, after that it failed to launch with "502 Proxy Error":
----
The proxy server received an invalid response from an upstream server.
The proxy server could not handle the request GET /redmine/settings/edit.
Reason: Error reading from remote server
----

If i just remove plugin folder, redmine start working again. Can you help?

Gravatar

Lisan

May 15 2009 15:49

I solved problem above by installing version 2.1.2 of actionmailer. But smtp vial gmail still don't work for me. :( Now it's "error: execution epired" on the attempt to send test email from Redmine. I'm behind firewall, but port 587 is accessable via telnet, so it shouldn't be a problem...

Gravatar

Sid

June 05 2009 01:25

it took me hours to find the correct way to do this.. and your link worked in 30 secs.. really awesome.. thanks so much

Gravatar

lee

June 30 2009 16:17

i'm a asp.net web developer assigned to install redmine (v. 0.8.2) on a windows 2003 server. i have it up and running using the bitnami installer. i'm running into some issues getting the emailer to work.

i've changed the email.yml and need to install the action_mailer_tls. i've tried running a ".script/plugin" but it doesn't find my install path. can you be more specific like what is the proper redmine root directory? mine is installed at 'c:\program files\bitnami redmine stack".

thanks.

Gravatar

Ivan

July 07 2009 14:16

I'm using Ruby 1.8.7 and Rails 2.3 and the configuration below worked just fine without any plugin.

Write the code below in your config/environment.erb file and be happy.

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:enable_starttls_auto => :true,
:address => "smtp.gmail.com",
:port => 587,
:domain => "mysite.com.br",
:authentication => :plain,
:user_name => "myaddress@mysite....",
:password => "xxxxxxx",
:tls => :true
}
config.action_mailer.perform_deliveries = :true
config.action_mailer.raise_delivery_errors = :true
config.action_mailer.default_charset = "utf-8"

Best regards.

Gravatar

Douglas F Shearer

July 10 2009 16:18

Thanks for the feedback guys! Good to know the that TLS Optional support was included in 2.3 as expected. I'll update the post to reflect this soon.

Gravatar

RP

July 12 2009 17:47

How can I make this to work on rails 2.3.2 with both ruby 1.8.6 and 1.8.7?

I'm using basically the same settings as Ivan, without any plugins, and it works on my production machine running ruby 1.8.7, but it doesn't work on my development machine which runs 1.8.6.

If I install this plugin and set :tls => true, then it works on 1.8.6, but it stops working on 1.8.7. What's the best way to be able to make it run on both?

Thanks.

Gravatar

Carl

July 03 2008 20:27

I've been trying to get something like this working since I use Google Apps for about 5 hours and nothing quite worked until now. Thank you big time!

Gravatar

Burt

July 09 2008 01:08

There is a change in ruby 1.8.7 that breaks this app.

The symptom is an error like "(wrong number of arguments (3 for 2))" when sending a mail via tls.

The quick fix until the authors make a permenant fix is to remove authtype from the call to check_auth_args on line 33 of action_mailer_optional_tls/lib/smtp_tls.rb

see: http://www.redmin...

Gravatar

Sean Ouimet

July 29 2008 03:58

Thanks for this! Big help!

Add Your Comments

Commenting is closed for this entry