[TUTORIAL] msmtp for sending pve notification mails

srfftc

Member
Mar 18, 2020
4
2
23
39
My mailprovider was rejecting my pve mails. I didn't get it working with postfix. So I tried msmtp and found a working config.

EDIT/Attention: Logfile needs to be at /var/log/msmtp because otherwise AppArmor profile will deny msmtp to write to it.

This is not the most secure configuration. See links at bottom for better ways to store passwords etc.

Step 1 Installation msmtp


Bash:
apt install msmtp msmtp-mta
this replaces postfix as mta

Step 2 add systemwide msmtp config

/etc/msmtprc

Code:
defaults
port 587
tls on
auth on
logfile /var/log/msmtp

account youraccount
host smtp.yourprovider.com
from youremail@yourprovider.com
user youruser
password yourpassword

account default : youraccount

Step 3 create log writeable for everyone

Bash:
groups msmtp
touch /var/log/msmtp.log
chown msmtp:msmtp /var/log/msmtp
chmod 660 /var/log/msmtp

thanks to Astraea for updating step 3

Step 4 set E-Mail from in PVE Webinterface to a valid E-mail address

Set a proper From E-Mail address in there. Otherwise it wont work. PVE ignores /etc/aliases

email.png

Step 5 Testing

I testet my config by starting the daily update task systemctl start pve-daily-update.service


Links for more config (Google Account etc., secure config) with msmtp:
- https://wiki.archlinux.org/index.php/msmtp
 
Last edited:
  • Like
Reactions: lukasszz and fitful
Thank you for sharing your knowledge! If you edit your post, there is a dropdown to change its prefix next to the title field. You can change it to "Tutorial" to make it a little more visible in the thread overview.
 
I just tried this setup on a test VM and managed to get it all working except for writing to the logs. I had to execute the following additional commands to get it to write to the log:

Code:
groups msmtp
touch /var/log/msmtp.log
chown msmtp:msmtp /var/log/msmtp.log
chmod 660 /var/log/msmtp.log


My only question now is do I need to do this to each node or only to the node the cluster was created on?
 
I just tried this setup on a test VM and managed to get it all working except for writing to the logs. I had to execute the following additional commands to get it to write to the log:

Code:
groups msmtp
touch /var/log/msmtp.log
chown msmtp:msmtp /var/log/msmtp.log
chmod 660 /var/log/msmtp.log


My only question now is do I need to do this to each node or only to the node the cluster was created on?
Yeah steps 1-3 need to be done on a per host basis
 
Do you know what notifications you can expect from Proxmox VE, I tried to look it up but didn't find much, so far the only notification I get is when a backup completes and or fails depending on what I specified.
 
Do you know what notifications you can expect from Proxmox VE, I tried to look it up but didn't find much, so far the only notification I get is when a backup completes and or fails depending on what I specified.
+1 to your question, I'd like to know too.
 
My mailprovider was rejecting my pve mails. I didn't get it working with postfix. So I tried msmtp and found a working config.

EDIT/Attention: Logfile needs to be at /var/log/msmtp because otherwise AppArmor profile will deny msmtp to write to it.

This is not the most secure configuration. See links at bottom for better ways to store passwords etc.

Step 1 Installation msmtp


Bash:
apt install msmtp msmtp-mta
this replaces postfix as mta

Step 2 add systemwide msmtp config

/etc/msmtprc

Code:
defaults
port 587
tls on
auth on
logfile /var/log/msmtp

account youraccount
host smtp.yourprovider.com
from youremail@yourprovider.com
user youruser
password yourpassword

account default : youraccount

Step 3 create log writeable for everyone

Bash:
groups msmtp
touch /var/log/msmtp.log
chown msmtp:msmtp /var/log/msmtp
chmod 660 /var/log/msmtp

thanks to Astraea for updating step 3

Step 4 set E-Mail from in PVE Webinterface to a valid E-mail address

Set a proper From E-Mail address in there. Otherwise it wont work. PVE ignores /etc/aliases

View attachment 20799

Step 5 Testing

I testet my config by starting the daily update task systemctl start pve-daily-update.service


Links for more config (Google Account etc., secure config) with msmtp:
- https://wiki.archlinux.org/index.php/msmtp
This worked flawlessly and super easy! Thank you!

A couple of comments if you allow me:

In step 3 where it reads:
touch /var/log/msmtp.log
should say:
touch /var/log/msmtp

Also you should avoid creating a log file other than /var/log/msmtp because you'll run into problems with apparmor.

And the other one, in step 5, I think that test only works if you have a subscription, otherwise you can just setup an email in a backup job with notification enabled and wait, or you can also test from the command line with something like:

echo -e "Subject: MySubject\r\n\r\ntestmail" |msmtp youremailaddress@yourserver.com

I hope this helps someone!
 
I have been doing some tinkering and updating my notes for Proxmox VE - 7.2 and wanted to share my latest iteration of this deployment that can either be done as a script or as a set of terminal commands:

Bash:
apt update; \
apt install msmtp -y; \
apt install msmtp-mta -y; \

echo ""; \
echo ""; \
echo "Starting E-Mail Notification Configuration...."; \
echo ""; \

printf "defaults\n" >> /etc/msmtprc; \
printf "   port 587\n" >> /etc/msmtprc; \
printf "   tls on\n" >> /etc/msmtprc; \
printf "   tls_starttls on\n" >> /etc/msmtprc; \
printf "   auth on\n" >> /etc/msmtprc; \
printf "\n" >> /etc/msmtprc; \

printf "logfile /var/log/msmtp\n" >> /etc/msmtprc; \
printf "\n" >> /etc/msmtprc; \

printf "account outbound-mail\n" >> /etc/msmtprc; \

echo -n "Enter a mail server's FQDN: "; \
read VAR1; \
printf "   host $VAR1\n" >> /etc/msmtprc; \
printf "\n" >> /etc/msmtprc; \
unset VAR1; \

echo -n "Enter FROM address: "; \
read VAR2; \
printf "   from $VAR2\n" >> /etc/msmtprc; \
printf "\n" >> /etc/msmtprc; \
unset VAR2; \

echo -n "Enter Username: "; \
read VAR3; \
printf "   user $VAR3\n" >> /etc/msmtprc; \
unset VAR3; \

echo -n "Enter Password: "; \
read VAR4; \
printf "   password $VAR4\n" >> /etc/msmtprc; \
printf "\n" >> /etc/msmtprc; \
unset VAR4; \

printf "account default : outbound-mail" >> /etc/msmtprc; \
echo ""; \
echo "Settings Applied."; \
echo ""; \
echo ""; \

groups msmtp; \
touch /var/log/msmtp; \
chown msmtp:msmtp /var/log/msmtp; \
chmod 660 /var/log/msmtp; \

echo ""; \
echo ""; \
echo -n "Enter an email address: "; \
read VAR5; \
echo -e "Subject: MySubject\r\n\r\ntestmail" | msmtp $VAR5; \
unset VAR5;

This newest version does the installation, and configuration all at once and even sends a test email when finished. All of the required parameters are set in the commands or asked for from the user on the command line. I have also updated the corrections from @Scar_UY into this iteration as well.

You will still need to have an email address set in the corresponding location on the WebUI as this is not handled in the commands.
 
Last edited:
  • Like
Reactions: meisen

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!