Sending Gmail with Mail Gem.

July 18, 2012 — 1 Comment
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.

Mike

Posts Twitter Facebook

My name is Michael Le, and I am a web developer.

One response to Sending Gmail with Mail Gem.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.