Sending Gmail with Mail Gem.
1 min read

Sending Gmail with Mail Gem.

require 'rubygems'
require 'mail'

# Set up delivery defaults to use Gmail
Mail.defaults do
  delivery_method :smtp, { 
    :address => 'smtp.gmail.com',
    :port => '587',
    :user_name => '<user>@gmail.com',
    :password => '<password>',
    :authentication => :plain,
    :enable_starttls_auto => true
  }
end

# Send email with attachment.
mail = Mail.new do
  from     '<user>@gmail.com'
  to       '[email protected]'
  subject  'Hi, Michael Le'
  #add_file :filename => 'hello_world.txt', 
:content => File.read('hello_world.txt')

end

# Don't forget delivery
mail.deliver!

You can check Mikel Lindsaar’s mail gem on github.