a coder
a coder

Reputation: 7639

Shell script hangs on mail command

I'm finding that a call to the mail command is causing a script to suspend without error. To close the script I have to ctrl-c or issue a kill command on the process id.

The pertinent section of the script is below:

EMAIL_TO="[email protected]"

if [ -f /www/archives/pdf/pdf_201207021048.tar ]; then
    echo "file exists"
else
    echo "file does not exist"
fi

echo "sending mail next..."

mail -s "pdfbackup" "$EMAIL_TO"

echo "mail sent?"

When running this, I'm seeing the text "sending mail next..." and nothing more. It never returns to prompt.

I can see the script is still in memory with ps -ax | grep myscript.sh.

I've tried using quotes around the subject and email, and again without. The same result is produced either way.

What am I doing wrong?

Upvotes: 2

Views: 8515

Answers (1)

Conner
Conner

Reputation: 31060

From man mail:

Sending Mail
To send a message to one or more people, mail can be invoked with arguments which are the names of people to whom the mail will be sent. You are then expected to type in your message, followed by a at the beginning of a line. The section below Replying To or Originating Mail, describes some features of mail available to help you compose your letter.

It's expecting a body of the message. Use <C-d> for newlines and end the message with a blank line and <C-d>. Alternatively you can supply the body and pipe to the command...

echo "This is the body" | mail -s "Subject" "[email protected]"

or

mail -s "Subject" "[email protected]" <<< "This is the body"

Or your can read the body from a file..

mail -s "Subject" "[email protected]" < body_in_file.txt

Upvotes: 6

Related Questions