27

I am using the following command to create messages on the fly, and send them:

echo "Subject:Hello \n\n I would like to buy a hamburger\n" | sendmail [email protected]

It seems that when you send the information from a file, by doing something like:

sendmail [email protected] mail.txt 

Then sendmail sees each line as a header, and parses it. But the way I sent it above, everything ends up in the subject line.

If one wants to echo a message complete with headers, into sendmail, then what is the format ? How does one do it ?

2 Answers 2

30

Your echo statement should really output newlines not the sequence \ followed by n. You can do that by providing the -e option:

echo -e "Subject:Hello \n\n I would like to buy a hamburger\n" | sendmail [email protected]

To understand what is the difference have a look at the output from the following two commands:

echo "Subject:Hello \n\n I would like to buy a hamburger\n"
echo -e "Subject:Hello \n\n I would like to buy a hamburger\n"
2

"Here document" in shell scripts (You compose message headers and body)

#!/bin/sh
[email protected]
/usr/sbin/sendmail -i $TO <<MAIL_END
Subject: Hello
To: $TO

I would like to buy a hamburger
MAIL_END

Message body from external file

#!/bin/sh
[email protected]
BODY_FILE=mail.txt
(cat - $BODY_FILE)<<HEADERS_END | /usr/sbin/sendmail -i $TO
Subject: Hello
To: $TO

HEADERS_END

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.