Self hosting an email server is generally a bad idea. I’d recommend using Google Apps gmail service for hosting one. And DigitalOcean has a button which will setup the required network settings too! However, for sending email it’s sometimes pretty useful, we’ll setup sendmail for sending only.
sudo apt-get install mailutils
Choose “Internet Site” when prompted. And enter your domain name as the system mail name.
If it shows a subdomain (test.example.com) just delete it so it reads the main domain name (example.com).
Let’s quickly tweak the settings.
sudo nano /etc/postfix/main.cf
Find the line:
inet_interfaces = all
It was near the bottom in my version. Change this to read:
inet_interfaces = localhost
Save the file and restart the service.
sudo service postfix restart
We can test is this configuration works by running a simple command (substitute your email address).
echo "Test email body" | mail -s "Test email subject" firstname.lastname@example.org
It might be worth checking the junk mail too!
At this point you’re done, you might consider forwarding local system mail to your email address using the
/etc/aliases file. It’s also worth setting up an SPF record to prevent spoofing and spam. However, the best option is to outsource it to Google!