从linux shell脚本发送邮件

Zub*_*air 110 linux email shell sendmail

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

Fra*_*ita 114

如果服务器配置良好,例如它具有正在运行的MTA,则可以使用mail命令.

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

$ cat /path/to/file | mail -s "your subject" your@email.com
Run Code Online (Sandbox Code Playgroud)

man mail 更多细节.

  • 'mta'在这里是什么意思,有全名还是链接?作为初学者,这就是我想知道的.由于我缺乏这方面的经验. (5认同)
  • @Zen MTA代表邮件传输代理.postfix,sendmail,qmail等 (4认同)
  • 如果您不确定如何安装/配置并且您使用的是 Ubuntu:`sudo apt-get install mailutils` 并选择_Internet 站点:直接使用 SMTP 发送和接收邮件。_。 (2认同)

小智 84

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

mail -s "subject here" email@address.com <<< "message"
Run Code Online (Sandbox Code Playgroud)

<<<用于重定向标准输入.它长期以来一直是bash的一部分.

  • echo -e"Some \nMultiline and tab\t msg"| mail -s"subject"your@email.com (6认同)

hor*_*bzz 24

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

然后它非常简单,仅配置2个文件(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 $?  
Run Code Online (Sandbox Code Playgroud)

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


cha*_*n A 10

从 LINUX 发送邮件到 GMAIL

使用后缀

1: 安装软件

Debian 和 Ubuntu:

apt-get update && apt-get install postfix mailutils
Run Code Online (Sandbox Code Playgroud)

打开SUSE:

zypper update && zypper install postfix mailx cyrus-sasl
Run Code Online (Sandbox Code Playgroud)

软呢帽:

dnf update && dnf install postfix mailx
Run Code Online (Sandbox Code Playgroud)

CentOS:

yum update && yum install postfix mailx cyrus-sasl cyrus-sasl-plain
Run Code Online (Sandbox Code Playgroud)

拱门Linux:

pacman -Sy postfix mailutils
Run Code Online (Sandbox Code Playgroud)

自由BSD:

portsnap fetch extract update

cd /usr/ports/mail/postfix

make config
Run Code Online (Sandbox Code Playgroud)

在配置中选择 SASL 支持

make install clean

pkg install mailx
Run Code Online (Sandbox Code Playgroud)

2.配置Gmail

/etc/postfix.conf 创建或编辑密码文件:

vim /etc/postfix/sasl_passwd
Run Code Online (Sandbox Code Playgroud)

我使用 vim 你可以使用任何文件编辑器,如 nano、cat .....

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

添加这个

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

[smtp.gmail.com]:587    user@gmail.com:password
Run Code Online (Sandbox Code Playgroud)

保存并关闭文件并使其只能由 root 访问:因为它是包含您的密码的敏感内容

chmod 600 /usr/local/etc/postfix/sasl_passwd
Run Code Online (Sandbox Code Playgroud)

>自由BSD:

目录 /usr/local/etc/postfix.

vim /usr/local/etc/postfix/sasl_passwd
Run Code Online (Sandbox Code Playgroud)

添加行:

[smtp.gmail.com]:587    user@gmail.com:password
Run Code Online (Sandbox Code Playgroud)

保存并使其只能由 root 访问:

chmod 600 /usr/local/etc/postfix/sasl_passwd
Run Code Online (Sandbox Code Playgroud)

3.后缀配置

配置文件main.cf

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

Ubuntu、Arch Linux、Debian:

编辑

 vim /etc/postfix/main.cf
Run Code Online (Sandbox Code Playgroud)

修改以下值:

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
Run Code Online (Sandbox Code Playgroud)

smtp_sasl_security_options在配置中将设置为空,以确保不使用与 Gmail 不兼容的安全选项。

保存并关闭

就像对于

打开SUSE:

vim /etc/postfix/main.cf
Run Code Online (Sandbox Code Playgroud)

调整

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
Run Code Online (Sandbox Code Playgroud)

它还需要配置文件master.cf

调整:

vim /etc/postfix/master.cf
Run Code Online (Sandbox Code Playgroud)

通过取消注释此行(删除#)

#tlsmgr unix - - n 1000? 1 tlsmg
Run Code Online (Sandbox Code Playgroud)

保存并关闭

费多拉、CentOS:

vim /etc/postfix/main.cf
Run Code Online (Sandbox Code Playgroud)

调整

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
Run Code Online (Sandbox Code Playgroud)

自由BSD:

vim /usr/local/etc/postfix/main.cf
Run Code Online (Sandbox Code Playgroud)

调整:

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
Run Code Online (Sandbox Code Playgroud)

保存并关闭它

4. 处理密码文件:

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

postmap /etc/postfix/sasl_passwd
Run Code Online (Sandbox Code Playgroud)

免费BSD

postmap /usr/local/etc/postfix/sasl_passwd
Run Code Online (Sandbox Code Playgroud)

4.1) 重启后缀

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

systemctl restart postfix.service
Run Code Online (Sandbox Code Playgroud)

对于FreeBSD:

service postfix onestart
nano /etc/rc.conf
Run Code Online (Sandbox Code Playgroud)

添加

postfix_enable=YES
Run Code Online (Sandbox Code Playgroud)

保存然后运行开始

service postfix start
Run Code Online (Sandbox Code Playgroud)

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

https://support.google.com/accounts/answer/6010255

6. 发送测试电子邮件

mail -s "subject" recever@domain.com
Run Code Online (Sandbox Code Playgroud)

按回车键

根据需要添加邮件正文,按 Enter 键,然后按ctrl+d正确终止

如果它不起作用,请再次检查所有步骤,并检查您是否在 Gmail 中启用了“不太安全的应用程序”

如果你修改了其中的任何内容,则重新启动 postfix

对于 shell 脚本,创建 .sh 文件并根据您的要求添加 6 步骤命令

例如仅作为示例

#!/bin/bash
REALVALUE=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')
THRESHOLD=80

if [ "$REALVALUE" -gt "$THRESHOLD" ] ; then
    mail -s 'Disk Space Alert' mailid@domainname.com << EOF
Your root partition remaining free space is critically low. Used: $REALVALUE%
EOF
fi
Run Code Online (Sandbox Code Playgroud)

当磁盘使用率超过 THRESHOLD 变量指定的百分比(此处为 80%)时,脚本会发送电子邮件。


SPR*_*BRN 9

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

  • 被否决是因为多个进程可以同时调用该脚本。这可能会导致 tmp 文件损坏/不正确。此外,每次使用时,/tmp 文件都不会被覆盖 - 它实际上随着包含所有以前的电子邮件的每封电子邮件而增长。不好。 (2认同)

Gre*_*Cat 8

通常,您希望使用mail命令来使用本地MTA发送邮件(将使用SMTP将其传递到目标,或者只是将其转发到更强大的SMTP服务器,例如,在您的ISP).如果你没有本地MTA(尽管类UNIX系统省略一个有点不寻常),你可以使用像ssmtp这样的简约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
Run Code Online (Sandbox Code Playgroud)

另一种选择是使用直接连接到SMTP服务器的myriads脚本之一并尝试在那里发布消息,例如Smtp-Auth-Email-Script,smtp-cli,SendEmail等.


Ger*_*ano 6

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

export SUBJECT=some_subject
export smtp=somehost:someport
export EMAIL=someaccount@somedomain
echo "some message" | mailx -s "$SUBJECT" "$EMAIL"
Run Code Online (Sandbox Code Playgroud)

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

  • 如果没有安装“mailx”怎么办? (2认同)