How to insert new line in the email using linux mail command? [duplicate]

馋奶兔 提交于 2019-12-18 03:53:55

问题


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?


回答1:


Try using echo -e

echo -e "Hello \n World"

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




回答2:


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



回答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



回答4:


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



回答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



来源:https://stackoverflow.com/questions/12614573/how-to-insert-new-line-in-the-email-using-linux-mail-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!