Email problems are often network problems in disguise. A firewall rule blocks the submission port, a security group is missing the return path, an inspection device breaks STARTTLS or an old allowlist contains the previous IP of a provider. At the same time, firewalls are one of the most effective tools against email abuse, because compromised machines that cannot open connections to arbitrary mail servers cannot spam. Good rules therefore do two jobs: they permit exactly the mail flows the business needs and they deny everything else. This guide lists the ports and directions for common mail roles, explains egress control, provider allowlisting, the effect of inspection devices, IPv6 and cloud networking and gives a checklist and troubleshooting routine. It builds on submission vs transfer, which port to use and connection timeouts.
Ports and directions
| Service | Port | Direction | Who should be allowed |
|---|---|---|---|
| Inbound mail (MX) | 25/TCP | Internet to your MX hosts | Everyone (the public internet), with rate limits and filtering |
| Outbound mail transfer | 25/TCP | Your MTA or relay to the internet | Only your designated mail servers |
| Submission with STARTTLS | 587/TCP | Clients and apps to your submission server (inbound), or your apps to a provider (outbound) | Authenticated users; restrict by network where possible |
| Submission with implicit TLS | 465/TCP | Same as 587 | Same as 587 |
| Alternative submission | 2525/TCP | To providers that offer it | Only if 587 is blocked on a path |
| IMAP / POP3 over TLS | 993/TCP, 995/TCP | Clients to mailbox servers | Users; consider VPN or conditional access for sensitive systems |
| Plain IMAP / POP3 | 143, 110 | Avoid exposing | Prefer TLS-only ports. See TLS for email |
| ManageSieve | 4190/TCP | Clients to mailbox servers | Authenticated users only. See Sieve |
| HTTPS APIs | 443/TCP | Your apps to the email API; provider webhooks to your endpoint | Allow specific hosts; verify signatures on webhooks |
| DNS | 53/UDP and TCP | Mail servers to resolvers | Resolvers you control or trust; allow TCP for large answers |
| NTP | 123/UDP | Servers to time sources | Chosen sources only. See clock sync |
| Health and monitoring probes | varies | Monitoring hosts to mail services | Known probe sources. See SMTP monitoring |
Egress control: stopping abuse from inside
Most spam from legitimate networks does not come from the mail server; it comes from infected laptops, compromised web applications and forgotten test servers that connect directly to port 25 of the internet. A single outbound rule prevents that class of abuse:
# iptables: only the mail relay may speak SMTP to the internet
iptables -A OUTPUT -p tcp --dport 25 -m owner --uid-owner postfix -j ACCEPT
iptables -A OUTPUT -p tcp --dport 25 -j REJECT
# Network firewall policy, in words:
# - Allow mail-relay-01, mail-relay-02 -> any : 25/tcp
# - Allow all internal hosts -> mail-relay-01/02 : 587/tcp (authenticated)
# - Deny all other internal hosts -> any : 25/tcp, 465/tcp, 587/tcp (log)Log the denied attempts. A sudden burst from one host is a strong indicator of compromise, and the log is evidence for the incident response. Cloud platforms apply the same idea at the account level; many block outbound port 25 by default and lift the block only after a request, which is a good safeguard. See why port 25 is blocked.
Allowlisting a provider or relay
- Prefer names over addresses where your firewall supports it. Provider IP ranges change; a fully qualified domain name rule that refreshes DNS follows them automatically.
- If IP rules are required, obtain the official list from the provider documentation or status page, record the date and subscribe to change notices. Review the list at least quarterly.
- Allow only the needed ports (for example 587 and 443 to the provider), not all traffic to a range.
- Remember the return path. Stateful firewalls allow replies automatically; stateless rules and some cloud network ACLs need explicit rules for ephemeral return ports.
- Document each rule with its owner, purpose and review date, so stale rules are removed.
- For inbound webhooks, allowlisting source IPs helps but is not authentication; always verify signatures. See webhook signatures.
- If a customer requires you to publish fixed sending IPs, use a dedicated pool and give them the list with notice before changes. See controlling source IPs.
Inspection devices and email
| Behaviour | Effect | Advice |
|---|---|---|
| Firewall strips the STARTTLS capability from the EHLO response | Servers fall back to plain text silently; mail is sent unencrypted | Disable SMTP "fixup" or inspection features that alter commands; test with a transcript. See SMTP transcript |
| Deep inspection of TLS with a substituted certificate | Certificate validation fails; MTA-STS and DANE policies are violated | Exempt mail ports from TLS interception |
| Connection or idle time limits | Long DATA transfers or slow servers are cut off with timeouts | Raise idle timeouts for SMTP to several minutes |
| Connection limits per source | Bulk senders using many parallel connections get dropped | Allow the relay hosts higher limits; prefer connection reuse. See connection pooling |
| Application-layer "SMTP proxy" rewriting banners | Banner and HELO no longer match PTR | Keep the real banner or set a correct one. See the SMTP banner |
| NAT with shared outgoing addresses | Multiple servers appear as one IP; reputation is shared | Give mail servers their own addresses |
IPv6 and cloud networks
- Mirror IPv4 rules for IPv6. A server with an unprotected IPv6 address can be reachable on ports you thought were closed. See IPv6 sending.
- Cloud security groups are stateful by default; network ACLs may not be. Allow inbound 25 only to MX instances and restrict management ports.
- Elastic or floating IPs must be attached before warming and registered with reverse DNS. See FCrDNS.
- Container platforms often hide the source IP behind a gateway; verify that the address seen by remote servers is the intended one.
- Private connectivity to a provider (VPN or private link) can simplify rules but still needs TLS and authentication on the mail protocol.
Checklist
| Item | Done when |
|---|---|
| Inbound 25 limited to MX hosts | Scanning from outside shows 25 only on the MX addresses |
| Outbound 25 limited to relays | A test from a normal host to an external server on 25 fails and is logged |
| Submission ports restricted | Only users and apps can reach 587 and 465, and authentication is required |
| No cleartext mailbox access | Ports 110 and 143 are closed or protected |
| STARTTLS visible | A transcript from outside shows STARTTLS in the EHLO response |
| Provider allowlists current | Entries have owners and review dates; names preferred over IPs |
| IPv6 rules mirrored | Equivalent rules exist for IPv6 |
| Logging and alerts | Denied outbound mail attempts are alerted on |
| Documentation | Diagram and rule list stored with the runbook |
Troubleshooting routine
- Test from the affected host:
nc -vz mail.example.com 587oropenssl s_client -starttls smtp -connect mail.example.com:587. - Distinguish refused (an active reject), timed out (silently dropped), reset (a device interfering) and TLS errors.
- Trace the path: host firewall, network firewall, cloud security group, provider block.
- Check the logs of each device for the source and destination pair.
- Compare with a host where it works, including DNS answers (the name may resolve to different addresses).
- Capture a transcript to see whether the banner or EHLO response is being altered.
- Fix the narrowest rule, retest, and document the change.
A good mail firewall policy is short: let the MX receive, let designated relays send, let users submit with authentication and block everything else with logs. Add inspection only where it does not alter the protocol, keep allowlists current and test from outside, and a large share of both outages and abuse incidents never happens.
Check how your mail IP looks to the world Scan for blocklist listings after network changes.
References and further reading
Frequently asked questions
Which ports does an email system need?
25 inbound to MX hosts, 587 and 465 for submission, 993 and 995 for mailbox access and 443 for APIs, plus DNS and NTP.
Why should outbound port 25 be restricted?
So that compromised machines cannot spam directly; only designated mail servers should connect to port 25 on the internet.
Should I allowlist a provider by IP address?
Prefer hostnames if possible; if IPs are required, use the provider official list and review it regularly.
Can a firewall break STARTTLS?
Yes. Some devices strip the STARTTLS capability or intercept TLS, causing plain-text delivery or certificate errors.
How do I test whether a port is open?
Use nc -vz or openssl s_client from the affected host and distinguish refused, timed out and reset results.