Mailgun SMTP is a developer-focused transactional email relay by Sinch. The SMTP server is smtp.mailgun.org on port 587 (STARTTLS) or 465 (SSL). Authentication uses SMTP credentials tied to a specific sending domain — a username and password you find in the Mailgun dashboard under your domain’s settings, not your Mailgun account login. Mailgun offers a free Flex plan with 1,000 emails/month for the first 3 months, with a sandbox domain for testing that doesn’t require DNS setup. This guide covers the full setup: domain verification, finding credentials, and working code in PHP, Python, and Node.js.
:::note[TL;DR]
- SMTP Server:
smtp.mailgun.org - Port 587 (STARTTLS) or 465 (SSL)
- Credentials: found in Mailgun dashboard under your domain → SMTP credentials
- Sandbox domain available for testing without DNS setup
- Free Flex plan: 1,000 emails/month for 3 months, then pay-as-you-go :::
What are the Mailgun SMTP settings?
| Setting | Value |
|---|---|
| SMTP Server | smtp.mailgun.org |
| Port (STARTTLS) | 587 |
| Port (SSL/TLS) | 465 |
| Encryption | STARTTLS (recommended) |
| Authentication | Required |
| Username | Your Mailgun SMTP login (from domain settings) |
| Password | Your Mailgun SMTP password (from domain settings) |
Unlike personal email SMTP servers, Mailgun SMTP credentials are per domain — each sending domain in your Mailgun account has its own SMTP username and password. You find them in the dashboard, not by generating them manually.
How do I find my Mailgun SMTP credentials?
- Log in to app.mailgun.com
- Go to Sending > Domains
- Click on your domain (or the sandbox domain)
- Click the SMTP credentials tab
- You’ll see a Login (username) and a Password field
If no password is shown (it’s hidden after creation), click Reset Password to set a new one. The username format is typically [email protected] or [email protected] for custom logins.
The Scenario: You’ve been using Mailgun for a month via their API. You need to switch to SMTP because the client’s legacy PHP app only supports SMTP. You look for the password you’d “set during signup” — there isn’t one. SMTP credentials are domain-specific and separate from your account password. Check the domain settings in the dashboard.
What is the Mailgun Sandbox Domain?
Mailgun creates a sandbox domain for every new account: sandbox[random-string].mailgun.org. You can use it to send email without setting up your own domain or adding DNS records.
The catch: in sandbox mode, you can only send to authorized recipients — email addresses you explicitly allow in the sandbox domain settings. It’s designed for development and testing, not production.
To add an authorized recipient:
- Dashboard → Sending > Domains → click your sandbox domain
- Click Authorized Recipients → add an email address
- The recipient gets a verification email — they need to click the link to confirm
Once authorized, you can send test emails to that address using the sandbox domain’s SMTP credentials.
How do I add and verify a sending domain in Mailgun?
For production sending, you need a real domain with DNS access:
- Dashboard → Sending > Domains → Add New Domain
- Enter your domain (e.g.,
mail.yourdomain.com) — using a subdomain likemail.is recommended - Mailgun shows you DNS records to add: two TXT records (SPF, DKIM) and a CNAME
- Add those records in your DNS provider’s panel
- Back in Mailgun, click Verify DNS Settings
DNS propagation usually takes 15–60 minutes. Once verified, the domain status turns green and you can start sending.
:::tip
Use a subdomain like mail.yourdomain.com instead of the root domain for your Mailgun sending domain. This keeps your transactional email DNS records isolated from your root domain’s DNS, which reduces the risk of misconfiguration affecting your main website.
:::
How do I send email via Mailgun SMTP in code?
PHP with PHPMailer
composer require phpmailer/phpmailer
<?php
use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.mailgun.org';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]'; // From Mailgun domain SMTP settings
$mail->Password = 'your-mailgun-smtp-password'; // From Mailgun domain SMTP settings
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
$mail->setFrom('[email protected]', 'Your Name');
$mail->addAddress('[email protected]');
$mail->Subject = 'Test via Mailgun SMTP';
$mail->Body = 'Sent through Mailgun SMTP with PHPMailer.';
$mail->send();
echo 'Message sent.';
The From address must be on the verified Mailgun domain. The SMTP credentials (Username, Password) come from the domain’s SMTP credentials tab, not your Mailgun account.
Python with smtplib
import smtplib
from email.mime.text import MIMEText
smtp_server = "smtp.mailgun.org"
port = 587
username = "[email protected]" # Mailgun domain SMTP login
password = "your-mailgun-smtp-password" # Mailgun domain SMTP password
msg = MIMEText("Sent through Mailgun SMTP.")
msg["Subject"] = "Test via Mailgun SMTP"
msg["From"] = "[email protected]"
msg["To"] = "[email protected]"
with smtplib.SMTP(smtp_server, port) as server:
server.ehlo()
server.starttls()
server.login(username, password)
server.sendmail("[email protected]", "[email protected]", msg.as_string())
print("Email sent.")
Node.js with Nodemailer
npm install nodemailer
const nodemailer = require("nodemailer");
const transporter = nodemailer.createTransport({
host: "smtp.mailgun.org",
port: 587,
secure: false, // STARTTLS
auth: {
user: "[email protected]", // Mailgun domain SMTP login
pass: "your-mailgun-smtp-password", // Mailgun domain SMTP password
},
});
async function sendMail() {
await transporter.sendMail({
from: '"Your Name" <[email protected]>',
to: "[email protected]",
subject: "Test via Mailgun SMTP",
text: "Sent through Mailgun SMTP with Nodemailer.",
});
console.log("Email sent.");
}
sendMail().catch(console.error);
What does Mailgun cost?
| Plan | Included Emails | Price |
|---|---|---|
| Flex (free) | 1,000/month for first 3 months | Free, then pay-as-you-go |
| Foundation | 50,000/month | ~$35/month |
| Scale | 100,000/month | ~$80/month |
After the 3-month Flex period, you pay per email sent (around $0.80 per 1,000 emails). There’s no free tier beyond the trial. If you need a free permanent tier, SendGrid gives 100/day indefinitely. If you’re on AWS, Amazon SES is far cheaper at high volume.
Common Mailgun SMTP errors and how to fix them
535 Authentication Failed Wrong SMTP credentials. Check the domain’s SMTP settings tab — the credentials there are different from your Mailgun account login.
550 Relay Not Permitted
The From address domain isn’t verified in Mailgun. Add and verify the domain, or use the sandbox domain (with authorized recipients only).
No MX records found / DNS not verified
Your DNS records haven’t propagated yet, or the records weren’t added correctly. Use a tool like dig TXT yourdomain.com to check if the SPF and DKIM records are live.
Recipient address rejected (sandbox) You’re using the sandbox domain and the recipient isn’t on the authorized recipients list. Add them and have them click the verification email.
FAQ
Can I use Mailgun SMTP after the free trial ends without paying?
No. After the 3-month Flex period, Mailgun charges per email sent. There’s no permanent free tier. For free ongoing SMTP, SendGrid (100/day) is the better choice.
Does Mailgun SMTP work for marketing emails?
Mailgun is designed for transactional email (password resets, notifications, receipts). Bulk marketing campaigns need higher throughput settings and suppression list management — for that, Mailgun Campaigns or a dedicated ESP is the right tool.
What’s the difference between smtp.mailgun.org and api.mailgun.net?
smtp.mailgun.org uses standard SMTP. api.mailgun.net is Mailgun’s HTTP REST API. Both send email through the same Mailgun infrastructure. The API is more powerful (supports templates, schedules, suppression lists) but requires Mailgun-specific SDK integration. SMTP works with any SMTP-capable tool.
Can I have multiple sending domains in one Mailgun account?
Yes. Each domain gets its own SMTP credentials and DNS verification. You can switch between them by changing the SMTP username/password in your app config.
Is Mailgun good for deliverability?
Yes — Mailgun has strong deliverability when your domain is properly authenticated (SPF, DKIM, DMARC). Their email validation and bounce handling tools also help keep your sender reputation clean.
What to Read Next
- How to Use SendGrid SMTP Server for Sending Email — permanent 100/day free tier; simpler auth (just an API key)
- How to Use Amazon SES SMTP Server for Sending Email — best cost-per-email for high volume on AWS
- How to Use Zoho Mail SMTP Server for Sending Email — good for custom domain SMTP with a free plan