bash 从 Linux 命令行发送电子邮件的不同方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4934176/
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
Different ways of sending e-mail from linux command line
提问by aimfeld
For our web projects, we need a reliable e-mail distribution mechanism. Due to bad experiences in the past, I have written a bash script (executed hourly) which sends a notification e-mail if
对于我们的网络项目,我们需要一个可靠的电子邮件分发机制。由于过去的糟糕经历,我编写了一个 bash 脚本(每小时执行一次),如果出现以下情况,它会发送通知电子邮件
- the qmail-send process is not running
- there are too many failures in the mail log
- qmail-send 进程没有运行
- 邮件日志中有太多失败
For sending the notification e-mail I obviously don't want to depend on qmail, since qmail will be unavailable if the qmail-send process is not running. However, the following command sends the notification e-mail via qmail:
对于发送通知电子邮件,我显然不想依赖 qmail,因为如果 qmail-send 进程没有运行,qmail 将不可用。但是,以下命令通过 qmail 发送通知电子邮件:
echo "failure rate critical" | mail -s "qmail notification" [email protected]
What's the easiest way to send e-mail from the linux command line without qmail? Can I use sendmail?
在没有 qmail 的情况下从 linux 命令行发送电子邮件的最简单方法是什么?我可以使用sendmail吗?
If you guys have smarter alarm systems to monitor qmail, please let me know.
如果你们有更智能的警报系统来监控 qmail,请告诉我。
回答by ThiefMaster
Invoke the /usr/sbin/sendmailbinary. It is usually available no matter which MTA you use and you can be sure it supports the standard sendmail interface if it's named sendmail.
调用/usr/sbin/sendmail二进制文件。无论您使用哪种 MTA,它通常都可用,并且如果它名为 sendmail,您就可以确定它支持标准的 sendmail 接口。
The easiest way to use it is invoking sendmail -tand then writing the email including a valid Toheader to its stdin. If you omit -tyou'll have to pass the recipient address as a commandline argument.
使用它的最简单方法是调用sendmail -t,然后将包含有效To标头的电子邮件写入其标准输入。如果省略-t,则必须将收件人地址作为命令行参数传递。
Another solution would be using SMTP but if you need to send emails from a bash script this is clearly a bad solution as there are no standard libraries in Bash which contain functions to send an email over smtp (unlike in python where you cannot easily send mails using sendmail but over SMTP).
另一种解决方案是使用 SMTP,但如果您需要从 bash 脚本发送电子邮件,这显然是一个糟糕的解决方案,因为 Bash 中没有包含通过 smtp 发送电子邮件的功能的标准库(与在 python 中无法轻松发送邮件不同)使用 sendmail 但通过 SMTP)。
回答by ThiefMaster
You have a mailutility in POSIX. If you're only for linux, sendmailis ok (but then you rely on the system's (mis)configuration, right?).
您mail在 POSIX 中有一个实用程序。如果您只使用 linux,那没问题sendmail(但是您依赖于系统的(错误)配置,对吗?)。
All in all, SMTP protocol is not that difficult. I'd say you can talk in pure SMTP. It's about four commands to issue for a trivial mail. And it's portable:) But if there are complications it may result in PITA...
总而言之,SMTP 协议并没有那么难。我想说你可以用纯 SMTP 说话。为一封普通邮件发出大约四个命令。而且它是便携的:) 但是如果出现并发症,它可能会导致 PITA ......

