Search code examples
bashsendmailcron

Sendmail is not working with crontab (bash)


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


Solution

  • 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)