Ubuntu – Sendmail not working as desired in bash script

bashscriptssendmail

This is the code I have in a bash script that runs as a cronjob. The cronjob run as root.

   /usr/sbin/sendmail myemail@email.com<<EOF
   subject:Backup Error!
   from:backup@server01
   $error
        EOF

There is code after this and the email I get is as follows:

From the root user on the machine.

and the message includes:

   subject:Backup Error!
   from:backup@server01
   $error
        EOF
   More code...
   that is in the script
   all the way to the end...

I have tried other variations, this is the closest I've got. I tried this in a regular script and it worked properly. Whats going on, and how can I send this email, specifying the subject and form sender?

Best Answer

The previous answers have already mentioned:

  • bad end of "here document" (END)
  • missing empty line between headers and body

Additional fixes:

  • -i command line option to stop special treatment for lines starting with dot
  • -- to separate command line options from addresses of recipients (good style recomendation)

Corrected script:

/usr/sbin/sendmail -i -- myemail@email.com <<EOF
subject: Backup Error!
from:backup@server01

$error
EOF
Related Question