There is nothing more upsetting than email. It’s an old technology, and no one should use it. You know it, I know it, everyone knows it.
It’s just too bad the entire world practically revolves around email. So I’m going to help you with an issue I ran into recently. Hopefully this saves some hair-pulling.
Today, I’ll be explaining how to install the Sendmail like application Postfix in conjunction with Mailutils to allow you to send email from your web application. This particular guide will also solve the issue of your web application server not sending to an external mail server on the same domain.
The example I’ll be using will relate to an Amazon Web Service Elastic Cloud Compute (AWS EC2) instance sending mail to GSuite. I know that there are other (likely better) ways of sending mail like AWS SES (Simple Email Service), GSuite SMTP-Relay, Mailgun, or Mandrill. However, we’re going to go purist on this first try and have the Application Server do the entire job.
Server Setup and Installing Mailutils & Postfix
I’ll be removing Sendmail as Postfix will be replacing it. You can run
sudo apt purge sendmail if you would like to remove sendmail with it’s configuration. If you only want to remove sendmail but keep its configuration run:
sudo apt remove sendmail.
Next, you’ll want to specify the server hostname and change your hosts file if you haven’t yet.
sudo vim /etc/hostname
Remove the current hostname and replace with your domain. (eg. yourdomain.com)
sudo vim /etc/hosts
Change the first line in your host file to reflect the following:
127.0.0.1 yourdomain.com localhost
Once that is complete, give your instance a restart to have it reflect the changes.
Next we’ll need to make sure we have all the utilities and packages we’ll be using installed. I’m told that installing Mailutils will also install Postfix and prompt the setup steps, but in my case, it did not.
sudo apt install mailutils sudo apt install postfix
Either mailutils will prompt you to set up postfix (a pink background fullscreen prompt) or installing postfix itself will.
You’ll want to specify the the
configuration type as
Internet Site. You’ll then want to set the
System mail name: to
The next step will be editing your Postfix main configuration file.
sudo vim /etc/postfix/main.cf
You’ll want to change these lines:
.... mydestination = $myhostname, yourdomain.com, localhost.yourdomain.internal, localhost .... inet_interfaces = all ....
.... mydestination = .... inet_interfaces = loopback-only ....
Once you’re done, save your file and restart Postfix.
sudo service postfix restart
Make sure your postfix is running without errors using
sudo systemctl status postfix. If you run into any errors, give your server a reboot. This is an important note. I was running into issues with port 25 having already been bound.
You can now send a test email by running:
echo "This is some email body text" | mail -s "This is a test email subject" email@example.com
Make sure to test it on your own domain, and you should be good to go!
Notes About Spam
You’ll want to make sure of a few things when sending email from multiple sources. One of the most important, and easiest spam-securing technologies available is the humble SPF record.
By adding a TXT record to your Domain Records, you can specify which servers are allowed to send mail, and what to do when something is not validated.
A simple example of this would be:
v=spf1 a mx include:_spf.example.com ip4:999.88.777.66 -all
This SPF record effectively validates A records, MX records, includes, and IP addresses to be able to send mail as an authorized domain sender. If your web application sits at your A record, you’re done. If not, add it with an
ip4: tag and call it a day!
Hope you enjoyed!