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”