Proxmox-NUT Homelab HOWTO - Step 2: Setup email using gmail.smtp.com as relay / Test Email from shell and Proxmox GUI / Test Proxmox startup / shutdown notification

Step 2: Setup email using gmail.smtp.com as relay / Test Email from shell and Proxmox GUI / Test Proxmox startup / shutdown notification

We will set up Gmail as a relay within Debian (the OS on which Proxmox runs). We will look at the options within Proxmox that allow us to configure email communication on this SMTP. We will also configure Debian so that whenever it starts and stops it alerts us with an email.

Generate a Gmail app password setup

 

  1. Log into Gmail, click on the cog and click on "Manage Google Account".
  2. In the search bar, type "App Passwords".
  3. Enter a name that describes where the password is used. This step is very important because this name is the only way to remember where this app password is being used. If you have many app passwords, you can easily recognise which password is used where. Furthermore, if you decide to disengage an SMTP relay all you have to do is delete the app associated with that service.
  4. Copy the generated password into a text editor and remove all spaces.

Setup Gmail as an SMTP relay in Debian

  1. Open a terminal from within Proxmox.
  2. Check for and install any updates
apt update
apt upgrade # if necessary
  1. Install additional modules that are required by Gmail.
apt install -y postfix libsasl2-modules mailutils
  1. Edit /etc/postfix/main.cf, delete or comment out the blank relayhost= entry and append the text below to the file. Save it.
# Use Gmail as relay
relayhost = [smtp.gmail.com]:587
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
smtp_use_tls = yes
  1. Edit the file /etc/postfix/sasl_passwd and insert the following text (replacing <your gmail email> and <your gmail app password> with your values.
[smtp.gmail.com]:587 <your gmail email>:<your gmail app password>
  1. Change the file permissions granting root exclusive read write access.
chmod 600 /etc/postfix/sasl_passwd
  1. Create a hashed database file from the credentials file.
postmap hash:/etc/postfix/sasl_passwd
  1. Restart the postfix service and verify that it is active.


 

systemctl reload postfix
systemctl status postfix
  1. Verify that the solution is working (replace <recipient_email_addr> with your value by sending an email from your Debian terminal and confirming that the <recipient_email_addr> gets it.
echo "This is a test email." | mail -s "Shell Test email" <recipient_email_addr>

 

Testing from within Proxmox

 

  1. Click on Datacenter and from the menu select
  2. Click on mail-to-root and click the Test
  3. Verify that an email has been generated.


 

You can customise the email author and from address that appears in the sent email. To do this go to Notifications, click on mail-to-root and click the Modify button. Check the Advanced checkbox and fill in the Author and From Address fields.

Generating an email when Proxmox (Debian) Starts and Shuts

 (Reboot = Shutdown followed by Startup)

  1. Create a script that will send the startup and shutdown emails. Let’s call this script /usr/bin/bootmail.sh. The action will be passed as a parameter ($1). You may add additional environment variables to suit your needs.
#! /bin/sh
SUBJECT="Proxmox-NUT HOST $1"
echo "For information purposes" | mail -s "${SUBJECT}" <recipient_email_addr>
  1. Make it executable and readable only by root.
chmod 700 /usr/bin/bootmail.sh
  1. Create a service that will be executed when the OS starts and stops. The service will invoke the script created about with the appropriate parameter. The service is called /etc/systemd/system/bootmail.service.
[Unit]
  Description=Run Scripts at Start and Stop
[Service]
  Type=oneshot
  RemainAfterExit=true
  ExecStart=/usr/bin/bootmail.sh startup
  ExecStop=/usr/bin/bootmail.sh shutdown

[Install]
  WantedBy=multi-user.target
  1. Install the service and verify that it is active.
systemctl daemon-reload
systemctl enable bootmail.service
systemctl status bootmail.service
 

  1. Reboot Proxmox and verify that the emails have been generated.

 

Video

  

Links

Setting up Email Notifications in Proxmox Using Gmail
    https://www.naturalborncoder.com/linux/2023/05/19/setting-up-email-notifications-in-proxmox-using-gmail/
Send me an email on computer shutdown
    https://askubuntu.com/questions/851946/send-me-an-email-on-computer-shutdown
Gmail
    https://www.google.com
Run Jobs or Scripts Using Crontab on Boot
    https://www.linode.com/docs/guides/run-jobs-or-scripts-using-crontab-on-boot/



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

Comments

Popular posts from this blog

20150628 Giarratana Circular

HOWTO setup OpenVPN server and client configuration files using EasyRSA

How to clone and synchronise a GitHub repository on Android