Sendmail is not working with crontab (bash)

12,803

Solution 1

You need a blank line between the message header and the body.

{
    echo "Subject: $SUBJECT"
    echo "$(< sendmail_list.txt)"
    echo "MIME-Version: 1.0"
    echo "Content-Type: text/html"
    echo "Content-Disposition: inline"
    echo ""
    cat $CONTENT
} | /usr/sbin/sendmail -t 

A couple of other things:

  • you don't need a subshell here, so I changed the surrounding parentheses to braces
  • since this is bash, there's a shorthand for $(cat file) -- $(< file)

Solution 2

Please add /usr/sbin before sendmail in sh file:

/usr/sbin/sendmail "[email protected]" < file.txt 

Hope it helps https://stackoverflow.com/editing-help

Share:
12,803
KnowledgeSeeeker
Author by

KnowledgeSeeeker

Updated on June 26, 2022

Comments

  • KnowledgeSeeeker
    KnowledgeSeeeker almost 2 years

    I have created one disk cleanup script which after cleanup sends a status email. now when I run this through command line, it executes perfectly but through cronjob its not able to send staus mail rest the script is working fine though. I have read many solutions in google but nothing is working for me. I am using Bash on my Ubuntu machine. here is sendmail part of my script.

    export CONTENT="/root/cleanup/cleanup.htm"                                           
    export SUBJECT="Disk Space Clean Up Process : Completed @ $date_time"
    
    (echo "Subject: $SUBJECT"
    echo "`cat sendmail_list.txt`"
    echo "MIME-Version: 1.0"
    echo "Content-Type: text/html"
    echo "Content-Disposition: inline"
    cat $CONTENT
    )|/usr/sbin/sendmail -t 
    

    please help me know the solution...thanks