- What's new
Easier and Faster Implementation with Our Updated SDKs
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
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?
Building the Best Email Experience for Creators - Flipsnack’s Story
Quick Tips to Getting Started with Mailgun
When Transactional Email Preps 4 Million Contracts For E-Signature Every Month
Easier and Faster Implementation with Our Updated SDKs
We stand with the AAPI community
The Difference Between SMTP and API
The Basics of Email Dark Mode
COVID-19 Survey: How the Pandemic Has Affected Email Sending
Mailgun Validations Features Improved Performance for EU Customers
International Women’s Day: How Pathwire’s Female Leaders Choose To Challenge
The Top Email Clients and Email Apps of 2021
How To Build An Email List The Right Way
The Path To Email Engagement In 2021: Key Learnings
Easier and Faster Implementation with Our Updated SDKs
We stand with the AAPI community
The Difference Between SMTP and API
Preparing Your Email Infrastructure Correctly
4 Tips To Improve Your Email Deliverability In 2021
COVID-19 Email Communications Dos and Don’ts
How To Use Parallel Programming
Mailgun’s COVID-19 Plan of Action
Password Meters Are Not For Humans
Send Your Emails at the Perfect Time with Send Time Optimization
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.