keepcalculator

06 Numbers and Powers

SMTP Setup & Restricting Access Guide

Card No. 06·72929 · Updated · Standard formula, shown below

Setting up SMTP email services and restricting access means configuring a mail server to send outgoing email and then locking down who can actually use it, typically through authentication, IP allowlists, and port controls. This article walks you through the core setup steps and the practical ways to prevent unauthorized use, which is critical because an open SMTP relay is how spammers hijack servers.

What SMTP Actually Does

SMTP (Simple Mail Transfer Protocol) is the standard protocol for sending email across the internet. It works on a push basis: your email client (like Outlook or Gmail) hands a message to an SMTP server, which then talks to other SMTP servers to deliver it. The protocol traditionally uses port 25 for server-to-server communication, but most modern setups use port 587 with STARTTLS for client submissions, and port 465 for implicit TLS. Understanding these ports matters because restricting access often means deciding which ports are open to which networks.

Your mail server has two distinct roles: it receives mail from other servers (inbound, usually on port 25) and it accepts mail from your users (outbound submission, usually on port 587). The outbound role is where you need the strictest controls, because that is the part spammers will try to abuse.

Core SMTP Server Setup Steps

Setting up a basic SMTP service involves installing a mail transfer agent (MTA) like Postfix, Exim, or Sendmail, configuring it to accept connections, and defining your domains. The essential steps are:

  • Install the MTA: For example, on Ubuntu, sudo apt install postfix and choose “Internet Site” during configuration.
  • Set your hostname: The server needs a fully qualified domain name (FQDN) like mail.yourdomain.com that resolves correctly in DNS.
  • Configure the domains you handle: Tell the server which domains it should accept mail for (e.g., mydomain.com).
  • Set up DNS records: You need an MX record pointing to your server for inbound mail, and an SPF record that lists your server as an authorized sender for your domain.
  • Enable TLS: Install an SSL certificate so connections can be encrypted, which is now a requirement for most providers and recipients.

Once these are in place, the server can send and receive mail, but it is wide open by default. That is where restriction comes in.

Why Restricting SMTP Access Is Non-Negotiable

An SMTP server without restrictions is an open relay: it will accept mail from anyone and forward it to anyone. Spammers scan the internet for open relays constantly, and if they find yours, they will use it to send millions of spam messages. The consequences are severe: your server IP gets blacklisted, your legitimate email goes to spam folders, and you may face bandwidth bills or legal trouble. Restricting access is not an optional security feature; it is the difference between a mail server and a spam cannon.

The core principle is to only allow authenticated users or known IP addresses to send mail through your server. Everything else should be rejected.

Methods to Restrict SMTP Access

There are several layers you can apply, and you should use them together for defense in depth:

  • SMTP Authentication (AUTH): Require users to log in with a username and password before sending. This is the most common method and is enabled by default in most modern MTAs. In Postfix, this means setting smtpd_sasl_auth_enable = yes and configuring a SASL backend like Dovecot.
  • IP Allowlisting: Restrict which IP addresses or networks can connect to the submission port. For example, you might only allow your office’s public IP or your VPN range. In Postfix, you can use mynetworks to define trusted networks that can relay without authentication.
  • Port Blocking: Do not expose port 25 to the public internet on your firewall. Only allow inbound connections from known mail servers (which is hard to enumerate) or simply use port 587 for all client submissions and keep port 25 only for receiving mail from other servers.
  • Rate Limiting: Limit how many messages a single user or IP can send per hour. This prevents a compromised account from blasting out thousands of emails. Tools like policyd or postfwd can enforce these limits.
  • Greylisting and Spam Filters: While not access control per se, these reduce the impact of unauthorized use by rejecting suspicious messages.

The most effective setup requires authentication for all submissions and additionally allows trusted IPs to skip authentication if you want convenience for internal tools.

Step-by-Step: Restricting SMTP in Postfix

