Jimmy Cuadra Web Services

RSS Screencast

ActionMailer and Gmail

This screencast shows how to set up a Ruby on Rails application to send email with ActionMailer and Gmail.

View the screencast in high definition View the screencast in high definition

View the screencast on YouTube View the screencast on YouTube

Screencast notes Notes

Ruby 1.8.7 and Rails 2.2.1 or later versions are required to use TLS (required for Gmail SMTP) without a plugin. If you use older versions of either Ruby or Rails, try a plugin like action_mailer_optional_tls.

I’ve improved the quality of the video for this screencast. A high quality version is available on YouTube via the link above.

I’d like to give a big thanks to Nebyoolae for providing the intro sound!

Here is the code from the screencast:

# config/environments/development.rb

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  :enable_starttls_auto => true,
  :address => 'smtp.gmail.com',
  :port => 587,
  :authentication => :plain,
  :domain => 'myapp.com',
  :user_name => 'username@gmail.com',
  :password => 'password'
}
# app/models/notifier.rb

class Notifier < ActionMailer::Base
  def gmail_message
    subject     'Message via Gmail'
    recipients  'youremail@address.com'
    from        'webmaster@address.com'
    sent_on     Time.now
  end
end
# app/views/notifier/gmail_message.text.plain.erb

Hello,

You are receiving this email via Gmail!
<!-- app/views/notifications/index.html.erb -->

<a href="/notifications/create">Send</a> a message via Gmail!
# app/controllers/notifications_controller.rb
class NotificationsController < ApplicationController
  def index
  end

  def create
    Notifier.deliver_gmail_message
    flash[:notice] = 'Your message has been sent.'
    redirect_to root_path
  end
end
# config/routes.rb

ActionController::Routing::Routes.draw do |map|
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'
  map.root :controller => 'notifications', :action => 'index'
end


Tagged with: ruby on rails, actionmailer, gmail, email

View all screencasts

Comments

Nebil Y. Layington commented

These screencasts are looking hella professional, mang. And that Nebyoolae fellow is awesome.

conspirisi commented

Hi,

I've tried this, and it doesn't seem to error. Should it work from localhost?

conspirisi commented

just for anyone else, yes it does work from localhost

Sergei Orozco commented

same here, tried this and doesn't work, no flash notice 'Your message has been sent.' and no new mail in gmail

Add a comment

Optional fields are denoted in italics.

Find content by tags