Skip to content

Instantly share code, notes, and snippets.

@jakcharlton
Created September 1, 2013 00:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakcharlton/6401563 to your computer and use it in GitHub Desktop.
Save jakcharlton/6401563 to your computer and use it in GitHub Desktop.
Mandrill
def perform(email_job_id, recipient_ids)
job = EmailJob.find(email_job_id)
recipients = Person.where("email IS NOT NULL and email <> ''").where(id: recipient_ids)
to = []
substitues = []
sub_fields = ["title", "first_name", "last_name", "email", "telephone_number", "website"]
recipients.each do |r|
recipient = {}
recipient["_rcpt"] = "[#{r.full_name}](mailto:#{r.email})"
sub_fields.each{|sf| recipient[sf.upcase] = r.send(sf) }
substitues << recipient
to << r.email
end
args = { email_job_id: email_job_id, machine: job.machine_id, name: job.name, sidekiq_id: bid }
EmailCampaignMailer.send_mandrill(to, substitues, job.subject, job.body_html, job.body_text, args).deliver
end
class EmailCampaignMailer < ActionMailer::Base
def send_mandrill(to, substitues, subject, body_html, body_text, unique_args={})
headers "X-MC-Tags" => self.class.name
headers "X-MC-Metadata" => { environment: Rails.env }.merge(unique_args)
headers "X-MC-Track" => "opens, clicks_htmlonly"
substitues.each do |recipient|
headers "X-MC-MergeVars" => recipient
end
mail :from => "jakcharlton@gmail.com",
:to => to.join(','),
:subject => subject,
body: body_html,
content_type: "text/html"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment