【发布时间】:2020-04-05 16:20:08
【问题描述】:
我编写了一个 bash 脚本,并希望它向我发送一封邮件,邮件正文包含多行消息。
subject="subject1"
我尝试了多个messages,这里只是我记得的几个:
message="temporary message"
message="temporary message
next line here"
message=printf '%s\n' "Note that $start and $end use server time. \n $timeelapsed"
message=echo -e . . .
这里只是我尝试结合上面的messages 的mail 命令的许多变体中的四个:
mail -s "$subject" emailadd1@google.com <<< "$message"
mail -s "$subject" emailadd1@google.com <<< $( echo -e "Note that $start and $end use server time. \n the end")
mail -s "$subject" emailadd1@google.com <<< $( echo -e "Note that $start and $end use server time. \n the end")
mail -s "$subject" emailadd1@google.com <<< $( echo -e "Note that $start and $end use server time. \r the end")
没有一个有效。结果始终是电子邮件正文中的单行消息。
我已经尝试了这个相关帖子中的所有内容,但没有运气:How to insert new line in the email using linux mail command?
有人可以为我写一个完整的解决方案吗?
【问题讨论】:
-
SMTP 使用 CRLF 行尾,而不是单独使用 CR 和单独使用 LF。事实上,RFC 明确规定您不得发送单独的 CR 或单独的 LF。此外,消息应该以两个 CRLF 结尾,而不是一个。另请参阅RFC 5321,第 2.3.8 节,行。
-
我确信这对某人有帮助,但它超出了我的范围。什么 bash 代码可以完成我想要完成的任务?
-
我没有找到一个好的副本,这有点令人惊讶......对于行尾问题,也许可以尝试:
echo -en "Test CRLF endings\r\n"。用hexdump查看它会显示预期的行尾。echo -en "Test CRLF endings\r\n" | hexdump -C显示54 65 73 74 20 43 52 4c 46 20 65 6e 64 69 6e 67 73 0d 0a。echo -en应该可以在 Linux、OS X 和 Windows 上运行。 -
如果您有很多行,我建议您将消息写在多行带引号的字符串中(您只需使用文字换行符来表示换行符)然后使用
sed插入\rat每行的末尾和末尾的额外行。见this ideone;在输出中^M代表\r和$\n -
如果
$message实际上包含多行字符串,您的几次尝试都会正常工作。