How to Add a Domain with Mailgun for Email Sending


Mailgun is a powerful email delivery service designed for developers. It provides an efficient way to send, receive, and track emails using your custom domain. If you're looking to integrate Mailgun into your application, the first step is to add your domain to Mailgun. This guide walks you through the process step-by-step.
Step 1: Log In to Your Mailgun Account
Go to the Mailgun website and log in to your account.
If you don’t have an account, sign up for free and follow their onboarding process.
Step 2: Navigate to Domain Settings
Once logged in, navigate to the "Sending" tab in the left sidebar.
Click on "Domains" to view and manage your domains.
Select “Add New Domain” to start the setup process.
Step 3: Enter Your Domain Name
In the Add Domain section, enter the domain name you want to use for sending emails.
- Example:
email.yourdomain.com
(a subdomain is recommended for email purposes to separate it from your main domain).
- Example:
Click "Add Domain" to proceed.
Step 4: Configure DNS Records
To authenticate your domain, Mailgun requires you to add specific DNS records to your domain's DNS settings. This ensures your emails are delivered reliably and not flagged as spam.
Required DNS Records:
TXT Records:
Used for domain verification and SPF (Sender Policy Framework).
Copy the TXT record values provided by Mailgun and paste them into your DNS settings.
Example:
Name:
email.yourdomain.com
Value:
v=spf1 include:mailgun.org ~all
CNAME Records:
Used for tracking clicks and opens in your emails.
Add the CNAME record provided by Mailgun.
MX Records:
These are for receiving emails through Mailgun.
Set the priority to the value provided (e.g., 10) and point it to
mxa.mailgun.org
andmxb.mailgun.org
.
DKIM Records:
Adds a layer of email authentication to verify that your emails aren’t tampered with during delivery.
Add the DKIM TXT records as shown in your Mailgun dashboard.
Step 5: Verify DNS Records
After adding the required records, return to the Mailgun dashboard.
Click "Verify DNS Records" to let Mailgun validate your setup.
It might take a few minutes to a few hours for the DNS changes to propagate.
Step 6: Test Your Domain
Once Mailgun verifies your DNS records:
Send a test email from your domain to ensure everything is working.
Use the Mailgun-supplied API or SMTP settings to integrate email sending into your application.
Step 7: Update Your Application with Mailgun Credentials
For applications using Mailgun, you’ll need to configure the following:
SMTP Configuration:
Host:
smtp.mailgun.org
Port:
587
(or465
for secure connections)Username:
postmaster@email.yourdomain.com
Password: Your Mailgun API key
API Integration:
Use the Mailgun API to send emails directly from your application. Use libraries like
axios
or tools likePostman
to test your setup.Example: Mailgun Node.js SDK for JavaScript/Node.js developers.
Best Practices for Using Mailgun
Use Subdomains for Email: Always use a subdomain like
email.yourdomain.com
to separate email traffic from your main domain.Enable Tracking: Take advantage of Mailgun’s tracking features (clicks, opens) to measure email performance.
Set Up Bounce Handling: Configure webhooks to manage bounces and delivery issues.
Monitor Your Sending Reputation: Avoid sending spammy or unsolicited emails to maintain your domain’s reputation.
Conclusion
Adding a custom domain to Mailgun is essential for professional and reliable email delivery. With proper DNS configuration and domain authentication, you can ensure your emails reach your users' inboxes. Whether you’re building a newsletter, transactional emails, or a marketing campaign, Mailgun simplifies the process with robust tools and features.
By following this guide, you’re now equipped to set up and use your custom domain with Mailgun effectively. 🎉
Subscribe to my newsletter
Read articles from ihtizaz Ahmad directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

ihtizaz Ahmad
ihtizaz Ahmad
I'm ihtizaz Ahmad, a Full Stack Developer with 4+ years of experience. I specialize in creating web applications with Angular, React.js,Next J's, Node.js, Nest.js, and various databases.