Please note: I did my research and experimentation, and I cannot solve this problem; there are many partial solutions that don't address all my points. Don't dismiss this question quickly.
Scenario: Ubuntu Linux 20.04, server edition.
- I want to get e-mail notifications to an external address for ALL failures in cron jobs;
- I don't control all cron jobs files. Sometimes it's a package that puts things in /etc/cron.$something$/ and I don't want to edit/update/tinker with such files. But I still want to get an e-mail if something bad happens. So, I cannot set "MAILTO" in my jobs.
- I want to use an external SMTP server (I'm currently using AWS SES in this specific instance) to relay e-mail messages, because the host IP address of my server will rarely (if ever) be accepted as a "good" IP for sending e-mail.
- The server is fairly small; Ideally, I'd like to use a daemonless approach to send mail; configuring a big, full-fledged SMTP server (e.g. OpenSMTPD?) to just relay internal e-mail seems a bit out of scope to me; I COULD do that if I found confirmation that it's possible to do what I'd like to.
I have tried with different combinations of configurations of MTAs like ssmtp/msmtp and I found that no-one could solve all the problems together; in fact:
- Ubuntu's
cron
implementation seems to hardcode/usr/lib/sendmail
and the required args into the source code, so I'm stuck with sendmail emulation. - ssmtp with sendmail emulation does a correct processing of reverse aliases (local user as sender to an arbitrary user -> e.g. sending from root= From: myuser@mydomain.com). This is crucial because AWS SES requires a registered, verified sender in order to allow e-mail to be sent. But ssmtp via sendmail emulation does not support 'direct' aliases (rewriting the 'To:', so if cron tries to send an e-mail to "root", rewrite won't happen and sending will fail. If using the "mail" CLI it WOULD work (since it takes care of aliases), but there's no way to set such executable in cron.
- When using msmtp, 'direct' aliases work, but there's no support for reverse aliases; so rewriting from "root" to "user@mydomain.com" doesn't happen and the SMTP server rejects the e-mail.
- There seems no way to set a global "MAILTO" for cron jobs; it's required for each job to have its own variable (and, as I said, I don't want to edit system-provided jobs).
Recap:
I want a way to get an e-mail for ALL THE ERRORS in cron jobs for ANY USER in the system to a target errors@example.com
mailbox, even cronjobs that I haven't written and that I don't want to modify, and I need to use an external SMTP server which performs validation on the "From" field.
I'm open to any kind of solution, as long as I don't have to recompile essential system packages like cron
. And it must be totally unattended - no babysitting.
/etc/pam.d/cron
appears to requirepam_env.so
for the session, which implies that it should respect aMAILTO
variable set in/etc/environment
- but I must admit I haven't investigated it – steeldriver Apr 27 '21 at 11:37