0

I'm using a script that sends mail using mail utils but now I want to send mail using sendmail can anyone help me as I was not able to get the same result from sendmail as I did from mail.

#Email Settings
Message_Success="Application Backup generated successfully"
Message_Failure="Problem occured while generating Application Backup please verify"

Subject="Application Backup Status Mail"

Recipients="tarun@gmail.com"

#Check if Backup is complete
if [ -e "$Backup_File" ]; then
echo "Today's Backup File Exist's" >> $Log_File
echo "$Message_Success" | mail -s "$Subject" "$Recipients"
else
echo "Today's Backup File Not Found" >> $Log_File
echo "$Message_Failure" | mail -s "$Subject" "$Recipients"
fi

I figured out a bit of it and here it is. But where can I define the emails subject.

echo "$Message_Success" | sendmail -f sender@email.com "$Recipients"
0

I would write

if [ -e "$Backup_File" ]; then
    echo "Today's Backup File Exist's" >> $Log_File
else
    echo "Today's Backup File Not Found" >> $Log_File
fi
{
    printf "To: %s\nSubject: %s\n\n" "$Recipients" "$Subject"
    if [ -e "$Backup_File" ]; then
        echo "$Message_Success"
    else
        echo "$Message_Failure"
    fi
} | /path/to/sendmail -oi -t -f sender@example.com 

You're writing the message with headers, and passing to sendmail. The -t flag forces sendmail to read the recipients from the messasge.

  • You need to separate the header from the body with an additional blank line. That's why I have 2 \n at the end of my printf format string. – glenn jackman Jul 3 '13 at 9:55

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.