【问题标题】:How to insert new line in message body of email using Bash? [duplicate]如何使用 Bash 在电子邮件的消息正文中插入新行? [复制]
【发布时间】: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 . . .

这里只是我尝试结合上面的messagesmail 命令的许多变体中的四个:

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 0aecho -en 应该可以在 Linux、OS X 和 Windows 上运行。
  • 如果您有很多行,我建议您将消息写在多行带引号的字符串中(您只需使用文字换行符来表示换行符)然后使用sed 插入\r at每行的末尾和末尾的额外行。见this ideone;在输出中^M 代表\r$ \n
  • 如果 $message 实际上包含多行字符串,您的几次尝试都会正常工作。

标签: linux bash


【解决方案1】:

使用 Bash 3.2 在 MacOS 上测试

mail -s "$subject" MyTestEmail@gmail.com <<< $(printf "%s\r\n%s\n" "Note that $start and $end use server time"  "the end")

这是收到的电子邮件的 gmail 屏幕截图

【讨论】:

  • 这看起来很有希望,但是,它对我不起作用。该电子邮件的正文中没有消息,而是一个附件,ATT00001.bin
  • 你在用什么样的外壳?重击? zsh ?你能告诉我你运行的命令吗?
  • 重击。我复制粘贴了你的命令,只是更改了电子邮件地址。
  • 您也发送到 gmail 地址吗?这可能是邮件服务器在做一些有趣的事情
  • 我在将您的解决方案作为更复杂实施的一部分实施时犯了一个错误。我让它工作了。谢谢。
猜你喜欢
  • 2015-01-26
  • 2023-03-19
  • 2021-02-06
  • 1970-01-01
  • 2019-02-08
  • 2013-05-23
  • 1970-01-01
  • 2017-10-17
  • 1970-01-01
相关资源
最近更新 更多