从linux shell脚本发送邮件。

时间:2021-10-25 18:15:43

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脚本的邮件。这样做的标准命令是什么,我需要设置任何特殊的服务器名称吗?

9 个解决方案

#1


98  

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

如果服务器配置良好,例如它有一个up并运行MTA,那么您可以使用mail命令。

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

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

$ cat /path/to/file | mail -s "your subject" your@email.com

man mail for more details.

男士来信索取更多详情。

#2


72  

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中使用干净简单的方法,并且您不想使用cat、echo等,最简单的方法是:

mail -s "subject here" email@address.com <<< "message"

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

< <用于重定向标准输入。很长一段时间都是bash的一部分。< p>

#3


20  

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 exim or postfix first and reinstall ssmtp.

如果exim和ssmtp都在运行,您可能会遇到麻烦。因此,如果您只想运行一个简单的MTA,只需让一个简单的smtp客户端发送电子邮件通知,您就可以将最终预安装的MTA清除,比如exim或postfix,然后重新安装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 :

然后它非常直接,只配置了两个文件(revaliases和ssmtp.conf)—见ssmtp doc—,在您的bash或bourne脚本中使用如下:

#!/bin/sh  
SUBJECT=$1  
RECEIVER=$2  
TEXT=$3  

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

[[ -z $1 ]] && SUBJECT="Notification from $SENDER on server $SERVER_NAME"  
[[ -z $2 ]] && RECEIVER="another_configured_email_address"   
[[ -z $3 ]] && 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)。

#4


6  

Generally, you'd want to use mail command 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的系统来说,省略一个),您可以使用像ssmtp这样的简单的MTA。

ssmtp is 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-script、SMTP -cli、SendEmail等。

#5


6  

Another option for in a bash script:

bash脚本中的另一个选项:

mailbody="Testmail via bash script"
echo "From: info@myserver.test" >> /tmp/mailtest
echo "To: john@mywebsite.test" >> /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/mailtest is overwritten everytime this script is used.
  • 每次使用此脚本时,都会覆盖文件/tmp/mailtest。
  • The location of sendmail may differ per system.
  • sendmail的位置可能不同于每个系统。
  • When using this in a cron script, you have to use the absolute path for the sendmail command.
  • 当在cron脚本中使用这个时,您必须使用sendmail命令的绝对路径。

#6


3  

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@somedomain to actual values that you would use. No encryption and no authentication is performed in this example.

将somehost、someport和someaccount@somedomain更改为您将使用的实际值。在本例中没有加密和身份验证。

#7


1  

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-smtp。

#8


1  

On linux, mail utility 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 name@domain.com <<< "Hi Buddy, Please find failure reports."

邮件-s“这是主题”-附件。txt name@domain.com << Hi Buddy,请查找故障报告。

#9


0  

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

邮件命令可以做到这一点(谁会想到;-)。打开您的shell并输入man邮件,以获得针对所有可用选项的邮件命令的手册页。

#1


98  

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

如果服务器配置良好,例如它有一个up并运行MTA,那么您可以使用mail命令。

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

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

$ cat /path/to/file | mail -s "your subject" your@email.com

man mail for more details.

男士来信索取更多详情。

#2


72  

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中使用干净简单的方法,并且您不想使用cat、echo等,最简单的方法是:

mail -s "subject here" email@address.com <<< "message"

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

< <用于重定向标准输入。很长一段时间都是bash的一部分。< p>

#3


20  

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 exim or postfix first and reinstall ssmtp.

如果exim和ssmtp都在运行,您可能会遇到麻烦。因此,如果您只想运行一个简单的MTA,只需让一个简单的smtp客户端发送电子邮件通知,您就可以将最终预安装的MTA清除,比如exim或postfix,然后重新安装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 :

然后它非常直接,只配置了两个文件(revaliases和ssmtp.conf)—见ssmtp doc—,在您的bash或bourne脚本中使用如下:

#!/bin/sh  
SUBJECT=$1  
RECEIVER=$2  
TEXT=$3  

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

[[ -z $1 ]] && SUBJECT="Notification from $SENDER on server $SERVER_NAME"  
[[ -z $2 ]] && RECEIVER="another_configured_email_address"   
[[ -z $3 ]] && 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)。

#4


6  

Generally, you'd want to use mail command 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的系统来说,省略一个),您可以使用像ssmtp这样的简单的MTA。

ssmtp is 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-script、SMTP -cli、SendEmail等。

#5


6  

Another option for in a bash script:

bash脚本中的另一个选项:

mailbody="Testmail via bash script"
echo "From: info@myserver.test" >> /tmp/mailtest
echo "To: john@mywebsite.test" >> /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/mailtest is overwritten everytime this script is used.
  • 每次使用此脚本时,都会覆盖文件/tmp/mailtest。
  • The location of sendmail may differ per system.
  • sendmail的位置可能不同于每个系统。
  • When using this in a cron script, you have to use the absolute path for the sendmail command.
  • 当在cron脚本中使用这个时,您必须使用sendmail命令的绝对路径。

#6


3  

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@somedomain to actual values that you would use. No encryption and no authentication is performed in this example.

将somehost、someport和someaccount@somedomain更改为您将使用的实际值。在本例中没有加密和身份验证。

#7


1  

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-smtp。

#8


1  

On linux, mail utility 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 name@domain.com <<< "Hi Buddy, Please find failure reports."

邮件-s“这是主题”-附件。txt name@domain.com << Hi Buddy,请查找故障报告。

#9


0  

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

邮件命令可以做到这一点(谁会想到;-)。打开您的shell并输入man邮件,以获得针对所有可用选项的邮件命令的手册页。