Last Updated on December 24, 2022 by Thiago Crepaldi
Everytime we spawn a new server, it is a good idea to enable email services on it so that they can a means to reach us and notify of important events, such as software or hardware failures. In this post we are going to enable configure E-mail on Linux distros using postfix. I will use Ubuntu as an example, but it should work for most distros with minor path changes to postfix installation dir. The email server will be backed my GMail, which also works if you have Google Suite and a custom domain, such as yourdomain.com!
Install OS dependencies
The first step is to install a library that will be used to support GMail authentication. Go to Server view >> Datacenter and select your server. Next, click at Shell button
# apt update # apt install -y postfix libsasl2-modules
Optional: Create app password for 2FA GMail accounts
If your GMail account has 2FA configured, go to App Passwords and generate a new password just for Proxmox
- Select app: Mail
- Select device: Other
- Select the app and device you want (…): Proxmox
Click on Generate and take note of this password
Setting GMail credentials
Now that all dependencies are installed and you have your password, let’s do the actually setting. Make adjustment to the snippet below to add your actual email and password separated by a colon.
# echo "smtp.gmail.com email@example.com:yourpassword" > /etc/postfix/sasl_passwd # postmap hash:/etc/postfix/sasl_passwd # chmod 600 /etc/postfix/sasl_passwd
With your favorite editor, hopefully vim or nano, edit Postfix configuration file.
# vim /etc/postfix/main.cf
Append the following to the end of the file
relayhost = smtp.gmail.com:587 smtp_use_tls = yes smtp_sasl_auth_enable = yes smtp_sasl_security_options = smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd smtp_tls_CAfile = /etc/ssl/certs/Entrust_Root_Certification_Authority.pem smtp_tls_session_cache_database = btree:/var/lib/postfix/smtp_tls_session_cache smtp_tls_session_cache_timeout = 3600s
Before saving the config file, also comment out the existing “relayhost =” line by updating it to “# relayhost =”. This is needed because the snippet you appended already sets this line to Google’s email and we can’t have duplicate settings.
At this point, all configuration is done. Just reload postfix and test it
# postfix reload # echo "my msg" | mail -s "my subject" firstname.lastname@example.org postfix/postfix-script: refreshing the Postfix mail system
For the test email, you have to use a destination that is different than the email account you used to configure the server. If everything went well, you should get the email in your inbox in a couple seconds.