29

how to insert new line in the email using linux mail command?

echo "Hi xxx, would you tell me something?\\n thanks!\\n -xxx" | mail -s "subject" xxx@gmail.com

The email shows the literal '\n', not a newline, how do fix it?

jww
  • 97,681
  • 90
  • 411
  • 885
user881480
  • 5,005
  • 6
  • 32
  • 31
  • SMTP uses CRLF line endings, not CR alone and not LF alone. In fact, the RFC specifically says you MUST NOT send a lone CR or lone LF. Also, messages should end in two CRLF, not one. Also see [RFC 5321](https://tools.ietf.org/html/rfc5321), Section 2.3.8, *Lines*. – jww Dec 12 '19 at 10:37
  • @jww As already discussed elsewhere, `mail` or `mailx` takes care of this for you. You don't need to know or care what SMTP requires in this particular case. – tripleee Dec 12 '19 at 16:33

5 Answers5

45

Try using echo -e

echo -e "Hello \n World"

You can type man echo from the command line to read more.

jahroy
  • 22,322
  • 9
  • 59
  • 108
  • `echo -e` is brittle and poorly portable. You should prefer `printf` instead; it also simplifies this particular use case because you can conveniently split the lines to separate string arguments. – tripleee Dec 12 '19 at 16:36
  • 1
    I've never expected that I'll search for echo's options, but here we are. even echo has options (because of course it does :D) – Miro Rodozov Oct 23 '20 at 15:43
18

With mailx, if you send the email to an Outlook user, you can add 2 spaces at the beginning of each line.

{ echo "Hi xxx, would you tell me something" ; echo "thanks!" ; echo "-xxx" } | sed 's/^/  /g' | mailx -s "subject" xxx@domain.com
CarenRose
  • 1,266
  • 1
  • 12
  • 24
Eric
  • 181
  • 1
  • 2
5

The accepted answer did not work for me when using the mail command, I had to use

\r

My whole command is

mail -s "SUBJECT" -aFrom:"from@sdf.com "to@sdfd.com" <<< $( echo -e "Line1\rLine2")
Lenka Pitonakova
  • 979
  • 12
  • 14
5

Tested on MacOS with Bash 3.2

bash-3.2$ mail -s "$subject" TestEmail@gmail.com <<< $(printf "%s\r\n%s\n" "This is Line One"  "This is Line Two")

This is a screen shot from gmail of the email received

enter image description here

David Walton
  • 321
  • 4
  • 10
  • 1
    While this works, the here string is superfluous if you are using a separate process to produce the string anyway. `printf '%s\n' 'This is line one' 'This is line two' | mail -s "$subject"` – tripleee Dec 12 '19 at 16:32
  • For a way to fix this specifically for Outlook users on the receiving end, see https://serverfault.com/a/1083188/34013 . – Richlv Nov 10 '21 at 20:35
  • This is the only working solution for me! It was a very pain to make this working! – Zac Mar 27 '23 at 15:11
3

I encountered this issue and resolved it by surrounding with quotes the variable I was piping into mailx.

I started with a list of processes from ps i.e. list="$(ps |grep some_process)".

Then when I tried to mail that out as follows, the newlines were obliterated:

echo $body | mailx -s "${subject}" recipient@someplace.com

But, simply wrapping $body with quotes preserved the newlines:

echo "$body" | mailx -s "${subject}" recipient@someplace.com
dstandish
  • 2,328
  • 18
  • 34