Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tiegz
Last active July 18, 2018 15:43
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tiegz/6789111 to your computer and use it in GitHub Desktop.
Save tiegz/6789111 to your computer and use it in GitHub Desktop.
Getting Mailgun SMTP Batch emails to work in ActionMailer.
class FoobarMailer < BaseMailer
# The method you'll actually call to generate the batched email
def batched_foobar
# Recipient Variables
recipients = {"you@somewhere.com" => {"name" => "You Youington"}}
mail(to: "me@somewhere.com") do |format|
# Mailgun requires you to base64 your Recipient Variables JSON
format.custom('application/json', content_transfer_encoding: "base64") do
render text: Base64.encode64(recipients.to_json)
end
# The standalone email, including both headers and body
format.custom('message/rfc822') do
render text: FoobarMailer.regular_foobar.to_s
end
end
end
# The standalone email... used to generate one part of the batched email
def regular_foobar()
mail(to: "%recipient%") do |format|
format.text { render text: "Hello %recipient.name%" }
end
end
end
# Example
FoobarMailer.batched_foobar.deliver
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment