Swift_TransportException: Connection to tcp://smtp.mailgun.org:587 Timed Out
The error means your server never completed a TCP connection to Mailgun's SMTP host on port 587, so no credentials were tried. The usual cause is an outbound firewall, security group or host block. Test with nc, open outbound traffic, switch to port 2525 or 465, or use Mailgun's HTTP API.
Sohail Hussain9 min readShort answer
- Your server sent a connection request to smtp.mailgun.org on port 587 and never got an answer, so something between the server and Mailgun is dropping the traffic. Your SMTP password is not the problem, because login never started.
- Run a port test from the failing server. If 587 hangs but 2525 or 465 connects, switch ports; if every port hangs, open outbound rules or send through Mailgun's HTTP API on port 443.
- Then run php artisan config:clear and restart queue workers, or Laravel keeps using the old settings.
You see this in your Laravel log, a failed job, or a PHP stack trace:
Swift_TransportException: Connection to tcp://smtp.mailgun.org:587 Timed Out
It often works on your laptop and fails after you deploy. That pattern points at the server's network, not your code.
Why it happens
Swift Mailer, the library behind older Laravel mail drivers, opens a plain TCP socket to the host and port you configured. "Timed Out" means that socket never connected. The usual causes, most common first:
- Outbound SMTP is blocked by the host or cloud provider. Many VPS and cloud providers restrict outbound mail ports by default to limit spam. Mailgun's own help page notes that some ISPs block or throttle port 25, and it recommends 587 instead.
- A firewall rule or security group drops egress. Cloud security groups, network ACLs,
iptablesorufwrules can all block the port. - No route out. An instance in a private subnet without a NAT gateway cannot reach the internet at all.
- Stale config. You fixed
.env, but Laravel still reads a cached config, or a queue worker still holds the old values in memory. - Broken IPv6, DNS or container networking. The hostname resolves but connections hang.
- Wrong regional host. An EU-region Mailgun domain uses
smtp.eu.mailgun.org; Mailgun's own docs name that host for EU domains.
Newer Laravel versions (9 and up) use Symfony Mailer, so you may see Symfony\Component\Mailer\Exception\TransportException with similar wording. The diagnosis is the same.
| Symptom | What it means | Where to look |
|---|---|---|
Connection ... Timed Out | Packets dropped or unanswered | Firewall, host policy, routing |
Connection refused | Host answered, nothing listening | Wrong host or port |
Expected response code 250 but got code "535" | Connected, login rejected | SMTP username and password |
| TLS handshake errors | Encryption mode does not match the port | Port and encryption pair |
Step-by-step fixes
Fix 1: test the connection from the failing server
Run this on the server that throws the error, not on your own machine:
nc -vz -w 10 smtp.mailgun.org 587
If nc is missing:
timeout 10 bash -c 'cat < /dev/null > /dev/tcp/smtp.mailgun.org/587' && echo open || echo blocked
Then check DNS:
dig +short smtp.mailgun.org
Read the results like this:
- No DNS answer: fix the server's resolver first (
/etc/resolv.conf, VPC DNS settings). - 587 hangs but 2525 or 465 connects: something filters 587 specifically. Go to Fix 3.
- Every port hangs: a general outbound block. Go to Fix 2.
- All ports connect from the shell but PHP still fails: the problem is in your app config or PHP environment. Go to Fix 5.
Mailgun's help page says its servers listen on ports 25, 465 (SSL/TLS), 587 (STARTTLS) and 2525, so test all of them: Our servers listen on ports 25, 465 (SSL/TLS), 587 (STARTTLS), and 2525. See Mailgun's SMTP credentials and ports article for the current list.
Fix 2: open outbound rules
Allow outbound TCP to the SMTP port on every layer that can filter it:
- Cloud security group or firewall. On AWS, check the instance's security group outbound rules and the subnet's network ACL. Network ACLs are stateless, so return traffic on ephemeral ports must be allowed too.
- Host firewall. Check
iptables -L OUTPUT -n,ufw statusorfirewall-cmd --list-all. - Hosting provider policy. Check the provider's docs for an SMTP restriction, or ask support to lift it.
- Private subnet routing. Confirm the instance has a NAT gateway or equivalent route to the internet.
- Mailgun IP allowlist. If you enabled one, make sure the server's public IP is on it. Mailgun says that only the IPs you have specified can connect once it is in use, and that since April 2024 it covers SMTP as well as the API (see Mailgun's IP allowlist article). That page doesn't say whether a blocked IP shows as a timeout, so treat it as a second suspect after the network checks.
Fix 3: switch ports
If 587 is filtered but another port connects, change the port and keep the encryption setting that matches it.
| Port | Encryption | Notes |
|---|---|---|
| 587 | STARTTLS | Standard submission port; the one that timed out |
| 2525 | STARTTLS | Common fallback when 587 is filtered |
| 465 | Implicit TLS (SSL) | Encrypted from the first byte |
Mailgun's docs describe 465 as requiring TLS, while 25, 587 and 2525 allow a connection upgraded with STARTTLS. Laravel .env for the SMTP driver:
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=2525
MAIL_USERNAME=postmaster@mg.example.com
MAIL_PASSWORD=your-smtp-password
MAIL_ENCRYPTION=tls
For port 465, set MAIL_PORT=465 and MAIL_ENCRYPTION=ssl. Older Laravel versions use MAIL_DRIVER=smtp instead of MAIL_MAILER. For an EU-region domain, set MAIL_HOST=smtp.eu.mailgun.org.
Keep port and encryption paired
Using STARTTLS settings on port 465, or implicit TLS on 587, produces timeouts or handshake errors of its own. Change the port and the encryption value together.
Fix 4: skip SMTP and use the Mailgun HTTP API
If your host will not unblock SMTP, the API sends over HTTPS on port 443, which is rarely filtered. Laravel has a built-in mailgun mailer:
MAIL_MAILER=mailgun
MAILGUN_DOMAIN=mg.example.com
MAILGUN_SECRET=your-mailgun-api-key
MAILGUN_ENDPOINT=api.mailgun.net
Use api.eu.mailgun.net as the endpoint for an EU-region domain. Newer Laravel versions need the Symfony Mailgun bridge and an HTTP client:
composer require symfony/mailgun-mailer symfony/http-client
The mailgun entry in config/services.php must read those variables. Setup differs between Laravel releases, so check the Laravel mail documentation for your version.
I'd pick the API over fighting a host that blocks SMTP. It is one less thing to debug, and API errors are usually clearer than SMTP timeouts. Note that the API uses an API key (MAILGUN_SECRET), not the SMTP password.
Fix 5: clear Laravel's config cache and restart workers
After config:cache, Laravel does not read .env at runtime. Clear it:
php artisan config:clear
php artisan cache:clear
If you deploy with caching, rebuild it afterward with php artisan config:cache. Long-running queue workers keep the old config in memory, so restart them:
php artisan queue:restart
This is the most common reason a fix "didn't work": the worker kept trying port 587 with the old settings.
Fix 6: rule out the less common causes
- IPv6. If the host resolves to IPv6 but your server's IPv6 routing is broken, connections hang. Run
nc -4 -vz smtp.mailgun.org 587; if that works and the plain test hangs, fix or disable IPv6 on the server. - Containers. Run the
nctest inside the container, not only on the host. - Timeout too short. If the shell connects but PHP times out, a slow route may need a longer timeout. Symfony Mailer's SMTP transport accepts a
timeoutoption. - Mailgun-side issue. Rare, but check Mailgun's status page before spending an hour on your firewall. If every port fails from several unrelated networks, it is probably not you.
Checklist before you try again
ncwas run from the failing server (or container), not your laptop- The host is
smtp.mailgun.orgfor US domains, orsmtp.eu.mailgun.orgfor EU domains - Port and encryption are paired: 587 or 2525 with
tls, 465 withssl - Outbound rules, network ACLs and any host firewall allow that port
- The server's IP is on your Mailgun IP allowlist, if you use one
php artisan config:clearwas run and queue workers were restarted
If nothing works
Contact your hosting provider first if the test fails on every port; they control egress rules. Contact Mailgun only if the connection works from other networks but not yours. Include these:
- The exact error text and the time it happened (with timezone)
- The
ncoutput for ports 587, 2525 and 465 - The server's public IP and the Mailgun domain and region
- The output of
traceroute smtp.mailgun.org - What you already tried (ports, rules, config cache)
Subject: Outbound SMTP to smtp.mailgun.org times out from {server-ip}
Hello,
Since {date and time, timezone}, our server at {server-ip} cannot connect to
smtp.mailgun.org on port 587. Laravel reports:
Swift_TransportException: Connection to tcp://smtp.mailgun.org:587 Timed Out
Test results from the server:
- nc -vz -w 10 smtp.mailgun.org 587: timed out
- nc -vz -w 10 smtp.mailgun.org 2525: {result}
- nc -vz -w 10 smtp.mailgun.org 465: {result}
- traceroute output attached
Is outbound SMTP restricted for this server or network? If so, can it be
lifted for ports 587, 2525 or 465?
Thank you.
Common questions
Is the timeout caused by wrong Mailgun credentials?
No. A timeout happens before authentication starts. Bad credentials give a different error, typically a 535 response after the connection succeeds.
Which port should I use if 587 is blocked?
Try 2525 first, since it is a common fallback and also uses STARTTLS. If STARTTLS traffic is being interfered with, try 465 with implicit TLS. Mailgun's page also notes that Google Compute Engine allows 2525 for SMTP submission.
Why does it work locally but not on my server?
Your home or office network usually allows outbound SMTP submission, while cloud and shared hosts often restrict it. Test with nc from the server itself.
Should I just use the API instead of SMTP?
If the host blocks SMTP and will not lift it, yes. The API runs over HTTPS on port 443, and Laravel supports it natively. For background on how SMTP and its encryption modes work, see our guide to SSL/TLS for email and SMTP encryption and the SMTP glossary entry.
Explore: Email Deliverability
Related Articles
Apollo + Mailgun: "domain not on a Mailgun account" and IMAP
Apollo shows a "domain not being on a Mailgun account" error and asks for an IMAP host because Mailgun is a transactional sending service with no mailboxes. Verify the domain in Mailgun if you send by API; for sequences that need replies, connect a Gmail, Outlook or SMTP+IMAP mailbox instead.
Connection refused #111 on SMTP port 587: how to fix it
Connection refused #111 on port 587 means your server's TCP connection never reached the SMTP host, so a firewall, security group or hosting policy is blocking outbound traffic. Test the port with openssl s_client, open egress, switch to port 2525 or 465, or send through the provider's HTTP API instead.
DMARC Fails When SPF and DKIM Pass: Fix Alignment (550 5.7.23)
DMARC needs SPF or DKIM to pass and the passing domain to match your visible From domain. When both pass for a different domain (a vendor's bounce domain, a Mailgun subdomain), DMARC still fails. Sign DKIM with your From domain; one aligned DKIM signature is enough.
EmailError ETIMEDOUT in Ghost: Mailgun SMTP Timeout Fix
Ghost's "EmailError ETIMEDOUT ... Connection timeout" means your server could not open a TCP connection to the SMTP host in time. Newsletters go through the Mailgun API over HTTPS, so they still work. Test port 587 from the server, then open it, switch to port 2525, or fix DNS.
Ready to supercharge your email marketing?
Start sending smarter emails with AI-powered campaigns. No credit card required.
Get Started Free