Ubuntu 16.04: Install Postfix for SMTP server

This article will describe installing Postfix as SMTP server and send mail to localhost and your domain. And then send mail to internet via Gmail.

1 Send mail to localhost and your domain

  • If you send mail to only localhost, set myhostname = localhost and mydomain = localdomain.
  • If you send mail to your domain, set your hostname to myhostname and your domain to mydomain. This hostname and domain must be known with multiple machine (common /etc/hosts or DNS server). Both mail sender machine and receiver machine needs this Postfix setting.

The following script will set myhostname = localhost and mydomain = localdomain.

#!/bin/sh

postfix_install()
{
  cat <<EOF | sudo debconf-set-selections
postfix postfix/main_mailer_type select No configuration
EOF
  sudo apt install -y postfix

  # shellcheck disable=SC2016
  cat <<EOF | sudo tee /etc/postfix/main.cf
myhostname = ${1}
mydomain = ${2}
myorigin = \$myhostname.\$mydomain
mydestination = localhost, localhost.\$mydomain, \$myhostname, \$mydomain, \$myorigin
compatibility_level = 2
command_directory = /usr/sbin
daemon_directory = /usr/lib/postfix/sbin
data_directory = /var/lib/postfix
mail_owner = postfix
inet_interfaces = all
local_recipient_maps = unix:passwd.byname \$alias_maps
unknown_local_recipient_reject_code = 550
mynetworks_style = subnet
mynetworks = 127.0.0.0/8
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
smtpd_banner = \$myhostname ESMTP \$mail_name (Ubuntu)
debugger_command =
         PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin
         ddd \$daemon_directory/\$process_name \$process_id & sleep 5
sendmail_path = /usr/sbin/postfix
newaliases_path = /usr/bin/newaliases
mailq_path = /usr/bin/mailq
setgid_group = postdrop
inet_protocols = ipv4
EOF

  sudo newaliases
  sudo systemctl restart postfix
  sudo apt install -y mutt
}

postfix_main()
{
  postfix_install localhost localdomain             # localhost only.
  # postfix_install ${YOUR_HOSTNAME} ${YOUR_DOMAIN} # your network.
}

postfix_main

2 Send mail to internet via Gmail

You need the previous Postfix setting. The myhostname and mydomain can be localhost setting or your domain setting.

Append the following to /etc/postfix.main.cf.

$ cat <<EOF | sudo tee -a /etc/postfix/main.cf
relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_sasl_mechanism_filter = plain
smtp_use_tls = yes
EOF

Create /etc/postfix/sasl_passwd. Please set your Gmail address to GMAIL_ADDR and your Gmail password to GMAIL_PASSWD.

$ echo "[smtp.gmail.com]:587 ${GMAIL_ADDR}:${GMAIL_PASSWD}" | \
sudo tee /etc/postfix/sasl_passwd
$ sudo chmod 600 /etc/postfix/sasl_passwd
$ sudo postmap hash:/etc/postfix/sasl_passwd

Restart Postfix.

$ sudo systemctl restart postfix

Gmail will return authentication error.

postfix/smtp ... SASL authentication failed; server smtp.gmail.com...

The 2-step verification and application password is better. But this article will use "Allow less secure apps: ON".

https://myaccount.google.com/lesssecureapps

If Gmail returns authenticaion error again, you need "Allow access to your Google account".

https://accounts.google.com/DisplayUnlockCaptcha

And then you can send mail to internet via Gmail. Mail's from address is your Gmail address.

postfix/smtp ... to=<yourgmail@gmail.com>,relay=smtp.gmail.com ...