Send Emails in Ruby

If you ever need to send emails from ruby, use the mail gem. Run

gem install mail

to install the gem. Here’s how to send an UTF-8 encoded multipart mail via SMTP and TLS.

require 'mail'

m = Mail.new do
  delivery_method :smtp, {
    :address => 'smtp.mydomain.com',
    :domain => 'mydomain.com',
    :user_name => 'smtp_login',
    :password => 'smtp_password',
    :enable_starttls_auto => true,
    :authentication => :plain,
  }

  from    'me@mydomain.com'
  to      'you@yourdomain.com'
  subject 'Testing, 1, 2, 3!'

  text_part do
    content_type 'text/plain; charset=UTF-8'
    body 'Hi there!'
  end

  html_part do
    content_type 'text/html; charset=UTF-8'
    body '<html><body><h1>Hi there!</h1></body></html>'
  end
end

m.deliver
This entry was posted in Ruby and tagged , , . Bookmark the permalink.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>