- Quick tips
How to Improve Holiday Supply Chain Communication with Email
This post is written by Anthoy Eden, founder and developer, at DNSimple. DNSimple is the hassle-free way to manage all of your domains in a single place without having to click through 10 screens of upsells to complete a simple purchase. In addition to registrar services, DNSimple offers a REST API for creating and managing domains and records. They also make it easy to receive email at your domain and forward to any other email account, like an email alias you have set up to handle support or billing emails, or an external email provider like Gmail.
At DNSimple we love DNS in an almost unhealthy way. What we don’t love though is managing mail servers. Earlier this year we decided to add email forwarding as one of the services we offer to our customers, and given our distaste in email server management we went looking for a provider with a nice API that we could easily integrate. Thanks to recommendations on Twitter we ended up finding and going with Mailgun and we have been thrilled with how easy it has been for us to integrate and maintain.
Our web application is developed using Ruby on Rails, so integrating Mailgun was a snap.
We use HTTParty, a nice little Ruby gem from John Nunemaker, to communicate with the Mailgun API. First we set up a class that is our client:
require 'httparty'
class Mailgun
class MailgunError < StandardError
end
include HTTParty
base_uri 'https://api.mailgun.net/v2'
basic_auth 'api', C_.mailgun_api_key
end
Once we have this class we can call through to the API to start our process of programmatically creating and configuring new custom domains for our customers to use for forwarding emails.
mailgun_domain_res = Mailgun.get("/domains/#{domain.unicode_name}")
if mailgun_domain_res.code != 200
mailgun_domain_res = Mailgun.post('/domains', :body => {:name => domain.unicode_name, :smtp_password => smtp_password})
# if created then increment the count
if mailgun_domain_res.code == 200
increment_email_forwarding(domain)
else
raise "Failed to create domain in Mailgun"
end
end
In this case we first check to see if a domain exists in Mailgun. If it already exists then we can move onto the next step. If it does not exist then we create it by invoking a POST request to the /domains resource. We pass the name and an SMTP password. If the domain was successfully created then we increment the email forwarding count for the customer since we bill on a per domain basis. If this is the first time we create the domain then we also add MX records for sending automatically for our customers as well as the appropriate SPF record which increases deliverability.
Next we set up the email forwarding that the customer requested (e.g. forward emails to info@mycompany.com to paul@mycompany.com). In Mailgun, this feature is called a “route” because it describes how to route a message. In our case we want to route based on a regular expression provided by the customer and forward the domain to the destination email address that they choose:
mailgun_route_res = Mailgun.post("/routes", body: {
description: "Email forwarding",
expression: %Q(match_recipient('#{email_forward_attributes[:from]}')),
action: %Q(forward('#{email_forward_attributes[:to]}'))
})
mailgun_route = Hashie::Mash.new(mailgun_route_res.parsed_response)
if mailgun_route_res.code != 200
raise Mailgun::MailgunError, mailgun_route.message
end
If the route cannot be set up then we raise an error, otherwise we return the Mailgun route. Once we have a route we store the route_identifier in our database and that’s it!
If a customer decides to remove routes then we can simply delete them via the API:
mailgun_route_res = Mailgun.delete("/routes/#{email_forward.route_identifier}")
And if they have no more routes then we also remove the domain from the Mailgun system:
mailgun_domain_res = Mailgun.delete("/domains/#{domain.unicode_name}")
decrement_email_forwarding(domain)
Compared to the pain and frustration of having to operate and maintain a growing mail storage and delivery system, Mailgun provides an extremely simple way to set up email for our customers, and we love them for that.
Last updated on August 28, 2020
How to Improve Holiday Supply Chain Communication with Email
What Is a RESTful API, How It Works, Advantages, and Examples
Email’s Not Dead Season 3 Is Finally Here
Easier and Faster Implementation with Our Updated SDKs
The Difference Between SMTP and API
Catch-All Domain Support Is Now Available In Email Validations
The Basics of Email Subdomains
The Science and Art of Gmail Deliverability
When Should You Use An Email API?
How Do Users on G2 Rate Mailgun?
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.