msmtp cannot write to /var/log/msmtp/msmtp.log

In /etc/msmtprc I added credentials for a gmail account and also specified a log file to use in /var/log/msmtp/msmtp.log

I also created the folder and file in /var/log/msmtp/msmtp.log

When I test it using: echo "This is a test e-mail from my server using msmtp!" | msmtp ******@gmail.com

I receive an email but in the terminal I also see an error message like so: msmtp: cannot log to /var/log/msmtp/msmtp.log: cannot open: Permission denied

The current permissions for /var/log/msmtp are:

drwxr-xr-x  2 root      root        4096 Jan 29 20:19 msmtp

What am I doing wrong? What do the permissions on the log file need to be for msmtp to be able to write to the file?


I had the same error message, and ultimately changing permissions, creating the log file, etc., didn't work. The problem in my case was caused by AppArmor: in my system, the file /etc/apparmor.d/usr.bin.msmtp only listed /var/log/msmtp as write permission in /var/log, so solution can be:

  • to use /var/log/msmtp as log in the configuration, instead of /var/log/msmtp.log
  • modify the rules in the msmtp apparmor profile to allow adding and locking the file we want.

After installing msmtp there is an msmtp user and group. If you change the ownership of the logfile to msmtp, and change rights to read/write for user and group, the logging works.

groups msmtp
sudo touch /var/log/msmtp
sudo chown msmtp:msmtp /var/log/msmtp
sudo chmod 660 /var/log/msmtp

I haven't tested it, but I suppose this works for /var/log/msmtp/msmtp.log as well, if you set the rights for the folder properly.