Back to main menu

Case Studies

How Ruby-based DNSimple uses Mailgun’s API to create & configure domains for email forwarding

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.

Company size

--

Industry

All

Use case

All

Key metrics

Easy

Setup and Maintainability

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.

Programmatically creating and configuring domains

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:

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.

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.

Setting up email forwarding based on regex match

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:

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:

And if they have no more routes then we also remove the domain from the Mailgun system:

Conclusion

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.

“We’re able to ensure a better service for our marketers by using Mailgun. They count on us, and we count on Mailgun: and this relationship helps us maintain credibility with our customers.”

Paulius Milisauskas
115F513A-17CF-463E-999E-489BC063244D

Paulius Milisauskas

VP of Customer Operations

Related resources

Header Image - Introducing the new Mailgun

Video

4min

Introducing the new Mailgun

Read More

Email's Not Dead banner

Podcast

S1 Ep. 03: Where we’re going, we don’t need spam

Read More

Email's Not Dead banner

Podcast

S1 Ep. 07: Season Finale: Complaints and unsubscribes

Read More

Email icon

Keep me posted! Get the latest from Mailgun delivered to your inbox.

Send me the newsletter. I expressly agree to receive the newsletter and know that I can easily unsubscribe at any time.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

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