Sending mail via GMail with Rails ActionMailer

If you are wondering how to get your Rails app to send email through your GMail account, read on…

However, you need at least Ruby 1.8.7 according to the Agile Web Development with Rails book. I use BlueHost and they are on 1.8.6, as of July 27, 2009. So, these instructions won’t work on BlueHost (yet).

By default, ActionMailer cannot send email through GMail as it does not support SSL (TLS) and GMail requires this.

To implement this, simply

sudo gem install tlsmail

and in your environment.rb, configure ActionMailer as follows. Place it after the Rails::Initializer.run block, at the end. be sure to replace your email, username and password with what I have below:

require 'tlsmail'
Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.default_charset = "utf-8"

ActionMailer::Base.raise_delivery_errors = true

ActionMailer::Base.smtp_settings = {
  :domain          => "youremail@gmail.com",
  :address         => 'smtp.gmail.com',

  :port            => 587,
  :tls             => true,
  :authentication  => :plain,
  :user_name       => 'yourusername',
  :password        => 'TYPEYOURPASSWORDHERE'
}

Once you do the above, follow instructions on page 611 through 615, in the PDF version of the Agile Web Development with Rails book:

1. ruby script/generate mailer OrderMailer confirm sent
2. Create a simple email template for now
3. Do something similar to this to send email, from your controller:

class TestController < ApplicationController
  def ship_order
    order = Order.find_by_name("Dave Thomas")
    email = OrderMailer.create_sent(order)
    email.set_content_type("text/html")
    OrderMailer.deliver(email)
    render(:text => "Thank you...")
  end
end
VN:F [1.9.22_1171]
Rating: 3.3/5 (6 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 2 votes)
Sending mail via GMail with Rails ActionMailer, 3.3 out of 5 based on 6 ratings
Facebook Twitter Email

2 Comments to “Sending mail via GMail with Rails ActionMailer”

  1. Felipe Orellana Castillo 8 January 2010 at 3:49 pm #

    Hi

    i was trying this out, but when i run the
    script/generate mailer OrderMailer confirm sent

    i get the following error

    :10: uninitialized constant ActionMailer (NameError)
    from /home/forellana/.rvm/gems/ruby-1.8.7-p174%test_algo/gems/rails-2.3.5/lib/initializer.rb:111:in `run’
    from /home/forellana/workspace/ATS/ats_front/config/environment.rb:9
    from /home/forellana/.rvm/ruby-1.8.7-p174/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require’
    from /home/forellana/.rvm/ruby-1.8.7-p174/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require’
    from /home/forellana/.rvm/gems/ruby-1.8.7-p174%test_algo/gems/rails-2.3.5/lib/commands/generate.rb:1
    from /home/forellana/.rvm/ruby-1.8.7-p174/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require’
    from /home/forellana/.rvm/ruby-1.8.7-p174/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require’
    from script/generate:3

    But i was doing things like you told, so it’s weird… can you help me with this?

    thanks, greetings

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  2. Kaspars Bankovskis 26 August 2009 at 3:50 am #

    Actually there is no need for additional gems/plugins for TLS support in ActionMailer, as this support has been there for some time already.
    All you need is to put this directive in your smtp_settings:

    :enable_starttls_auto => true

    Tickets #1336 and #1731 at https://rails.lighthouseapp.com/ is about the subject.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)

Leave a Reply