从 linux shell 脚本发送邮件

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

Sending a mail from a linux shell script

linuxemailshellsendmail

提问by Yazz.com

I want to send an email from a Linux Shell script. What is the standard command to do this and do I need to set up any special server names?

我想从 Linux Shell 脚本发送电子邮件。执行此操作的标准命令是什么,我是否需要设置任何特殊的服务器名称?

采纳答案by Francesco Laurita

If the server is well configured, eg it has an up and running MTA, you can just use the mail command.

如果服务器配置良好,例如它有一个启动并正在运行的 MTA,您可以只使用 mail 命令。

For instance, to send the content of a file, you can do this:

例如,要发送文件的内容,您可以这样做:

$ cat /path/to/file | mail -s "your subject" [email protected]

man mailfor more details.

man mail更多细节。

回答by DarkDust

The mailcommand does that (who would have guessed ;-). Open your shell and enter man mailto get the manual page for the mailcommand for all the options available.

mail命令会这样做(谁会猜到;-)。打开您的 shell 并输入man mail以获取mail所有可用选项的命令手册页。

回答by GreyCat

Generally, you'd want to use mailcommand to send your message using local MTA (that will either deliver it using SMTP to the destination or just forward it into some more powerful SMTP server, for example, at your ISP). If you don't have a local MTA (although it's a bit unusual for a UNIX-like system to omit one), you can either use some minimalistic MTA like ssmtp.

通常,您希望使用mail命令通过本地 MTA 发送您的消息(这将使用 SMTP 将其传送到目的地,或者只是将其转发到一些更强大的 SMTP 服务器,例如,在您的 ISP 处)。如果您没有本地 MTA(尽管类似 UNIX 的系统省略一个 MTA 有点不寻常),您可以使用一些简约的 MTA,如ssmtp

ssmtpis quite easy to configure. Basically, you'll just need to specify where your provider's SMTP server is:

ssmtp很容易配置。基本上,您只需要指定提供商的 SMTP 服务器在哪里:

# The place where the mail goes. The actual machine name is required
# no MX records are consulted. Commonly mailhosts are named mail.domain.com
# The example will fit if you are in domain.com and you mailhub is so named.
mailhub=mail

Another option is to use one of myriads scripts that just connect to SMTP server directly and try to post a message there, such as Smtp-Auth-Email-Script, smtp-cli, SendEmail, etc.

另一种选择是使用直接连接到 SMTP 服务器并尝试在那里发布消息的无数脚本之一,例如Smtp-Auth-Email-Scriptsmtp-cliSendEmail等。

回答by hornetbzz

If both exim and ssmtp are running, you may enter into troubles. So if you just want to run a simple MTA, just to have a simple smtp client to send email notifications for insistance, you shall purge the eventually preinstalled MTA like eximor postfixfirst and reinstall ssmtp.

如果 exim 和 ssmtp 都在运行,您可能会遇到麻烦。所以如果你只是想运行一个简单的 MTA,只是为了有一个简单的 smtp 客户端来发送电子邮件通知以供坚持,你应该先清除最终预装的 MTA,如eximpostfix,然后重新安装 ssmtp。

Then it's quite straight forward, configuring only 2 files (revaliases and ssmtp.conf) - See ssmtp doc - , and usage in your bash or bourne script is like :

然后就很简单了,只配置 2 个文件(revaliases 和 ssmtp.conf) - 请参阅 ssmtp doc - ,并且在 bash 或 bourne 脚本中的用法如下:

#!/bin/sh  
SUBJECT=  
RECEIVER=  
TEXT=  

SERVER_NAME=$HOSTNAME  
SENDER=$(whoami)  
USER="noreply"

[[ -z  ]] && SUBJECT="Notification from $SENDER on server $SERVER_NAME"  
[[ -z  ]] && RECEIVER="another_configured_email_address"   
[[ -z  ]] && TEXT="no text content"  

MAIL_TXT="Subject: $SUBJECT\nFrom: $SENDER\nTo: $RECEIVER\n\n$TEXT"  
echo -e $MAIL_TXT | sendmail -t  
exit $?  

Obviously do not forget to open your firewall output to the smtp port (25).

显然不要忘记将防火墙输出打开到 smtp 端口 (25)。

回答by Germano

Admitting you want to use some smtp server, you can do:

承认你想使用一些 smtp 服务器,你可以这样做:

export SUBJECT=some_subject
export smtp=somehost:someport
export EMAIL=someaccount@somedomain
echo "some message" | mailx -s "$SUBJECT" "$EMAIL"

Change somehost, someport, and someaccount@somedomainto actual values that you would use. No encryption and no authentication is performed in this example.

变化somehostsomeportsomeaccount@somedomain实际值,你会使用。本示例中不进行加密和身份验证。

回答by Germano

If you want a clean and simple approach in bash, and you don't want to use cat, echo, etc., the simplest way would be:

如果你想在bash一个清洁,简单的方法,而你不想用catecho等等,最简单的方法是:

mail -s "subject here" [email protected] <<< "message"

<<<is used to redirect standard input. It's been a part of bash for a long time.

<<<用于重定向标准输入。很长一段时间以来,它一直是 bash 的一部分。

回答by SPRBRN

Another option for in a bash script:

在 bash 脚本中的另一个选项:

mailbody="Testmail via bash script"
echo "From: [email protected]" > /tmp/mailtest
echo "To: [email protected]" >> /tmp/mailtest
echo "Subject: Mailtest subject" >> /tmp/mailtest
echo "" >> /tmp/mailtest
echo $mailbody >> /tmp/mailtest
cat /tmp/mailtest | /usr/sbin/sendmail -t
  • The file /tmp/mailtestis overwritten everytime this script is used.
  • The location of sendmail may differ per system.
  • When using this in a cron script, you have to use the absolute path for the sendmail command.
  • /tmp/mailtest每次使用此脚本时都会覆盖该文件。
  • sendmail 的位置可能因系统而异。
  • 在 cron 脚本中使用它时,您必须使用 sendmail 命令的绝对路径。

回答by Daniel J.

You don't even need an MTA. The SMTP protocol is simple enough to directly write it to your SMTP server. You can even communicate over SSL/TLS if you have the OpenSSL package installed. Check this post: https://33hops.com/send-email-from-bash-shell.html

您甚至不需要 MTA。SMTP 协议非常简单,可以直接将其写入您的 SMTP 服务器。如果您安装了 OpenSSL 软件包,您甚至可以通过 SSL/TLS 进行通信。检查这个帖子:https: //33hops.com/send-email-from-bash-shell.html

The above is an example on how to send text/html e-mails that will work out of the box. If you want to add attachments the thing can get a bit more complicated, you will need to base64 encode the binary files and embed them between boundaries. THis is a good place to start investigating: http://forums.codeguru.com/showthread.php?418377-Send-Email-w-attachments-using-SMTP

以上是关于如何发送文本/html 电子邮件的示例,这些电子邮件将立即可用。如果你想添加附件,事情可能会变得更复杂一些,你需要对二进制文件进行 base64 编码并将它们嵌入边界之间。这是开始调查的好地方:http://forums.codeguru.com/showthread.php?418377-Send-Email-w-attachments-using-SMTP

回答by krish___na

On linux, mailutility can be used to send attachment with option "-a". Go through man pages to read about the option. For eg following code will send an attachment :

在 linux 上,邮件实用程序可用于通过选项“-a”发送附件。通过手册页阅读有关该选项的信息。例如,以下代码将发送附件:

mail -s "THIS IS SUBJECT" -a attachment.txt [email protected] <<< "Hi Buddy, Please find failure reports."

mail -s "THIS IS SUBJECT" -a attachment.txt [email protected] <<< "Hi Buddy, Please find failure reports."

回答by TINGTING WANG

you can use 'email' or 'emailx' command.

您可以使用“email”或“emailx”命令。

(1) $ vim /etc/mail.rc # or # vim /etc/nail.rc

(1) $ vim /etc/mail.rc # 或 # vim /etc/nail.rc

set from = [email protected] #
set smtp = smtp.exmail.gmail.com #gmail's smtp server 
set smtp-auth-user = [email protected] #sender's email address
set smtp-auth-password = xxxxxxx #get from gmail, not your email account passwd
set smtp-auth=login

Because if it is not sent from an authorized account, email will get to junk mail list.

因为如果不是从授权帐户发送,电子邮件将进入垃圾邮件列表。

(2) $ echo "Pls remember to remove unused ons topics!" | mail -s "waste topics" -a a.txt [email protected] #send to group user '[email protected]'

(2) $ echo "请记得删除未使用的主题!" | mail -s "waste topics" -a a.txt [email protected] #send to group user '[email protected]'