In many cases it's easier to configure the Postfix mail transfer agent (MTA) to use an external SMTP server as a mail relay instead of hosting your own email server. One such SMTP server is GMail, plus it's free!
Install Postfix
To start, let's install Postfix and MailUtils. MailUtils is a set of utilities and daemons for processing e-mail:
$ apt-get update && apt-get install postfix mailutils
When you are prompted to select the mail server configuration type, select Internet Site. Next you will be prompted for a Mail Name. If you have a domain that resolves to your server's IP address, enter that domain. Otherwise, use your server's host name.
If you already have Postfix installed, or you don't see the prompts, use the following command to reconfigure Postfix:
$ sudo dpkg-reconfigure postfix
If you are asked for a user account to which mail for 'postmaster', 'root', and other system accounts will be redirected, enter root
or another permanent user.
Select the default option for all other configuration items.
Allow Less Secure App Access
In order for GMail to accept your connection, you need to allow less secure app access. You can find instructions here:
https://support.google.com/accounts/answer/6010255
Configure GMail Authentication
Postfix requires a special password file to authenticate, so let's create this now. The Postfix configuration directory is located at /etc/postfix/
, so let's create the authentication file at /etc/postfix/sasl_passwd
.
Include the following content in this file:
[smtp.gmail.com]:587 your_user@gmail.com:your_password
Obviously, replace your_user@gmail.com
and your_password
with your GMail email and password, respectively. If you use GSuite, be sure to use your Google Apps domain instead of gmail.com
.
When you're done, let's close the file and change its permissions, then hash the file for security purposes:
$ chmod 600 /etc/postfix/sasl_passwd
$ postmap /etc/postfix/sasl_passwd
Configure Postfix
Let's edit the Postfix configuration file, located here:
/etc/postfix/main.cf
Edit the file to contain these configuration items:
relayhost = [smtp.gmail.com]:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_security_options =
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
Afterwards, restart Postfix and we're all set!
$ systemctl restart postfix.service
To test everything is working, let's send a test email:
$ mail -s "Test Message" recipient@domain.com
As a note, Postfix will use the machine's account name as the email sender's name. You can view the name assigned to your account via the /etc/passwd
file, or change the sender's name via custom headers with the -a
flag:
$ mail -a "From: Someone <someone@example.com>" \
$ -a "Subject: This is a test" \
$ -a "X-Custom-Header: yes" other@example.com
Notice the \
, which continues the command onto a new line.
If you encounter any issues you can find the log files at /var/log/mail.log
, or feel free to drop a comment.