Skip to content

Sending E-mail with Rails via Gmail (Spoiler Alert! this is for dummies)

by dkberktas on July 30th, 2010

Yes, Rails is super cool, everything is super easy, and so far I really fall in love with it. But why is it that hard to send an e-mail via Gmail !?!

So after messing up a couple of git branches, I finally get it work, yey!

Here are the steps:

  • Watch the railscast about sending email
  • Install the gem with TLS
  • sudo gem install ambethia-smtp-tls -v '1.1.2' --source http://gems.github.com
  • Add the following to your environment.rb (Be careful after the Rails::Initializer.run do |config| block. )
  • #to the top of file
    require 'smtp-tls'
    
    ...
    #to the very end of the file
    ActionMailer::Base.smtp_settings =
    {
        :address => "smtp.gmail.com",
        :port => 587,
        :domain => "gmail.com",
        :user_name => "dkberktas",#not with @gmail.com
        :password => "xxxyyyzzz",
        :authentication => :plain,
        :enable_starttls_auto => true
    }
  • Run the mailer generator by
  • script/generate mailer user_mailer
    
  • Go to UserMailer class and add the method
  •   def registration_confirmation()
        recipients  "xxx@gmail.com"
        from        "xxx@gmail.com"
        subject     "Thank you for being that awesome"
        body        "no I am just kidding" #if you want to pass parameter do it like this -- > ":user => user"
      end
    
  • Add a sample method to application controller and add it to the routes.rb
  • #in application controller
      def mailDeneme
        UserMailer.deliver_registration_confirmation()
        render :text => "OK"
      end
    #in routes.rb
    map.mail  '/mail',  :controller => 'application', :action => 'mailDeneme'
    

If you see an error like this:

Net::SMTPAuthenticationError (530 5.7.0 Must issue a STARTTLS command first.

You miss the item above about  installing smtp-tls gem. Read it again again, or check this forum post.

ps. This is the official rails guide.
ps2. this is another link.

From → tech

No comments yet

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS