- Customer Success
How Do Users on G2 Rate Mailgun?
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
How Do Users on G2 Rate Mailgun?
Building the Best Email Experience for Creators - Flipsnack’s Story
When Transactional Email Preps 4 Million Contracts For E-Signature Every Month
Sending Millions Of Job Alerts 24/7 With The Mailgun Email API
Behind The Scene At Jungle Disk: Reliable Email Communications With Mailgun
Zipbooks Switched From Mandrill To Mailgun Using Laravel
Easily Integrate Forms Into Your App With Mailgun And Form.Io
Easily Send Email From Your Mobile App Using Appery.Io And Mailgun
TextMagic Improves Deliverability Rate With Mailgun
How Zapier Uses Mailgun To Send Emails
InboxReady x Salesforce: The Key to a Stronger Email Deliverability
Become an Email Pro With Our Templates API
Google Postmaster Tools: Understanding Sender Reputation
Navigating Your Career as a Woman in Tech
Implementing Dmarc – A Step-by-Step Guide
Email Bounces: What To Do About Them
Announcing InboxReady: The deliverability suite you need to hit the inbox
Black History Month in Tech: 7 Visionaries Who Shaped The Future
How To Create a Successful Triggered Email Program
Designing HTML Email Templates For Transactional Emails
InboxReady x Salesforce: The Key to a Stronger Email Deliverability
Implementing Dmarc – A Step-by-Step Guide
Announcing InboxReady: The deliverability suite you need to hit the inbox
Designing HTML Email Templates For Transactional Emails
Email Security Best Practices: How To Keep Your Email Program Safe
Mailgun’s Active Defense Against Log4j
Email Blasts: The Dos And Many Don’ts Of Mass Email Sending
Email's Best of 2021
5 Ideas For Better Developer-Designer Collaboration
Mailgun Joins Sinch: The Future of Customer Communications Is Here
Always be in the know and grab free email resources!
By sending this form, I agree that Mailgun may contact me and process my data in accordance with its Privacy Policy.