ActionMailler and Gmail (no plugins)

Last week I had some problems while trying to configure the ActionMailler with Gmail. I already have some applications in production mode recurring to this system, but this one, using ruby 1.8.7 and rails 2.3.3 gave me some trouble. Gmail requires TLS, older versions of ruby and rails do not support that, the work around was a pluging to enhance it(for example ActionMailerTLS). If you are using Ruby 1.8.7 and Rails 2.2.1, or later versions, you don’t need any plugin, you only need to follow my instructions.

Add the following lines in your config/enviroments/production.rb

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = YAML.load File.open("#{RAILS_ROOT}/config/mailer.yml")

And then create the yaml file config/mailer.yml :

--- 
  :enable_starttls_auto: true # This is how you tell ActionMailler to use tls
  :address: smtp.gmail.com
  :port: 587
  :domain: gmail.com
  :user_name: my_username #You should type your username, you should not include the "@gmail.com".
  :password: my_password #You should type your password
  :authentication: :plain

That’s it!

Now take a look in this notifier class example:

class Notifier < ActionMailer::Base
  def activation_instructions(user)
    @recipients  = "#{user.email}"
    #@from        = "Miguel <my_username@gmail.com>" # Doesn't work anymore 
    @from        = "my_username@gmail.com" 
    @subject     = "[Card-Sorting] "
    @sent_on     = Time.now
    @subject    += "Important" 
    @body[:user] = user
  end
end

There is a lack of backward compatible. Due to some changes in the latest versions you cannot use that syntax anymore, if you are upgrading from old versions be careful, you should only simply specify your email to get it correctly parsed.

0 Responses to “ActionMailler and Gmail (no plugins)”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s





Follow

Get every new post delivered to your Inbox.