如何使⽤sendEmail发送邮件
sendEmail是⼀个轻量级,命令⾏的SMTP邮件客户端。如果你需要使⽤命令⾏发送邮件,那么sendEmail是⾮常完美的选择:使⽤简单并且功能强⼤.这个被设计⽤在、bash、和web站点使⽤。
以上是sendEmail的简单介绍,千万不要和sendmail搞混掉了。⽤了sendEmail你将不在喜欢sendmail了.
下载安装sendEmail
qq怎么发邮件# sendEmail下载地址:caspian.dotconf/menu/Software/SendEmail/
wget caspian.dotconf/menu/Software/SendEmail/sendEmail-v1. //下载1.56版本
tar -xzvf sendEmail-v1. //解压后就可以使⽤了
cp sendEmail-v1.56/sendEmail /usr/local/bin/
chmod +x /usr/local/bin/sendEmail
安装组件
yum install perl-Net-SSLeay perl-IO-Socket-SSL
发送测试邮件
# sendEmail -f a@domain -t test@qq \
-s smtp.163 -u "我是邮件主题" -o message-content-type=html \
-o message-charset=utf8 -xu ttlsafrom@163 -xp 123456 -m "我是邮件内容"
发送成功之后会有如下提⽰:
Jul 29 15:02:53 e10074 sendEmail[26347]: Email was sent successfully!
命令说明:
/usr/local/bin/sendEmail 命令主程序
-f ttlsafrom@163 发件⼈邮箱
-t test@qq 收件⼈邮箱
-
s smtp.163 发件⼈邮箱的smtp服务器
-u "我是邮件主题" 邮件的标题
-o message-content-type=html 邮件内容的格式,html表⽰它是html格式
-o message-charset=utf8 邮件内容编码
-xu ttlsafrom@163 发件⼈邮箱的⽤户名
-xp 123456 发件⼈邮箱密码
-m "我是邮件内容" 邮件的具体内容
编写脚本
# 进⼊zabbix⾃定义的指定⽬录
# 可以查看f配置⽂件AlertScriptsPath变量是如何定义的。
cd /usr/local/zabbix/share/zabbix/alertscripts/
-
------------------------------
编辑脚本
vim sendEmail.sh
内容如下:
#!/bin/bash
to=$1
subject=$2
body=$3
/usr/local/bin/sendEmail -f a@domain -t "$to" -ail.qq -u "$subject" -o message-content-type=html -o message-charset=utf8 -
说明:
上⾯有4个地⽅我⽤紫⾊加粗字体表⽰了
a@domain 表⽰发件⼈邮箱
password 表⽰发件⼈邮箱
编辑完成后,给脚本权限
chmod +x zabbix_sendEmail.sh
chown -R zabbix.zabbix zabbix_sendEmail.sh
sendEmail使⽤命令帮助
[root@li229-122 scripts]# sendEmail --help
sendEmail-1.56 by Brandon Zehm <caspian@dotconf>
Synopsis: sendEmail -f ADDRESS [options]
Required:
-f ADDRESS from (sender) email address
* At least one recipient required via -t, -cc, or -bcc
* Message body required via -m, STDIN, or -o message-file=FILE
Common:
-t ADDRESS [ADDR ...] to email address(es)
-u SUBJECT message subject
-m MESSAGE message body
-s SERVER[:PORT] smtp mail relay, default is localhost:25
Optional:
-a FILE [FILE ...] file p_w_upload(s)
-cc ADDRESS [ADDR ...] cc email address(es)
-
bcc ADDRESS [ADDR ...] bcc email address(es)
-xu USERNAME username for SMTP authentication
-xp PASSWORD password for SMTP authentication
Paranormal:
-b BINDADDR[:PORT] local host bind address
-l LOGFILE log to the specified file
-v verbosity, use multiple times for greater effect
-q be quiet (i.e. no STDOUT output)
-o NAME=VALUE advanced options, for details try: --help misc
-o message-content-type=<auto|text|html>
-o message-file=FILE -o message-format=raw
-
o message-header=HEADER -o message-charset=CHARSET
-o reply-to=ADDRESS -o timeout=SECONDS
-o username=USERNAME -o password=PASSWORD
-o tls=<auto|yes|no> -o fqdn=FQDN
Help:
--help the helpful overview you're reading now
--help addressing explain addressing and related options
--help message explain message body input and related options
--help networking explain -s, -b, etc
--help output explain logging and other output options
--help misc explain -o options, TLS, SMTP auth, and more
在使⽤腾讯过程中,经常出现邮件发不出去的情况,返回以下报错:
Bash
Nov 03 14:39:58 localhost sendEmail[26222]: WARNING => The recipient <xxx@xxx> was rejected by the mail server, error follows:
Nov 03 14:39:58 localhost sendEmail[26222]: WARNING => ail.qq:25 returned a zero byte response to our query.
Nov 03 14:39:58 localhost sendEmail[26222]: ERROR => Exiting. No recipients were accepted for delivery by the mail server.
⽹上还不到具体原因,猜想可能是腾讯的安全认证级别太⾼,不加密的smtp转发被判定为垃圾邮件的原因吧。查看的帮助说明,smtp配置都是使⽤的465端⼝ssl加密。所以,我们使⽤sendEmail时候,需要使⽤tls加密,即再加上⼀个“ -o tls=yes ”的参数。⽽要使⽤tls,要先给系统安装相应的依赖组件:
Bash
# yum install -y perl perl-IO-Socket-SSL perl-Net-SSLeay
装完先发送⼀个测试邮件试试,OK了再更新到nagios command。
发布评论