Use SMTP Email Service From Google

If you are in need of email services and don't have your own mail server, one of the options you can use as a G Suite customer is Google's SMTP mail service.

The following instructions use Postfix to be the localhost mail delivery daemon:

1. Install Postfix

sudo apt install postfix

2. Configure Gmail Authentication

The best option is to enable Two-Factor Authentication (2FA) on your G Suite account so that you can create App Passwords. These passwords are used in place of your regular G Suite password when configuring Postfix as a relay mail server.

Once you have enabled 2FA for your G Suite account, find the latest instructions from Google's help section on creating App Passwords on your account.

3. Add G Suite User and Password to Postfix

Usernames and passwords are stored in the /etc/postfix/sasl/sasl_passwd file:
  1. Open or create /etc/postfix/sasl/sasl_passwd and add the SMTP host, username, and password accordingly for G Suite:

  2. [smtp.gmail.com]:587 [email protected]:password

  3. Create the sasl_passwd.db file for Postfix

  4. sudo postmap /etc/postfix/sasl/sasl_passwd

  5. Secure the sasl_passwd and sasl_passwd.db files

  6. sudo chown root:root /etc/postfix/sasl/sasl_passwd
    sudo chown root:root /etc/postfix/sasl/sasl_passwd.db
    sudo chmod 0600 /etc/postfix/sasl/sasl_passwd
    sudo chmod 0600 /etc/postfix/sasl/sasl_passwd.db

4. Configure Postfix to be a Relay Server

  1. Modify /etc/postfix/main.cf to use Google's SMTP server

  2. relayhost = [smtp.gmail.com]:587

  3. At the end of the file, add the following parameters to enable authentication

  4. # Enable SASL authentication
    smtp_sasl_auth_enable = yes
    # Disallow methods that allow anonymous authentication
    smtp_sasl_security_options = noanonymous
    # Location of sasl_passwd
    smtp_sasl_password_maps = hash:/etc/postfix/sasl/sasl_passwd
    # Enable STARTTLS encryption
    smtp_tls_security_level = encrypt
    # Location of CA certificates
    smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt

  5. Restart Postfix service

  6. sudo systemctl restart postfix

5. Test Postfix

Use Postfix’s sendmail implementation to send a test email:

sendmail [email protected]
From: [email protected]
Subject: Test mail
This is a test email
.

Comments