Back to main menu

Email

Using Mailgun with Capistrano recipes

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.

PUBLISHED ON

PUBLISHED ON

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!

Related readings

When transactional email preps 4 million contracts for e-signature every month

Transactional emails spring people to action – especially when they’re the only thing...

Read More

Sending millions of job alerts 24/7 with the Mailgun email API

Mailgun recently caught up with Graham Everitt over at Quantum Response Network to learn more about how his team sends transactional email around the clock. Read more...

Read More

Agape charity finds easy way to forward email for volunteers worldwide

I volunteer part time at the Agape Community Care Association (www.agape.org), an NGO that helps orphans and other...

Read More

Popular posts

Email inbox.

Email

5 min

Build Laravel 11 email authentication with Mailgun and Digital Ocean

Read More

Mailgun statistics.

Product

4 min

Sending email using the Mailgun PHP API

Read More

Statistics on deliverability.

Deliverability

5 min

Here’s everything you need to know about DNS blocklists

Read More

See what you can accomplish with the world's best email delivery platform. It's easy to get started.Let's get sending
CTA icon