Post Date: 02 September 2020
Last Updated: 16 November 2026
Introduction
Setup Postfix with Mailgun on Ubuntu to configure email delivery through Mailgun as an SMTP relay. Postfix is a Mail Transfer Agent (MTA) responsible for delivering and receiving emails. It is essential for creating a complete mail server.
In this guide, we will explain how to install Postfix, create a Mailgun account, and configure Postfix to send emails through Mailgun.
Implementation Steps
Step 1: Install Postfix
Log in to the server using SSH.
As the root user, install Postfix using the following command:
apt-get install postfix

Now, enter the fully qualified domain name (FQDN) that you want to use for sending and receiving emails.

Once the FQDN is configured, restart the Postfix mail server using the following command:
service postfix restart
Step 2: Sign Up for a Free Mailgun Account
Mailgun provides powerful APIs that allow you to send, receive, and track emails.
Sign up for a Mailgun account using the following link:
Once you have signed up, access your Mailgun Control Panel:
Follow these steps to add your domain:
- Click the Domains tab.
- Select the Add Domain button.
- Enter your domain name.
- Click the Create Domain button at the bottom of the form.
This will create your domain in Mailgun.
Click the Domains tab again and select your newly created domain from the list. This will open the details page for your domain.
Step 3: Configure Postfix to Use Mailgun as a Relay Host
You can find your SMTP password on the domain details page in Mailgun. This password is required when configuring Postfix to send emails through Mailgun.
Edit the Postfix main.cf file using the following command:
nano /etc/postfix/main.cf
Add the following configuration at the bottom of the file:
relayhost = smtp.mailgun.org smtp_sasl_auth_enable = yes smtp_sasl_password_maps = static:postmaster@pheonixsolutions.com:password smtp_sasl_security_options = noanonymous
Replace the following values:
pheonixsolutions.comwith your domain name.passwordwith the password obtained from the Mailgun Control Panel.
Save your changes and restart Postfix:
service postfix restart
Your Postfix server will now send emails through Mailgun.
Conclusion
Postfix can be configured with Mailgun to send emails through an external relay host. By installing Postfix, creating a Mailgun account, and configuring the SMTP relay settings, you can enable email delivery through Mailgun on your Ubuntu server.
Related Articles
Official Documentation
- Mailgun – SMTP Relay Configuration
- Mailgun – Sending Messages via SMTP
- Postfix – Official Documentation
- Ubuntu – Server Documentation
How Pheonix Solutions Can Help
Reliable email delivery is essential for business communication, application notifications, and server alerts.
Pheonix Solutions provides DevOps, cloud infrastructure, server administration, and email configuration services to help businesses maintain reliable IT environments.
Our experts can help you configure SMTP relays, troubleshoot email delivery issues, manage mail servers, and improve server communication.
Contact our experts to discuss your email infrastructure requirements.
