答:使用mailx命令可以从Linux Shell脚本发送电子邮件。您需要设置邮件服务器的名称,并确保您具有该服务器的访问权限。
如果服务器配置良好,例如已经安装并运行了MTA(邮件传输代理),你可以直接使用mail命令。
例如,要发送一个文件的内容,可以这样做:
$ cat /path/to/file | mail -s "your subject" your@email.com
man mail
了解更多详情。
sudo apt-get install mailutils
,然后选择“Internet site: Mail is sent and received directly using SMTP”。 - user1717828如果你想在bash中采用简洁的方式,而且不想使用cat
、echo
等命令,最简单的方法如下:
mail -s "subject here" email@address.com <<< "message"
<<<
被用于重定向标准输入。它已经是bash的一部分很长一段时间了。"cat << END
... END | mail -s "subject" test@example.com
- ulidtko#!/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 $?
在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
/tmp/mailtest
都会被覆盖。mail
命令来使用本地MTA发送消息(该命令将使用SMTP将其传递到目的地,或者只是将其转发到某个更强大的SMTP服务器,例如您的ISP)。如果您没有本地MTA(尽管UNIX类系统省略它有点不寻常),则可以使用一些极简的MTA,如ssmtp。
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
使用Postfix
1:安装软件
Debian和Ubuntu:
apt-get update && apt-get install postfix mailutils
OpenSUSE:
zypper update && zypper install postfix mailx cyrus-sasl
Fedora:
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
/etc/postfix。创建或编辑密码文件:
vim /etc/postfix/sasl_passwd
[smtp.gmail.com]:587 user@gmail.com:password
保存并关闭文件,并使其仅由root用户访问:因为它包含敏感内容,例如您的密码。
chmod 600 /usr/local/etc/postfix/sasl_passwd
>FreeBSD:
目录 /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. Postfix配置
配置文件main.cf
我们必须在Postfix中设置6个参数
Ubuntu、Arch Linux、Debian:
编辑
vim /etc/postfix/main.cf
修改以下数值:
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_options指配置文件中设置为空白,以确保不使用Gmail不兼容的安全选项。
保存并关闭。
如同对于OpenSUSE:
vim /etc/postfix/main.cf
修改
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
这还需要对文件 master.cf 进行配置。
修改:
vim /etc/postfix/master.cf
#tlsmgr unix - - n 1000? 1 tlsmg
vim /etc/postfix/main.cf
修改
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
自由BSD:
vim /usr/local/etc/postfix/main.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. 处理密码文件:
Ubuntu、Fedora、CentOS、OpenSUSE、Arch Linux、Debian:
postmap /etc/postfix/sasl_passwd
适用于 FreeBSD
postmap /usr/local/etc/postfix/sasl_passwd
4.1) 重新启动邮件服务器
Ubuntu、Fedora、CentOS、OpenSUSE、Arch Linux、Debian:
systemctl restart postfix.service
对于 FreeBSD:
service postfix onestart
nano /etc/rc.conf
添加
postfix_enable=YES
保存并运行以启动
service postfix start
5. 开启Gmail中的“较不安全应用程序”,请参考以下链接:
https://support.google.com/accounts/answer/6010255
6. 发送测试邮件
mail -s "subject" recever@domain.com
按下回车键
随意添加邮件正文,按下回车键,然后按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
如果您想使用某个 SMTP 服务器,可以执行以下操作:
export SUBJECT=some_subject
export smtp=somehost:someport
export EMAIL=someaccount@somedomain
echo "some message" | mailx -s "$SUBJECT" "$EMAIL"
somehost
,someport
和someaccount@somedomain
更改为您实际使用的值。
此示例中不执行加密和身份验证。mailx
,该怎么办? - nalply
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 #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 developer@xxx.com #send to group user 'developer@xxxx.com'
mail
命令可以实现这一功能(谁能想到呢 ;-)。打开你的终端并输入 man mail
来获取 mail
命令的手册页,了解所有可用选项。