Table of Contents
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/shpostfix_install(){ cat <<EOF | sudo debconf-set-selectionspostfix postfix/main_mailer_type select No configurationEOF sudo apt install -y postfix # shellcheck disable=SC2016 cat <<EOF | sudo tee /etc/postfix/main.cfmyhostname = ${1}mydomain = ${2}myorigin = $myhostname.$mydomainmydestination = localhost, localhost.$mydomain, $myhostname, $mydomain, $myorigincompatibility_level = 2command_directory = /usr/sbindaemon_directory = /usr/lib/postfix/sbindata_directory = /var/lib/postfixmail_owner = postfixinet_interfaces = alllocal_recipient_maps = unix:passwd.byname $alias_mapsunknown_local_recipient_reject_code = 550mynetworks_style = subnetmynetworks = 127.0.0.0/8alias_maps = hash:/etc/aliasesalias_database = hash:/etc/aliasessmtpd_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 5sendmail_path = /usr/sbin/postfixnewaliases_path = /usr/bin/newaliasesmailq_path = /usr/bin/mailqsetgid_group = postdropinet_protocols = ipv4EOF sudo newaliases sudo apt install -y mutt}postfix_main(){ postfix_install localhost localdomain # localhost only. # postfix_install ${YOUR_HOSTNAME} ${YOUR_DOMAIN} # your network. sudo reboot}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.cfrelayhost = [smtp.gmail.com]:587smtp_sasl_auth_enable = yessmtp_sasl_password_maps = hash:/etc/postfix/sasl_passwdsmtp_sasl_security_options = noanonymoussmtp_sasl_mechanism_filter = plainsmtp_use_tls = yesEOF
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
Reboot system.
$ sudo reboot
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=<[email protected]>,relay=smtp.gmail.com …