Install and configure Postfix (using Gmail as SMTP relay)


Install packages


apt update
apt install -y postfix libsasl2-modules mailutils

(Installs Postfix, the SASL modules required for authentication, and mailutils so you can send a messages from the command line.)

During installation a dialogue will appear --- select Internet Site.



(This configures Postfix for sending/receiving mail from the server; we will use a relayhost so outgoing mail is forwarded via Gmail.)

Edit Postfix configuration


nano /etc/postfix/main.cf

Add or update these lines:


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_tls_security_level = encrypt
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt

(These settings tell Postfix to relay via Gmail on port 587, enable SASL authentication using the /etc/postfix/sasl_passwd map, require TLS and use the system CA bundle.)

Create an app password on Google

Go to your Google Account (https://accounts.google.com), sign in and search for App passwords. Ensure 2-Step Verification is enabled for the account first. Create an app password, give it a descriptive name so you'll remember its purpose later, then copy the generated password into a text editor and remove any spaces.

Create the credentials file


sudo nano /etc/postfix/sasl_passwd

Add this line (replace with your actual Gmail SMTP username and the app password):


[smtp.gmail.com]:587 smtpemail@addr.com:smtpemail_pass

(This file maps the relay host to the credentials Postfix should use.)

Secure the credentials and update the hash map


sudo chmod 600 /etc/postfix/sasl_passwd
sudo postmap /etc/postfix/sasl_passwd
sudo systemctl reload postfix

(chmod 600 restricts file access to root, postmap generates /etc/postfix/sasl_passwd.db used by Postfix, and reloading Postfix applies the new configuration without stopping the service.)

Test email delivery


# replace email@addr.com with your email address
echo "Test email from $(hostname)" | mail -s "Test Alert" email@addr.com  

(Sends a simple test e-mail with the subject "Test Alert" to verify delivery via Gmail.)

 


 

Follow This, That and (Maybe), the Other:

<



Comments

Popular posts from this blog

How to clone and synchronise a GitHub repository on Android

The complete guide to installing, configuring, and managing Plex Media Server on an Ubuntu Server