Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从linux shell脚本发送邮件_Linux_Email_Shell_Sendmail - Fatal编程技术网

从linux shell脚本发送邮件

从linux shell脚本发送邮件,linux,email,shell,sendmail,Linux,Email,Shell,Sendmail,我想从Linux Shell脚本发送电子邮件。执行此操作的标准命令是什么?我是否需要设置任何特殊的服务器名称?使用mail命令可以做到这一点(谁会猜到;-)。打开shell并输入man-mail,以获取mail命令的手册页面,了解所有可用选项。如果服务器配置良好,例如它有一个启动并运行的MTA,您可以使用mail命令 例如,要发送文件内容,可以执行以下操作: $ cat /path/to/file | mail -s "your subject" your@email.com man-mail

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

使用
mail
命令可以做到这一点(谁会猜到;-)。打开shell并输入
man-mail
,以获取
mail
命令的手册页面,了解所有可用选项。

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

例如,要发送文件内容,可以执行以下操作:

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

man-mail
了解更多详细信息。

通常,您希望使用
mail
命令使用本地MTA发送邮件(该命令将使用SMTP将邮件发送到目标,或仅将其转发到某个功能更强大的SMTP服务器,例如ISP)。如果您没有本地MTA(尽管在类UNIX系统中省略一个MTA有点不寻常),您可以使用一些类似于的最低限度MTA

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

另一种选择是使用多种直接连接到SMTP服务器的脚本之一,并尝试在那里发布消息,例如、,等等。

如果exim和ssmtp都在运行,您可能会遇到麻烦。因此,如果您只想运行一个简单的MTA,只想让一个简单的smtp客户端发送电子邮件通知,您应该首先清除最终预安装的MTA,如exim或postfix,然后重新安装ssmtp

