Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

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
share|improve this answer
add comment

I suggest you to not use any whitespace character in front or after the second limit string EOF. If you use just only one space in front or after the limit string, that special-purpose code block will never work. So, use only:

EOF

And you should read some bash scripting guides in this sense like Chapter 19. Here Documents.

share|improve this answer
add comment

Sendmail (and email in general, it seems) requires a blank line between headers and body. The following example worked.

$ sendmail oli@mydomain.com <<EOF
> subject: testing
> from: oli2@mydomain.com
> 
> This is my body!
> EOF

(Obviously pasting that in with the >s in there won't work, just alter what you're doing so that there's a blank line between your headers and error message).

share|improve this answer
    
So simple. Thanks –  dan08 Oct 24 '13 at 14:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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