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

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

How Node.js app Cloud Monitoring uses the Mailgun API to automate email workflow

This post is written by Dan Di Spaltro, Director of Product for Rackspace Cloud Monitoring. Cloud Monitoring lets you monitor any...

Read more

How do users on G2 rate Mailgun?

When you’re thinking about trying out a new product or service, the first thing anybody...

Read more

Popular posts

Email inbox.

Build Laravel 10 email authentication with Mailgun and Digital Ocean

When it was first released, Laravel version 5.7 added a new capability to verify user’s emails. If you’ve ever run php artisan make:auth within a Laravel app you’ll know the...

Read more

Mailgun statistics.

Sending email using the Mailgun PHP API

It’s been a while since the Mailgun PHP SDK came around, and we’ve seen lots of changes: new functionalities, new integrations built on top, new API endpoints…yet the core of PHP...

Read more

Statistics on deliverability.

Here’s everything you need to know about DNS blocklists

The word “blocklist” can almost seem like something out of a movie – a little dramatic, silly, and a little unreal. Unfortunately, in the real world, blocklists are definitely something you...

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