• For devs

Using Mailgun With Capistrano Recipes

Mailgun Team
5 min read

Mailgun was created to be an antidote for email development pain. So, it always thrills us when developers take our platform a step further with the tools they create. If you have built an interesting library, module, etc. to be used with Mailgun, we’d love to know about it and highlight it on our blog.

The post below is written by Spike Grobstein a Sr. DevOps Engineer at Ticket Evolution with over 10 years of Linux experience, specializing in automation and deployment.


Sending emails manually after a successful deployment is annoying, so I decided to write a Capistrano recipe to automate it using Mailgun. You can get get the full code here. You’ll need a Mailgun account, but there is a free plan that includes access to all the Mailgun api’s that you can use for up to 200 emails a day so you can test it out and see if you like it.

To send a notification after deployment, add the following to your deploy.rb file:

set :mailgun_api_key, 'key-12345678901234567890123456789012' # your mailgun API key set :mailgun_api_key, 'key-12345678901234567890123456789012' # your mailgun API key set :mailgun_domain, 'example.com' # your mailgun email domain set :mailgun_from, 'deployment@example.com' # who the email will appear to come from set :mailgun_recipients, [ 'you@example.com', 'otherguy@example.com' ] # who will receive the email # create an after deploy hook after :deploy, 'mailgun_notify'

That’s it. When you do a deploy, it should automatically send an email using the built-in text and HTML templates.

You can also send emails that include more sophisticated variables. Check out the following example that sends different emails to the Dev and Ops teams. If you wanted to use this code, you’d just place it in yourdeploy.rb file:

# when using send_email, the following 2 settings are REQUIRED set :mailgun_api_key, 'key-12345678901234567890123456789012' # your mailgun API key set :mailgun_domain, 'example.com' # your mailgun email domain set :mailgun_recipient_domain, 'example.com' # append this to any unqualified email addresses set(:email_body) { abort "Please set email_body using `-s email_body='this is the body of the email'" } # some variables that we'll use when calling mailgun.send_email set :ops_emails, [ 'alice', 'bob' ] set :dev_emails, [ 'carl@contractors.com', 'dave' ] # some basic tasks namespace :email do task :ops do mailgun.send_email( :to => ops_emails, # build_recipients gets called automatically by Capistrano::Mailgun :from => 'some_dude@example.com', :subject => 'you have just been mailgunned', :text => email_body ) end task :devs do mailgun.send_email( :to => 'no-reply@example.com', :from => 'no-reply@example.com', :bcc => mailgun.build_recipients(dev_emails, 'audit.example.com'), # note the different domain :subject => 'You guys are just developers', :text => email_body ) end end

This defines 2 tasks that can be used to send emails to ops or devs. The email:ops task is using an Capistrano variableemail_body which should be set on the commandline. With this example, you could send an email to ops guys like the following:

cap email:ops -s email_body="You guys are awesome. Keep up the good work"

You could also take advantage of :text_template and/or :html_template for more complex messages. The above is just an example.

Also, notice the use of mailgun.build_recipients. Full documentation is available on Github.

I hope this code will help remove some of the complexity around managing large deployments. Let me know what you think!

Founded in April 2010, Ticket Evolution builds software and services that handles the real-time execution, clearing and settlement of event ticket trades. If you ever purchased a ticket to a sports game online, or called a broker for seats to a concert, chances are our system was used somewhere in between. The team at Ticket Evolution, prides themselves on creating a great place to both learn and contribute to exciting projects. They regularly solve complex technical challenges while staying abreast on both new technology and established computer science. They’re also hiring!

Last updated on August 28, 2020

  • Related posts
  • Recent posts
  • Top posts
View all

Always be in the know and grab free email resources!

No spam, ever. Only musings and writings from the Mailgun team.

By sending this form, I agree that Mailgun may contact me and process my data in accordance with its Privacy Policy.

sign up
It's easy to get started. And it's free.
See what you can accomplish with the world's best email delivery platform.
Sign up for Free