1

I am tempering with my cronjobs to change the sender and subject for emails sent by the cronjob. For sending emails I installed the mailutils package on Ubuntu 14.04.

This is how I am sending my email from the cronjob: 0 3 * * * /root/bin/backup.sh 2>&1 | mail -s "Slave1 Backup" -a "From:Slave1<slave3@example.com>" cronjob@example.com

Source: How to change "From:" field for emails from Cron?

However this sends me an empty email. The backup script ran because I can see that all the files are up to date, however the email contains no text.

If I change it back to this, the emails contains all the output from the backup script as I want to: MAILTO: cronjob@example.com 0 3 * * * /root/bin/backup.sh

What do I have to change so that mailutils will send the output from the cronjob?

rewb0rn
  • 27
  • 1
  • 7

3 Answers3

0

Cronjob looks fine, i just changed the place of one parameter in mail utility and it starts sending email with script output.

0 3 * * * /root/bin/backup.sh 2>&1 | mail -s "Slave1 Backup" cronjob@example.com -a "From:Slave1<slave3@example.com>"
xs2rashid
  • 184
  • 5
0

It seems like this was more a problem with my mail client than with the cronjob itself. I noticed that Thunderbird would show the email text correctly in the preview notification that pops up when you receive an email with Thunderbird open.

However when clicking on the email inside Thunderbird itself, it was empty and contained no text. So I figured it might be related to an empty HTML content. I could fix this by changing the headers parameter in mailutils to -a "Content-type: text/html;From:Slave3<slave3@spiele-palast.de>"

rewb0rn
  • 27
  • 1
  • 7
0

Add the -E argument to the mail command, which means "do not send any messages if the body is empty".

Therefore, change the crontab line to:

0 3 * * * /root/bin/backup.sh 2>&1 | mail -E -s "Slave1 Backup" -a "From:Slave1<slave3@example.com>" cronjob@example.com

In my system (Arch Linux), the -a parameter doesn't seem to change the From: address. Perhaps the crontab line needs to be changed to this instead:

0 3 * * * /root/bin/backup.sh 2>&1 | mail -E -s "Slave1 Backup" -Sfrom='Slave1 <slave3@example.com>' cronjob@example.com

EDIT: Actually, my mail executable comes from s-nail package. The -E argument is not supported by GNU mailutils, so my solution doesn't work.

You can use heirloom-mailx instead of GNU Mailutils. Install the heirloom-mailx package and modify the crontab line to:

 0 3 * * * /root/bin/backup.sh 2>&1 | heirloom-mailx -E -s "Slave1 Backup" -Sfrom='Slave1 <slave3@example.com>' cronjob@example.com