来自 bash 脚本的电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2275593/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:56:29  来源:igfitidea点击:

email from bash script

bash

提问by Jim

#!/bin/bash
MESSAGE="Line one. /n"

MESSAGE="$MESSAGE Line two. /n"
MESSAGE="$MESSAGE Line three."

echo $MESSAGE | mail -s "test" "[email protected]"

Is that how I should get each line, on its own line?

这是我应该如何在自己的行上获取每行?

回答by Matthew Flaschen

Use a heredoc.

使用heredoc

mail -s "test" "[email protected]" << END_MAIL
Line one.
Line two.
Line three.
END_MAIL

回答by Jim

Change:

改变:

echo $MESSAGE | mail -s "test" "[email protected]"

To:

到:

echo -e $MESSAGE | mail -s "test" "[email protected]"

回答by DigitalRoss

The heredoc advice is good, plus you might want to consider using mailxfor which there exists a Posix standard or perhaps sendmailwhich will exist if the mailer is either sendmail or postfix. (I'm not sure about qmail.)

Heredoc 建议很好,另外您可能要考虑使用mailx存在 Posix 标准的地方,或者sendmail如果邮件程序是 sendmail 或 postfix可能会存在。(我不确定 qmail。)

Unless using sendmail, it's also a good idea to set the MAILRCvariable to /dev/nullto bypass the user's configuration script, if any.

除非使用 sendmail,否则最好将该MAILRC变量设置/dev/null为绕过用户的配置脚本(如果有)。