Introduction
SMTP (Simple Mail Transfer Protocol) is essential for sending emails from applications or servers. Postfix is a reliable, secure, and easy-to-configure mail transfer agent (MTA) for Linux. This guide walks you through setting up a basic SMTP server using Postfix for outbound emails. Note: This setup is ideal for transactional emails (e.g., notifications) — not for running a full mail server.
Prerequisites
- A Linux server (Ubuntu/Debian/CentOS)
sudo
privileges- A domain name pointing to your server’s IP (e.g.,
yourdomain.com
) - Port 25 open in your firewall (check with your cloud provider; some block it by default).
Step 1: Install Postfix
Update packages and install Postfix:
# Ubuntu/Debian
sudo apt update
sudo apt install postfix -y
# CentOS/RHEL
sudo yum install postfix -y
sudo systemctl enable postfix
During installation:
- Select Internet Site when prompted.
- Enter your server’s domain (e.g.,
yourdomain.com
).
Step 2: Configure Postfix
Edit the main configuration file:
sudo nano /etc/postfix/main.cf
Update these key settings:
# Set your domain
myhostname = mail.yourdomain.com
mydomain = yourdomain.com
myorigin = $mydomain
# Limit to local network/IPs sending mail
inet_interfaces = all
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 YOUR_SERVER_IP
# Security (adjust for IPv6 if needed)
inet_protocols = ipv4
smtpd_tls_security_level = may
Replace YOUR_SERVER_IP
with your public IP (e.g., 192.0.2.1
). Use ip a
to find it.
Save the file and restart Postfix:
sudo systemctl restart postfix
Step 3: Test Your SMTP Server
Test 1: Local Command
Use telnet
to connect:
telnet localhost 25
After connecting, type:
ehlo localhost
mail from: test@yourdomain.com
rcpt to: your-personal-email@gmail.com
data
Subject: Test Email
Hello, this is a test!
.
quit
Check your Gmail’s Spam folder for the test email.
Test 2: Send via mail
Command
Install mailutils
:
sudo apt install mailutils -y # or sudo yum install mailx
Send a test email:
echo "Test body" | mail -s "Test Subject" your-email@gmail.com
Step 4: Secure Your Server
Firewall Rules
Allow port 25:
sudo ufw allow 25 # Ubuntu
# or
sudo firewall-cmd --permanent --add-port=25/tcp && sudo firewall-cmd --reload # CentOS
Prevent Spam
- Restrict Relay: Ensure
mynetworks
only includes trusted IPs. - Enable TLS (Optional): Add to
main.cf
:smtpd_use_tls = yes smtpd_tls_cert_file = /etc/ssl/certs/ssl-cert-snakeoil.pem smtpd_tls_key_file = /etc/ssl/private/ssl-cert-snakeoil.key
Troubleshooting
- Check Logs:
tail -f /var/log/mail.log # Ubuntu/Debian tail -f /var/log/maillog # CentOS
- Verify Configuration:
sudo postfix check
- Test SMTP Connectivity Externally:
telnet yourdomain.com 25
(If blocked, check firewall/ISP restrictions.)
Conclusion
You’ve now set up a basic SMTP server with Postfix! This allows your applications to send emails directly from your Linux server. For production:
- Use a dedicated email service (e.g., SendGrid, Mailgun) to avoid deliverability issues.
- Configure SPF/DKIM/DMARC records in DNS to prevent emails from being marked as spam.
- Monitor logs regularly for abuse attempts.
> Next Steps:
> – Encrypt emails with TLS: Let’s Encrypt Certbot Guide
> – Configure spam filtering: sudo apt install spamassassin
Questions? Share them in the comments below! 🐧✉️