Tuesday, January 24, 2012

Sending Transactional Messages from a Ruby Application

Do you need to send transactional emails from a Ruby application? Do you need open tracking, DKIM signing and easy to read reports? If so, then JangoSMTP is your solution.

This example demonstrates how to send messages using JangoSMTP from within a Ruby application. You will need the Mail gem, which can be installed with the command gem install mail.

After the mail gem is installed, download the source code for this example and insert your own JangoSMTP username/password/etc. It's as easy as that!
require 'mail'

Mail.defaults do
  delivery_method :smtp, { :address   => "relay.jangosmtp.net",
                           :port      => 25,
                           :domain    => "YourDomain.com",
                           :user_name => "Your Username",
                           :password  => "Your Password",
                           :authentication => 'plain',
                           :enable_starttls_auto => true }
end

# Set up your email
# The from address must be stored in your list of from addresses
# see Settings > Advanced > From Addresses
mail = Mail.deliver do
  to 'To@EmailAddress.com'
  from 'From Name <FromAddress@YourDomain.com>'
  subject 'This is the subject!'
  text_part do
    body 'This plain text email was sent by Ruby!'
  end
  html_part do
    content_type 'text/html; charset=UTF-8'
    body '<b>This HTML email was sent by Ruby!</b>'
  end
end