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?

  • Does the job actually produce any output ? – Iain Feb 24 '16 at 13:01
  • Yes definitely, and I receive the output through mail if I put the MAILTO back in place. – rewb0rn Feb 25 '16 at 9:04
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>"
  • I tried it, but for me the output is still empty. What mail package are you using? – rewb0rn Feb 25 '16 at 9:03
  • Mail package is mailx-12.4-8.el6_6.x86_64. – xs2rashid Feb 26 '16 at 10:58
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>"

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

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.