0

I've recently got Postfix working as a send only MTA relaying through Google Workspace (formerly Google G Suite). I would now like to send emails after completed cron jobs to my personal email. The problem I initially faced was that my server was sending out two emails, one to personal-email@hotmail.com and the other to root@example.com with the latter bouncing back a MAILER-DAEMON reply as this account doesn't exist.

This was my initial cron job located at /etc/cron.d/google-drive-backup:

59 11 * * * root /root/scripts/google-drive-backup.sh && mail --append="From:Root <no-reply@example.com>" --append="Reply-To:webmaster@example.com" personal-email@hotmail.com

I did research on these forums and came across suppressing the output by using >/dev/null 2>&1 which works if I place it directly after the script:

59 11 * * * root /root/scripts/google-drive-backup.sh >/dev/null 2>&1

I can confirm no emails are being sent as the mail log at /var/log/mail.log is empty. However, suppressing the output has no affect when using it in conjunction with the mail command:

59 11 * * * root /root/scripts/google-drive-backup.sh >/dev/null 2>&1 && mail --append="From:Root <no-reply@example.com>" --append="Reply-To:webmaster@example.com" personal-email@hotmail.com

or

59 11 * * * root /root/scripts/google-drive-backup.sh && mail --append="From:Root <no-reply@example.com>" --append="Reply-To:webmaster@example.com" personal-email@hotmail.com >/dev/null 2>&1

Essentially I would like to disable crontab's default email feature and use sendmail so that I can customise the subject and email headers.

willowen100
  • 21
  • 2
  • 8
  • 1
    You can redirect *stderr* to *stdout* and still pipe that mix to mail, like this: `command 2>&1 | mail recipient` - the `fd>&fd` syntax is not limited to cases where you plan to discard output! – anx Nov 09 '20 at 00:29
  • @anx How do I drop the output altogether? I want to only send an email if the script completes without any errors, but I don't want the output from the script to be in the message body of the email which your example does. – willowen100 Nov 11 '20 at 12:28
  • Does this answer your question? [How to change "From:" field for emails from Cron?](https://serverfault.com/questions/121121/how-to-change-from-field-for-emails-from-cron) – Gerald Schneider Nov 27 '20 at 09:02

1 Answers1

0

I ended up suppressing the stdout and stderr to a null file and then using two ampersands to create a subject with the echo command that pipes into the mail command to complete the rest. Here is my finished cron job:

59 11 * * * root /root/scripts/google-drive-backup.sh >/dev/null 2>&1 && echo "Cron job has finished at $(date +'\%d/\%m/\%Y') $(date +'(\%H:\%M:\%S)')" | mail --subject="WordPress Backup Complete" --append="From:Root <webmaster@example.com>" --append="Reply-To:webmaster@example.com" personal-email@yahoo.co.uk

willowen100
  • 21
  • 2
  • 8