New guide: how to warm up a dedicated IP without hurting your reputation. Read now →
Log in
MigoSMTP
SecurityUpdated 26 Oct 2026

Firewall Rules for Email: Ports, Egress Control, IP Allowlisting and Safe Defaults

Which ports and directions a mail system needs open, egress rules that stop spam abuse, allowlisting provider IPs, firewalls and TLS inspection, IPv6, cloud security groups and a rule checklist for senders and receivers.

OH
Written by Omar Haddad
Regional Deliverability Analyst, Middle East and Africa
Reviewed by Sofia Marino
  • Published8 Dec 2024
  • Read time6 min
  • Length1,236 words
Share this guide

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.

InboundOnly your MX hosts accept port 25 from the internet; submission and mailbox ports are limited to users
OutboundOnly designated mail servers and relays may connect to port 25 on the internet; clients use submission
Provider rulesAllow the specific hostnames, ports and IP ranges your relay or API uses and review them regularly
InspectionDo not break STARTTLS or rewrite SMTP commands; if you must inspect, do it with a proper mail gateway

Ports and directions

ServicePortDirectionWho should be allowed
Inbound mail (MX)25/TCPInternet to your MX hostsEveryone (the public internet), with rate limits and filtering
Outbound mail transfer25/TCPYour MTA or relay to the internetOnly your designated mail servers
Submission with STARTTLS587/TCPClients and apps to your submission server (inbound), or your apps to a provider (outbound)Authenticated users; restrict by network where possible
Submission with implicit TLS465/TCPSame as 587Same as 587
Alternative submission2525/TCPTo providers that offer itOnly if 587 is blocked on a path
IMAP / POP3 over TLS993/TCP, 995/TCPClients to mailbox serversUsers; consider VPN or conditional access for sensitive systems
Plain IMAP / POP3143, 110Avoid exposingPrefer TLS-only ports. See TLS for email
ManageSieve4190/TCPClients to mailbox serversAuthenticated users only. See Sieve
HTTPS APIs443/TCPYour apps to the email API; provider webhooks to your endpointAllow specific hosts; verify signatures on webhooks
DNS53/UDP and TCPMail servers to resolversResolvers you control or trust; allow TCP for large answers
NTP123/UDPServers to time sourcesChosen sources only. See clock sync
Health and monitoring probesvariesMonitoring hosts to mail servicesKnown 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

  1. 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.
  2. 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.
  3. Allow only the needed ports (for example 587 and 443 to the provider), not all traffic to a range.
  4. Remember the return path. Stateful firewalls allow replies automatically; stateless rules and some cloud network ACLs need explicit rules for ephemeral return ports.
  5. Document each rule with its owner, purpose and review date, so stale rules are removed.
  6. For inbound webhooks, allowlisting source IPs helps but is not authentication; always verify signatures. See webhook signatures.
  7. 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

BehaviourEffectAdvice
Firewall strips the STARTTLS capability from the EHLO responseServers fall back to plain text silently; mail is sent unencryptedDisable SMTP "fixup" or inspection features that alter commands; test with a transcript. See SMTP transcript
Deep inspection of TLS with a substituted certificateCertificate validation fails; MTA-STS and DANE policies are violatedExempt mail ports from TLS interception
Connection or idle time limitsLong DATA transfers or slow servers are cut off with timeoutsRaise idle timeouts for SMTP to several minutes
Connection limits per sourceBulk senders using many parallel connections get droppedAllow the relay hosts higher limits; prefer connection reuse. See connection pooling
Application-layer "SMTP proxy" rewriting bannersBanner and HELO no longer match PTRKeep the real banner or set a correct one. See the SMTP banner
NAT with shared outgoing addressesMultiple servers appear as one IP; reputation is sharedGive 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

ItemDone when
Inbound 25 limited to MX hostsScanning from outside shows 25 only on the MX addresses
Outbound 25 limited to relaysA test from a normal host to an external server on 25 fails and is logged
Submission ports restrictedOnly users and apps can reach 587 and 465, and authentication is required
No cleartext mailbox accessPorts 110 and 143 are closed or protected
STARTTLS visibleA transcript from outside shows STARTTLS in the EHLO response
Provider allowlists currentEntries have owners and review dates; names preferred over IPs
IPv6 rules mirroredEquivalent rules exist for IPv6
Logging and alertsDenied outbound mail attempts are alerted on
DocumentationDiagram and rule list stored with the runbook

Troubleshooting routine

  1. Test from the affected host: nc -vz mail.example.com 587 or openssl s_client -starttls smtp -connect mail.example.com:587.
  2. Distinguish refused (an active reject), timed out (silently dropped), reset (a device interfering) and TLS errors.
  3. Trace the path: host firewall, network firewall, cloud security group, provider block.
  4. Check the logs of each device for the source and destination pair.
  5. Compare with a host where it works, including DNS answers (the name may resolve to different addresses).
  6. Capture a transcript to see whether the banner or EHLO response is being altered.
  7. 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.

Open the blocklist check

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.

Topics#firewall#ports#egress#allowlist#network
Was this guide helpful?Your answer helps us improve our documentation.

Start sending in minutes.

Create an account, add your domain and send your first email.

Get started Talk to sales