Send email using Gmail SMTP from Rails 3 platform

Recently, I co-founded another and my second Startup: InternCity. The Platform is based on Ruby  (v-1.9.3) On Rails (v-3.2).  Developing appplication on RoR platform is most favourite platform right now specially for its rich features and community supports. Whatever, one of its important features was sending email to user on different activities. When it was Rails, then everything is very much handy. Rails has built in SMTP email library support: Action Mailer . Just, you will have to configure it properly. But , here is catch, it actually works in other SMTP but not in Gmail hosted SMTP. Because, Gmail / Google Mail requires SSL SMTP mailing service, meaning you cannot send email without creating a SSL connection to its SMTP server. So, sending email using Gmail SMTP, you need to create SSL connection first. There is always someone in Rails community who will save your ass to write a new library, plugin or GEM. I got nice GEM named: tlsmail  and solved my problem.

So, add the tlsmail in project Gemfile (you can keep that in production group only because Rails doesn’t not process email in TEST ENVIORNMENT until you configure it differently )

gem 'tlsmail'

Add following configuration in Production environment located at config/environment/production.rb

require 'tlsmail'
      Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
      ActionMailer::Base.delivery_method = :smtp
      ActionMailer::Base.perform_deliveries = true
      ActionMailer::Base.raise_delivery_errors = true
      ActionMailer::Base.smtp_settings = {
          :address =>  "smtp.gmail.com",
          :port =>  "587",
          :domain =>  "yourdomin.com",
          :enable_starttls_auto =>  true,
          :authentication => :login,
          :user_name => "[email protected]",
          :password => "your password"
      }
 
    config.action_mailer.raise_delivery_errors = true

Now, you can create the mailer class or can generate it …

rails generate mailer UserMailer

You will see a blank UserMailer class created at app/mailers/user_mail.rb as following

class UserMailer < ActionMailer::Base
  default :from =>  "[email protected]"
end

Add a welcome_email method into the class

 def welcome_email(user)
    @user = user
    mail(:to =>  user.email, :subject =>  "Welcome to My Awesome Site")
  end

Finally, Call the welcome_email method from your any of the controllers by passing User Obeject

UserMailer.intern_email(@intern).deliver

Checking it  your production environment. It will send the email to desire user. Done 🙂

 

Eftakhairul Islam

Hi, I'm Eftakhairul Islam, a passionate Software Engineer, Hacker and Open Source Enthusiast. I enjoy writing about technical things, work in a couple of startup as a technical advisor and in my spare time, I contribute a lot of open source projects.

 

One thought on “Send email using Gmail SMTP from Rails 3 platform

Leave a Reply

Your email address will not be published. Required fields are marked *

 

Read previous post:
Install Node.js From Source via GIT

Yeah!!! I am going to give to try Node.js by creating small product. But don't have Node.js developing environment ready...

Close