Sending Email Using the Mail Gem

This post shows you how to use the mail gem to send mail.


Published on:October 5, 2013
Below is an example script for sending mail to someone via the Mail gem. The mail gem tends to be easier to user than the built in SMTP library included with Ruby. In this example we use gmail and hence, the host and port are set up for gmail, but you can change the host/port to anything.

require 'mail'

options = { :address              => "smtp.gmail.com",
            :port                 => 587,
            :domain               => 'your.domain.com',
            :user_name            => 'your username',
            :password             => 'your password',
            :authentication       => 'plain',
            :enable_starttls_auto => true  }



Mail.defaults do
  delivery_method :smtp, options
end

Mail.deliver do
       to '[email protected]'
     from '[email protected]'
  subject 'test email'
     body 'This is a test.'
end