然后很简单,只配置2个文件(revalias和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 $?  

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

承认您想使用某些smtp服务器,您可以执行以下操作:

export SUBJECT=some_subject
export smtp=somehost:someport
export EMAIL=someaccount@somedomain
echo "some message" | mailx -s "$SUBJECT" "$EMAIL"
更改
somehost
someport
someaccount@somedomain
转换为您将使用的实际值。
在本例中,不执行加密和身份验证

如果您希望在bash中使用干净简单的方法,并且不想使用
cat
echo
等,最简单的方法是:

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

mail-s“此处主题”email@address.combash脚本中的另一个选项:

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
  • 每次使用此脚本时,都会覆盖文件
    /tmp/mailtest
  • sendmail的位置可能因系统而异
  • 在cron脚本中使用此命令时,必须使用sendmail命令的绝对路径

    • 您甚至不需要MTA。SMTP协议非常简单,可以直接将其写入SMTP服务器。如果安装了OpenSSL包,您甚至可以通过SSL/TLS进行通信。查看此帖子:


      上面是一个关于如何发送开箱即用的文本/html电子邮件的示例。如果您想添加附件,事情可能会变得更复杂,您需要对二进制文件进行base64编码,并将其嵌入到边界之间。这是一个开始调查的好地方:

      在linux上,邮件实用程序可用于发送带有选项“-a”的附件。浏览手册页以了解该选项。对于eg,以下代码将发送附件:


      邮件-s“这是主题”-a attachment.txtname@domain.com您可以使用“email”或“emailx”命令

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

      set from = xxx@xxx.com #
      set smtp = smtp.exmail.gmail.com #gmail's smtp server 
      set smtp-auth-user = xxx@xxx.com #sender's email address
      set smtp-auth-password = xxxxxxx #get from gmail, not your email account passwd
      set smtp-auth=login
      
      因为如果不是从授权帐户发送,电子邮件将进入垃圾邮件列表。 (二) $echo“请记住删除未使用的ons主题!”| mail-s“废物主题”-a.txtdeveloper@xxx.com#发送到组用户的developer@xxxx.com“

      从LINUX向GMAIL发送邮件 使用后缀

      1:安装软件

      Debian和Ubuntu:

      apt-get update && apt-get install postfix mailutils
      
      OpenSUSE:

      zypper update && zypper install postfix mailx cyrus-sasl
      
      软呢帽:

      dnf update && dnf install postfix mailx
      
      CentOS:

      yum update && yum install postfix mailx cyrus-sasl cyrus-sasl-plain
      
      Arch Linux:

      pacman -Sy postfix mailutils
      
      FreeBSD:

      portsnap fetch extract update
      
      cd /usr/ports/mail/postfix
      
      make config
      
      在配置中选择SASL支持

      make install clean
      
      pkg install mailx
      
      2。配置Gmail

      mail -s "subject" recever@domain.com
      
      /etc/后缀。创建或编辑密码文件:

      vim /etc/postfix/sasl_passwd
      
      我使用的是vim,你可以使用任何文件编辑器,比如nano,cat

      >Ubuntu、Fedora、CentOS、Debian、OpenSUSE、Arch Linux:

      vim /etc/postfix/main.cf
      
      vim /etc/postfix/main.cf
      
      vim /usr/local/etc/postfix/main.cf
      
      postmap /etc/postfix/sasl_passwd
      
      systemctl restart postfix.service
      
      service postfix onestart
      nano /etc/rc.conf
      
      加上这个

      其中用户替换为您的邮箱名和密码是您的gmail密码

      保存并关闭文件并使其仅可由root用户访问:因为它是一个包含您的密码的敏感内容

      >FreeBSD:

      vim /etc/postfix/main.cf
      
      vim /etc/postfix/main.cf
      
      vim /usr/local/etc/postfix/main.cf
      
      postmap /etc/postfix/sasl_passwd
      
      systemctl restart postfix.service
      
      service postfix onestart
      nano /etc/rc.conf
      
      目录/usr/local/etc/postfix

      vim /usr/local/etc/postfix/sasl_passwd
      
      添加行:

      [smtp.gmail.com]:587    user@gmail.com:password
      
      保存并使其仅可由root用户访问:

      chmod 600 /usr/local/etc/postfix/sasl_passwd
      
      3。后缀配置

      配置文件main.cf

      我们必须在后缀中设置6个参数

      Ubuntu、Arch Linux、Debian:

      vim /etc/postfix/main.cf
      
      vim /etc/postfix/main.cf
      
      vim /usr/local/etc/postfix/main.cf
      
      postmap /etc/postfix/sasl_passwd
      
      systemctl restart postfix.service
      
      service postfix onestart
      nano /etc/rc.conf
      
      编辑

      修改以下值:

      relayhost = [smtp.gmail.com]:587
      smtp_use_tls = yes
      smtp_sasl_auth_enable = yes
      smtp_sasl_security_options =
      smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
      smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
      
      smtp_sasl_security_选项在配置中将设置为空,以确保没有使用与Gmail不兼容的安全选项

      保存并关闭

      至于

      OpenSUSE:

      vim /etc/postfix/main.cf
      
      vim /etc/postfix/main.cf
      
      vim /usr/local/etc/postfix/main.cf
      
      postmap /etc/postfix/sasl_passwd
      
      systemctl restart postfix.service
      
      service postfix onestart
      nano /etc/rc.conf
      
      修改

      relayhost = [smtp.gmail.com]:587
      smtp_use_tls = yes
      smtp_sasl_auth_enable = yes
      smtp_sasl_security_options =
      smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
      smtp_tls_CAfile = /etc/ssl/ca-bundle.pem
      
      relayhost = [smtp.gmail.com]:587
      smtp_use_tls = yes
      smtp_sasl_auth_enable = yes
      smtp_sasl_security_options =
      smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
      smtp_tls_CAfile = /etc/ssl/certs/ca-bundle.crt
      
      它还需要配置文件master.cf

      修改:

      vim /etc/postfix/master.cf
      
      relayhost = [smtp.gmail.com]:587
      smtp_use_tls = yes
      smtp_sasl_auth_enable = yes
      smtp_sasl_security_options =
      smtp_sasl_password_maps = hash:/usr/local/etc/postfix/sasl_passwd
      smtp_tls_CAfile = /etc/mail/certs/cacert.pem
      
      通过取消对此行的注释(删除#)

      保存并关闭

      CentOS软呢帽:

      vim /etc/postfix/main.cf
      
      vim /etc/postfix/main.cf
      
      vim /usr/local/etc/postfix/main.cf
      
      postmap /etc/postfix/sasl_passwd
      
      systemctl restart postfix.service
      
      service postfix onestart
      nano /etc/rc.conf
      
      修改

      relayhost = [smtp.gmail.com]:587
      smtp_use_tls = yes
      smtp_sasl_auth_enable = yes
      smtp_sasl_security_options =
      smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
      smtp_tls_CAfile = /etc/ssl/ca-bundle.pem
      
      relayhost = [smtp.gmail.com]:587
      smtp_use_tls = yes
      smtp_sasl_auth_enable = yes
      smtp_sasl_security_options =
      smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
      smtp_tls_CAfile = /etc/ssl/certs/ca-bundle.crt
      
      FreeBSD:

      vim /etc/postfix/main.cf
      
      vim /etc/postfix/main.cf
      
      vim /usr/local/etc/postfix/main.cf
      
      postmap /etc/postfix/sasl_passwd
      
      systemctl restart postfix.service
      
      service postfix onestart
      nano /etc/rc.conf
      
      修改:

      vim /etc/postfix/master.cf
      
      relayhost = [smtp.gmail.com]:587
      smtp_use_tls = yes
      smtp_sasl_auth_enable = yes
      smtp_sasl_security_options =
      smtp_sasl_password_maps = hash:/usr/local/etc/postfix/sasl_passwd
      smtp_tls_CAfile = /etc/mail/certs/cacert.pem
      
      保存并关闭此文件

      4。处理密码文件:

      vim /etc/postfix/main.cf
      
      vim /etc/postfix/main.cf
      
      vim /usr/local/etc/postfix/main.cf
      
      postmap /etc/postfix/sasl_passwd
      
      systemctl restart postfix.service
      
      service postfix onestart
      nano /etc/rc.conf
      
      Ubuntu、Fedora、CentOS、OpenSUSE、Arch Linux、Debian:

      vim /etc/postfix/main.cf
      
      vim /etc/postfix/main.cf
      
      vim /usr/local/etc/postfix/main.cf
      
      postmap /etc/postfix/sasl_passwd
      
      systemctl restart postfix.service
      
      service postfix onestart
      nano /etc/rc.conf
      
      对于freeBSD

      postmap /usr/local/etc/postfix/sasl_passwd
      
      4.1)重新启动后缀

      Ubuntu、Fedora、CentOS、OpenSUSE、Arch Linux、Debian:

      vim /etc/postfix/main.cf
      
      vim /etc/postfix/main.cf
      
      vim /usr/local/etc/postfix/main.cf
      
      postmap /etc/postfix/sasl_passwd
      
      systemctl restart postfix.service
      
      service postfix onestart
      nano /etc/rc.conf
      
      对于FreeBSD:

      vim /etc/postfix/main.cf
      
      vim /etc/postfix/main.cf
      
      vim /usr/local/etc/postfix/main.cf
      
      postmap /etc/postfix/sasl_passwd
      
      systemctl restart postfix.service
      
      service postfix onestart
      nano /etc/rc.conf
      

      保存然后运行以开始

      service postfix start
      
      5。在Gmail中启用“不太安全的应用程序” 使用以下链接的帮助

      6。发送测试电子邮件

      mail -s "subject" recever@domain.com
      
      按回车键

      根据需要添加邮件正文按enter键