Created
October 16, 2013 10:26
-
-
Save ssaunier/7005741 to your computer and use it in GitHub Desktop.
Mandril API Mailer used by http://www.mobicheckin.com for Mailing campaigns.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright: Applidget - 2013 | |
# License: MIT | |
# Author: Sébastien Saunier (@ssaunier) | |
require "mandrill" | |
class MandrillApiMailer | |
def initialize(key = nil) | |
key ||= ENV['MANDRILL_PASSWORD'] | |
@client = Mandrill::API.new key | |
end | |
# The format for the options has to be a HASH with the following format: | |
# https://mandrillapp.com/api/docs/messages.html#method=send | |
# | |
# (the following hash is not an exhaustive list of parameters) | |
# | |
# options = { | |
# :subject => "example subject", | |
# :html => "<p>Example HTML content</p>", | |
# :from_email => "message.from_email@example.com", | |
# :from_name => "Example Name", | |
# :to_email => "message.to_email@example.com", | |
# :to_name => "Example name", | |
# :reply_to => "message.reply_to@example.com", | |
# :tags => [ "tag1", "tag2" ] | |
# :google_analytics_domains => [ "example.com" ] | |
# :google_analytics_campaign => "message.from_email@example.com", | |
# :metadata => { :key => "value" } | |
# :attachments => [ | |
# { | |
# :type => "text/plain", | |
# :name => "myfile.txt", | |
# :content => "ZXhhbXBsZSBmaWxl" | |
# } | |
# ], | |
# :images => [ | |
# { | |
# :type => "image/png", | |
# :name => "IMAGECID", | |
# :content => "ZXhhbXBsZSBmaWxl" | |
# } | |
# ] | |
# } | |
def send(options = {}) | |
default(options, :track_opens, true) | |
default(options, :track_clicks, true) | |
default(options, :auto_text, true) | |
default(options, :inline_css, true) | |
default(options, :url_strip_qs, true) # Strip the query string when aggregating tracked URLs. | |
parse_from(options) | |
parse_to(options) | |
parse_reply_to(options) | |
unless Rails.env.production? | |
options[:subject] = "[#{options[:to].first[:email]}] #{options[:subject]}" | |
options[:to] =[ { :email => ENV['EMAIL'] || "sebastien.saunier@applidget.com", :name => options[:to].first[:name] } ] | |
end | |
@client.messages :send, :async => true, :message => options unless Rails.env.test? | |
end | |
private | |
def default(options, key, value) | |
unless options.has_key? key | |
options[key] = value | |
true | |
end | |
end | |
def parse_from(options) | |
if options[:from_email].ends_with? "mobicheckin.com" | |
default(options, :signing_domain, "mobicheckin.com") | |
elsif options[:from_email].ends_with? "awesomevent.net" | |
default(options, :signing_domain, "awesomevent.net") | |
else | |
raise ArgumentError.new "Missing :from_email" | |
end | |
end | |
# Mandrill requests the "to" field to be an array of hash like the following: | |
# | |
# :to => [ | |
# { | |
# :email => "recipient.email@example.com", | |
# :name => "Recipient Name" | |
# } | |
# ], | |
def parse_to(options) | |
if options[:to].blank? | |
if options[:to_email].blank? | |
raise "No :to key set in the options, Mandrill doesn't know where to send the message!" | |
else | |
to_email = options.delete(:to_email) | |
options[:to] = [ { :email => to_email, :name => options[:to_name] }] | |
end | |
elsif options[:to].kind_of? String | |
options[:to] = [ { :email => options[:to] }] | |
elsif options[:to].kind_of? Hash | |
options[:to] = [ options[:to] ] | |
end | |
end | |
# Mandrill requests the "Reply-To" to be put in a "headers" hash. | |
# | |
# :headers => { | |
# :Reply-To => "message.reply@example.com" | |
# } | |
def parse_reply_to(options) | |
unless options[:reply_to].blank? | |
reply_to = options.delete(:reply_to) | |
options[:headers] = {} if options[:headers].nil? | |
options[:headers]["Reply-To"] = reply_to | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment