Setup Mailgun with Digital Ocean

One big panic thing is managing and having to set up the email server of own. It requires a lot of technical things, resources, pricing, security prerequisites and many more. A lot of modern, secure, reliable mail service providers are up in the tech market these days.

Here, in this blog post, we are going to set up a renowned mail service provider to work your project with low hassle. The mail service provider I'm setting up is Mailgun.

I use Digital Ocean as a primary hosting partner for the Laravel projects I work for my clients. We are going to setup Mailgun with Digital Ocean platform to send emails from the web application. In the previous years, Mailgun used to work without doing any configuration side of things to Digital Ocean end. In the following years, they want us the verify the ownership to use their service with any third party application.

Before following this article, I will assume you have following services available to you if you would like to implement the step by step guide.

Prerequisites

  • Register for Digital Ocean, create a droplet, install the platform you want to run your application.
  • Register your account with Mailgun

Before starting let's understand some benefits of using Mailgun.

  • Exciting thing up to now is, it gives you 10,000 emails free every month, no payment hassle for really small to medium-sized companies.
  • Reliable & Powerful APIs that facilitates to send, receive, track your emails seamlessly.
    Trusted by the tech giants in the industry, so kind of no compromise in security side.
  • If your business needs more than the free emails provided by the service provider, you can choose their simple pricing method as per your requirement. The pricing is like pay as you go, as they give you interface to calculate the price based on your needs for your monthly volume of emails you want.

There are a lot of competitors to this service providing the same quality of email services. I am using Mailgun for long with my projects, so due to its simplicity and reliability, I am considering to use it furthermore for now.

We are not going through any large server setup in this article. We are just going to look at the Networking part under the Digital Ocean account to manage a few DNS configuration to make emails stuff work with Mailgun.

Mailgun
After your successful registration with Mailgun service, the first step is to go to the Domains tab. For the fresh new account, you will see one record saying sandbox domain, which allows testing the development phase emails of your application before going to production.

Now, our concern is to setup emails environment for the production application.

Add Mailgun DomainGo through the steps below to add a new domain.

  • Click on the Add New Domain button and enter your production domain name in which you are going to use this Mailgun email service.
  • After adding your production domain, it will appear in the list under the Domains tab.
  • Click on the domain name from the list to view the email service configurations to be added to Digital Ocean service.
  • After clicking your domain from the list, you will see a page with a lot of details, and configuration that can be changed or managed to make the email service up and running.

Review the following headings on the page based on your need according to your application context.

  • Domain Information
  • Domain Settings
  • Tracking Settings
  • Security Settings For Outgoing Mail
  • IP Management
  • Domain Verification & DNS

In this article, we are going to look more in-depth about Domain Verification & DNS.

Before you add a few DNS configuration to the hosting partner end, the Mailgun shows the status of your domain as Unverified.

Note: I am taking Digital Ocean as my hosting partner, but not only Digital Ocean, this article works for any hosting provider which gives you access to manage DNS settings from their user panel based on what type of plan you choose from them.

After completing the above few things in the Mailgun end, we now have to log in into the hosting partner, for me its Digital Ocean.

Digital Ocean

After logging into digital Ocean dashboard, navigate to the Networking tab. It shows you the list of domains attached with droplets you own for your applications.

If you are going to manage the new domain, add it to the list, or select the existing one from the list.

Digital Ocean DNS PanelWe have to add a few DNS records like TXT, MX, CNAME to verify the Mailgun domain form the Digital Ocean DNS management panel.

Add following records under the Networking > yourdomain.com.

  • A record should always point to your droplet's IP Address, not the IP Address given by previous Mailgun configuration page.
  • While adding TXT record given by Mailgun, wrap them in double quotes before saving in Digital Ocean end.
  • TXT, MX, CNAME should be the correct value from Mailgun.

Mailgin Domain VerificationAfter all setup, you have to lastly go to Mailgun, under Domain Verification & DNS click on Check DNS Record Now, to verify the domain configuration page.

Usage

If you are using Mailgun with Laravel, you have to update your .env file or config/mail.php to use mailgun driver, and config/services.php

'mailgun' => [
    'domain' => 'your-mailgun-domain',
    'secret' => 'your-mailgun-api-key',
],


To get the API Key, select the domain from the Domains list, and you will see it on that page.

Testing

To test your production emails, now perform some email sending functionality, the emails log will appear in the Mailgun dashboard as processed, delivered, dropped and so on.

Conclusion

Thanks for reading this article up to the end. If you have any queries on setting up Mailgun, feel free to ask via comment. I will try to reply or even help for other hosting partners including Digital Ocean.

Last Updated: 7th, October 2018

Happy Coding!