20

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@example.com

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

sendmail email@example.com 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 ?

21

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@example.com

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"
1

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

#!/bin/sh
TO=email@example.com
/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
TO=email@example.com
BODY_FILE=mail.txt
(cat - $BODY_FILE)<<HEADERS_END | /usr/sbin/sendmail -i $TO
Subject: Hello
To: $TO

HEADERS_END

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.