5

I'm trying to send an e-mail with sendmail, but I need to specify some headers (From, Content-Type, Subject). Here is the command I am currently running:

echo "Content-Type: text/plain\r\nFrom: [email protected]\r\nSubject: Test\r\n\r\nThe body goes here" | sendmail -f [email protected] [email protected]

The problem is that the headers are not being set. Do I have the format correct?

2 Answers 2

4

You need to use echo with the 'e' parameter.

 -e  
    Enable interpretation of the following backslash-escaped characters in each STRING:  
      \n          new line  
      \r          carriage return

If you look at the result of sending mail with your command you see the following:

Content-Type: text/plain\r\nFrom: [email protected]\r\nSubject: Test\r\n\r\nThe body goes here

(It literally shows up as above as a single line, including the 'special characters')

Applying the above, slight, modification allows your code to work fine:

echo -e "Content-Type: text/plain\r\nFrom: [email protected]\r\nSubject: Test\r\n\r\nThe body goes here" | sendmail -f [email protected] [email protected]
0
0

Use printf

printf "Content-Type: text/plain\r\nFrom: [email protected]\r\nSubject: Test\r\n\r\nThe body goes here" | sendmail -f [email protected] [email protected]

1
  • You'd be better with printf "%s" "Content-Type...body goes here\r\n" so that inadvertent formatting instructions in the body don't confuse. May 19, 2018 at 22:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .