Welcome to Giant Robots Smashing Into Other Giant Robots — a weblog about development, business, design and technology — written by thoughtbot.
extending action_mailer
Recently a client wanted to use a web-based email system called JangoMail. JangoMail allows you to send emails, create distribution lists called groups, handle situations like bounces, etc. It also provides some reporting features for all your email statistics.
JangoMail has an API that supports SOAP and also simple HTTP POST/GET requests. I decided to go with the POST version.
Now I didn’t want to change all our email code from1 |
Mailer.deliver_some_email @user |
1 2 |
email = Mailer.create_some_email @user JangoMail.send_mass_email email |
ActionMailer::Base#delivery_method is used to determine how to send an email. Currently it supports smtp, sendmail and test. The delivery method is set in your environment specific files:
config/environments/test.rb1 |
config.action_mailer.delivery_method = :test
|
Now looking through ActionMailer::Base you find #deliver!. Here’s the portion of it we care about.
1 2 3 4 5 |
begin __send__("perform_delivery_#{delivery_method}", mail) if perform_deliveries rescue Exception => e # Net::SMTP errors or sendmail pipe errors raise e if raise_delivery_errors end |
What I want is
config/environments/development.rb1 |
config.action_mailer.delivery_method = :jango_mail
|
1 2 3 4 5 6 7 8 |
require 'lib/jango_mail' ActionMailer::Base.send :include, JangoMail ActionMailer::Base.jango_mail_settings = { :username => "username", :password => "password", :url => "http://api.jangomail.com/api.asmx" } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
module JangoMail def self.included(clazz) clazz.class_eval do cattr_accessor :jango_mail_settings end end def perform_delivery_jango_mail(mail) post_data = { "Username" => jango_mail_settings[:username], "Password" => jango_mail_settings[:password], "FromEmail" => mail.from, "FromName" => "", "ToGroups" => "", "ToGroupFilter" => "", "ToOther" => mail.to, "ToWebDatabase" => "", "Subject" => mail.subject, "MessagePlain" => mail.body, "MessageHTML" => "", "Options" => "" } uri = URI.parse "#{jango_mail_settings[:url]}/SendMassEmail" Net::HTTP.post_form uri, post_data end end |
SendMassEmail is the JangoMail API call for sending an email, all the parameters are required even if they’re not being used.
About this entry
You're reading an entry on GIANT ROBOTS SMASHING INTO OTHER GIANT ROBOTS, the company weblog of thoughtbot, inc.
- Author:
- Jared Carroll
- Published:
- February 28th 01:46 PM
- Updated:
- February 28th 01:56 PM
- Sections:
- Development Technology
thoughtbot is hiring
We are hiring web developers and web designers in both Boston and New York, NY.
What are we up to?
We built Shoulda, an eclectic set of additions to Test::Unit; Paperclip to manage uploaded files without hassle; Jester, a REST/ActiveResource client library written in Javascript, and Squirrel, an enhancement for ActiveRecord's find syntax; — amongst some other projects.

Chad (President) and Jon (CTO) co-authored a technical book titled Pro Active Record: Databases with Ruby and Rails, which explores the ins and outs of the ActiveRecord ruby library. You can buy it today at Amazon.com.
About thoughtbot, inc.
We are a small web application development consulting business, with offices in Boston, MA and New York, NY. If you're looking to find a team for your next web development project or your new web application — get in touch.
1 comment
Jump to comment form