4

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: do-not-reply@mydomain.com\r\nSubject: Test\r\n\r\nThe body goes here" | sendmail -f do-not-reply@mydomain.com admin@mydomain.com

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

pravdomil
  • 111
  • 6
Justin
  • 5,008
  • 19
  • 58
  • 82

2 Answers2

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: do-not-reply@mydomain.com\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: do-not-reply@mydomain.com\r\nSubject: Test\r\n\r\nThe body goes here" | sendmail -f do-not-reply@mydomain.com admin@mydomain.com
cyberx86
  • 20,620
  • 1
  • 60
  • 80
0

Use printf

printf "Content-Type: text/plain\r\nFrom: do-not-reply@mydomain.com\r\nSubject: Test\r\n\r\nThe body goes here" | sendmail -f do-not-reply@mydomain.com admin@mydomain.com

pravdomil
  • 111
  • 6
  • 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. – roaima May 19 '18 at 22:18