Here is a guide to allow a Ubuntu 22.04 server to send out local server based emails using Postfix.
Test Command Line Email
To test if emails can be sent via the command line, SSH into your server and try
echo "Is email sending OK..?" | mail -s "Sending email!" [email protected]
Nothing should return, if you do get a reply like the below, you are missing a mail utility package…
Command 'mail' not found, but can be installed with: apt install mailutils
Then install mailutils and try again
apt install mailutils
Now there should be no output after the command is re-run, but the email may not arrive due to….. issues.
Try and get an idea by running a tail on the mail log.
tail -f /var/log/mail.log
tail -f /var/log/syslog | grep postfix
Domain Name / FQDN
If your server has an invalid domain name the smtp connection will be rejected – set your server up with a valid domain name.
Firewall Port 25
If your smtp connection is timing out check that you have an outgoing Port 25 enabled, some providers will block this by default.
If your server already has Postfix installed, of which most spun up servers already do you’ll most probably need to reconfigure Postfix with your correct domain name and sending parameters.
Postfix can be reconfigured using the command dpkg-reconfigure postfix, this will present a simple wizard which will ask a few questions.
- Configuration: Internet Site
- System mail name: [server.example.com]
- Root and postmaster mail recipient: [blank]
- Destinations to accept mail for: server.example.com, server, localhost.example.com, localhost
- Force synchronous updates on mail queue? No
- Local networks: 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
- Mailbox size limit (bytes): 0
- Local address extension character: +
- Internet protocols to use: all
After the wizard has finished check/edit 3 values…
myhostname = server.example.com mydestination = server.example.com, localhost.example.com, server, localhost inet_interfaces = loopback-only
inet_interfaces will make sure it only receives email from local machine only, it sets the server to send email and only receive email from the loopback address.
Send Email from Main Domain
masquerade_domains = example.com
If your server has a subdomain or FQDN like server.example.com this will allow you to send email as the main domain, example.com – this directive is not in the main.cf file by default, add it in the end of the file if you want to use it.
postfix stop && postfix start
Restart the Postfix app
Alias System Emails
Set up an alias to receive email sent to the root account
and add …
root: [email protected]
Confirm the change…
Test it works…
echo "Are you getting root's email OK..?" | mail -s "Sending email!" root
You can use a relay if you wish or if you have to because your IP address is blacklisted, there are many solutions with some providers offering a free tier account, 2 popular ones are SendGrid and MailGun.
That’s it, now you can send emails from the command line and receive roots email at another forwarded address.