I recently tried to create a simple systemd service to send an email notification, but my initial approach with mail
and sendmail
failed with a strange permission error.
My original service file looked like this:
[Service]
ExecStart=mail --subject=Subject recipient@example.com
The error message was a bit of a head-scratcher: warning: mail_queue_enter: create file maildrop/....: Permission denied
.
A quick search pointed me to the cause: the postdrop
binary has setgid. However, the systemd setting NoNewPrivileges=true
prevents this.
While I hadn't explicitly used that setting, I was using DynamicUser=true
, which implies and enforces NoNewPrivileges=true
. This meant my service, running as a temporary user, couldn't get the permissions it needed to interact with the mail queue. Note that this implication cannot be disabled/overriden.
I wanted to avoid creating a new, dedicated user for this task. I realized that the problem was how mail
and sendmail
directly interact with the mail queue. The solution was to bypass that entire process and talk directly to the local SMTP server.
I didn't want to install another dedicated SMTP client. Fortunately, I learned that the curl
can also act as an SMTP client! This command worked perfectly, sending the email by directly:
curl --url smtp://localhost:25 --mail-rcpt recipient@example.com --upload-file body.txt
Comments