Tuesday, January 7, 2025

Sending email from Proxmox using postfix and gmail

Proxmox (https://www.proxmox.com/) is my reference hypervisor since a couple of years.

One of the thing I was missing is sending email from it, so I decide to setup postfix (https://www.postfix.org/) to do this.

The smtp mail service I've used for this is Google gmail. But you can use any other smtp server.

All the commands shown below has to be run from the node shell.

First you have to install some packages:

apt install -y libsasl2-modules mailutils rsyslog

Then you have to add a password file

nano /etc/postfix/sasl_passwd

Inside the file we are going to put the relay host, a username, and a password. As for the password, you are using google gmail you have to enable 2-Step-Verification and then add an App Password, find the guide here: https://support.google.com/accounts/answer/185833?hl=en
We are going to use the created app password for our credential. Note that this password usually contains spaces that we must remove on the sasl password file.

The content of the file should look something like this, here you should replace with your credentials

[smtp.gmail.com]:587 your_username@gmail.com:your_app_password_without_spaces

You then have to set permissions on this file, like so

chown root:root /etc/postfix/sasl_passwd
chmod 600 /etc/postfix/sasl_passwd

Now, and anytime you change this file you are going to build a new has using the postmap command, so run the following command:

postmap hash:/etc/postfix/sasl_passwd

Then we have to add a couple of file more, to replace the sender with your actual username, otherwise the smtp server will block your outgoing emails.

So add the sender file

nano /etc/postfix/sender_canonical_maps

and fill it with content:

/.+/    your_username@gmail.com

Then add the replace header file:

nano /etc/postfix/header_check

and fill it with content:

/From:.*/ REPLACE From: your_username@gmail.com

Now we can finally edit the mail configuration file

nano /etc/postfix/main.cf

We have to comment out the relayhost actual line

#relayhost =

And add the following lines at the bottom of the file

relayhost = [smtp.gmail.com]:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_security_options = noanonymous
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
sender_canonical_classes = envelope_sender, header_sender
sender_canonical_maps =  regexp:/etc/postfix/sender_canonical_maps
smtp_header_checks = regexp:/etc/postfix/header_check

Another thing you should change here, is to update the compatibility level from 2 to 3.6, this is not mandatory

compatibility_level = 3.6

We are now done, we can now restart postfix

systemctl restart postfix

And we should be able to send a test email by using the command:

echo "sample message" | mail -s "sample subject" recipient@email.com

If you want to look at the last 50 files of the postfix log file, just run

tail -n 50 /var/log/mail.log

Notes

  • read risk disclaimer
  • excuse my bad english



No comments:

Post a Comment