如何使用 linux mail 命令在电子邮件中插入新行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12614573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert new line in the email using linux mail command?
提问by user881480
how to insert new line in the email using linux mail command?
如何使用 linux mail 命令在电子邮件中插入新行?
echo "Hi xxx, would you tell me something?\n thanks!\n -xxx" | mail -s "subject" [email protected]
The email shows the literal '\n', not a newline, how do fix it?
电子邮件显示文字“\n”,而不是换行符,如何解决?
采纳答案by jahroy
Try using echo -e
尝试使用 echo -e
echo -e "Hello \n World"
echo -e "Hello \n World"
You can type man echo
from the command line to read more.
您可以man echo
从命令行键入以阅读更多信息。
回答by Eric
With mailx
, if you send the email to an Outlook user, you can add 2 spaces at the beginning of each line.
使用mailx
,如果您将电子邮件发送给 Outlook 用户,则可以在每行开头添加 2 个空格。
{ echo "Hi xxx, would you tell me something" ; echo "thanks!" ; echo "-xxx" } | sed 's/^/ /g' | mailx -s "subject" [email protected]
回答by Elendurwen
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:"[email protected] "[email protected]" <<< $( echo -e "Line1\rLine2")
回答by chorbs
I encountered this issue and resolved it by surrounding with quotes the variable I was piping into mailx
.
我遇到了这个问题,并通过用引号括住我正在输入的变量来解决它mailx
。
I started with a list of processes from ps i.e. list="$(ps |grep some_process)"
.
我从 ps ie 中的进程列表开始list="$(ps |grep some_process)"
。
Then when I tried to mail that out as follows, the newlines were obliterated:
然后,当我尝试按如下方式将其邮寄出去时,换行符被删除了:
echo $body | mailx -s "${subject}" [email protected]
But, simply wrapping $body
with quotes preserved the newlines:
但是,只需$body
用引号包裹即可保留换行符:
echo "$body" | mailx -s "${subject}" [email protected]
回答by David Walton
Tested on MacOS with Bash 3.2
在 MacOS 上使用 Bash 3.2 进行测试
bash-3.2$ mail -s "$subject" [email protected] <<< $(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
这是收到的电子邮件的 gmail 屏幕截图