Postfix is the most widely used MTA on Linux, so it is a good reference. Here is a practical configuration sequence:

  1. Edit /etc/postfix/main.cf: Set smtpd_tls_cert_file and smtpd_tls_key_file to your certificate paths, and set smtpd_tls_security_level = may to enable opportunistic TLS.
  2. Enable SASL authentication: Add smtpd_sasl_auth_enable = yes, smtpd_sasl_type = dovecot, and smtpd_sasl_path = private/auth. Then configure Dovecot to provide authentication against your user database.
  3. Define who can relay: Set mynetworks = 127.0.0.0/8 [::1]/128 to only allow localhost by default. Then add smtpd_relay_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination. This means only trusted IPs and authenticated users can relay.
  4. Restrict the submission port: In master.cf, the submission service (port 587) should have -o smtpd_tls_security_level=encrypt and -o smtpd_sasl_auth_enable=yes. This forces encryption and authentication on that port.
  5. Block port 25 on your firewall: Use ufw or iptables to deny incoming connections on port 25 from unknown IPs. You can allow specific mail server IPs if you need to receive mail from them.
  6. Test: Try sending mail without authentication from a remote host; it should be rejected. Then try with authentication; it should succeed.

This configuration blocks open relaying while allowing your legitimate users to send mail.

Common Mistakes and Troubleshooting

Even with the right configuration, things go wrong. Here are the most frequent issues:

  • Forgetting to restart the service: Postfix and Dovecot need a restart after config changes. Run sudo systemctl restart postfix dovecot.
  • Firewall blocking the submission port: If you block port 25 but also block 587, clients cannot connect. Ensure your firewall allows 587 (and 465 if you use it).
  • Authentication fails but logs say “SASL authentication failed”: Check that Dovecot is actually running and that the socket path matches. Also verify that the user exists in the database.
  • Mail goes to spam: Even with correct SMTP setup, missing SPF or DKIM records cause your mail to be flagged. Add these DNS records to improve deliverability.
  • Relay access denied for legitimate users: If users are not authenticating, they will be rejected. Make sure your email client is set to use authentication on the submission port.

A quick way to test your server is to use telnet mail.yourdomain.com 587 and try the EHLO command. If the server advertises AUTH, then authentication is available.

Comparing SMTP Restrictions: Authentication vs. IP Allowlisting

Authentication and IP allowlisting serve different purposes, and you often need both. Authentication is user-based: it verifies the identity of the person or system sending mail. It is essential for roaming users who connect from different IPs. IP allowlisting is network-based: it trusts a specific IP range without requiring a login. It is convenient for office networks or application servers that cannot easily store credentials.

The trade-off is security versus convenience. IP allowlisting is weaker because an attacker who compromises any device on the trusted network can relay mail. Authentication is stronger but requires managing passwords and protecting them. The best practice is to require authentication for everyone, and optionally allow IP-based trust for internal services that cannot authenticate.

Frequently Asked Questions

What is an open relay and how do I check if my server is one? An open relay is an SMTP server that accepts and forwards mail from any source to any destination. To check, you can use an online open relay test tool, which attempts to send a test message through your server from an external IP without authentication. If it succeeds, your server is an open relay.

Should I disable port 25 entirely? No, port 25 is how other mail servers deliver mail to you. If you disable it, you will not receive inbound mail. However, you should not use port 25 for client submissions; use port 587 instead. You can restrict port 25 to only accept connections from known mail servers, but this is hard to maintain, so most admins leave it open but rely on spam filters.

What is the difference between SMTP and IMAP/POP3? SMTP is for sending mail, while IMAP and POP3 are for receiving and reading mail. SMTP pushes messages to a server, and IMAP/POP3 pull them down to your client. They use different ports and protocols, and you restrict them differently.

How do I set up SMTP authentication for my email client? In your email client, enter your full email address as the username and your email password as the password. Set the outgoing server (SMTP) to your server hostname, port 587, and choose STARTTLS or TLS as the encryption method. Then enable “Authentication required” or “Use secure authentication.”

Why is my mail going to spam even though my SMTP is configured correctly? SMTP configuration alone does not guarantee deliverability. You need to set up SPF, DKIM, and DMARC records in your DNS to prove your server is authorized to send for your domain. Also, check that your server’s IP is not on a blacklist, and ensure your reverse DNS (PTR record) matches your hostname.

Can I restrict SMTP by country or geo-location? Yes, you can use firewall rules or tools like geoip to block or allow connections based on the source country. However, this is not reliable because attackers can use proxies or VPNs. It is better to rely on authentication and IP allowlisting for security.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →