当前位置: 首页 > news >正文

如何使用sendEmail发送邮件

sendEmail是一个轻量级,命令行的SMTP邮件客户端。如果你需要使用命令行发送邮件,那么sendEmail是非常完美的选择:使用简单并且功能强大.这个被设计用在php、bash、perl和web站点使用。
以上是sendEmail的简单介绍,千万不要和sendmail搞混掉了。用了sendEmail你将不在喜欢sendmail了.

下载安装sendEmail

1
2
3
4
5
# sendEmail下载地址:http://caspian.dotconf.net/menu/Software/SendEmail/
wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz //下载1.56版本
tar -xzvf sendEmail-v1.56.tar.gz //解压后就可以使用了
cp sendEmail-v1.56/sendEmail /usr/local/bin/
chmod +x /usr/local/bin/sendEmail

安装组件

1
yum install perl-Net-SSLeay perl-IO-Socket-SSL


发送测试邮件

1
2
3
#  sendEmail -f a@domain.com -t test@qq.com \
     -s smtp.163.com -u "我是邮件主题" -o message-content-type=html \
     -o message-charset=utf8 -xu ttlsafrom@163.com -xp 123456 -m "我是邮件内容"

发送成功之后会有如下提示:
Jul 29 15:02:53 e10074 sendEmail[26347]: Email was sent successfully!


命令说明:

1
2
3
4
5
6
7
8
9
10
/usr/local/bin/sendEmail 命令主程序
-f ttlsafrom@163.com  发件人邮箱
-t test@qq.com        收件人邮箱
-s smtp.163.com       发件人邮箱的smtp服务器
-u "我是邮件主题"     邮件的标题
-o message-content-type=html   邮件内容的格式,html表示它是html格式
-o message-charset=utf8        邮件内容编码
-xu ttlsafrom@163.com          发件人邮箱的用户名
-xp 123456               发件人邮箱密码
-m "我是邮件内容"        邮件的具体内容


编写脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 进入zabbix自定义的指定目录
# 可以查看zabbix_server.conf配置文件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.com -t "$to" -s smtp.exmail.qq.com -u "$subject" -o message-content-type=html -o message-charset=utf8 -xua@domain.com -xppassword -m "$body"
  
说明:
上面有4个地方我用紫色加粗字体表示了
a@domain.com 表示发件人邮箱
smtp.exmail.qq.com 表示邮箱的smtp服务器,因为我是用的腾讯企业邮箱。如果是其他邮箱,需要修改
password 表示发件人邮箱
 
编辑完成后,给脚本权限
chmod +x  zabbix_sendEmail.sh
chown  -R zabbix.zabbix zabbix_sendEmail.sh


sendEmail使用命令帮助


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
[root@li229-122 scripts]# sendEmail --help
  
sendEmail-1.56 by Brandon Zehm < caspian @dotconf.net>
  
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 attachment(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.com> was rejected by the mail server, error follows:
Nov 03 14:39:58 localhost sendEmail[26222]: WARNING => smtp.exmail.qq.com: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。


微信报警实现:https://www.abcdocker.com/abcdocker/2573

本文转自奔跑在路上博客51CTO博客,原文链接http://blog.51cto.com/qiangsh/1954419如需转载请自行联系原作者


qianghong000

相关文章:

  • TightVNC 企业内部部署
  • 请读者帮忙投个票喔
  • 网吧游戏的三层更新
  • DOM常用属性和方法汇总
  • 享元模式(Flyweight)解析例子
  • msdb.dbo.suspect_pages
  • apache做反向代理服务器
  • 家庭调解员
  • XSS 攻击实验 防御方案
  • ORACLE 笔记
  • 浅析重定向与反弹Shell命令
  • JS设置Excel格式
  • Windows系统磁盘分区详解
  • 使用DELPHI编写一个小的控件
  • android Service控件
  • 时间复杂度分析经典问题——最大子序列和
  • 【MySQL经典案例分析】 Waiting for table metadata lock
  • 【剑指offer】让抽象问题具体化
  • 2018一半小结一波
  • IDEA常用插件整理
  • 从0到1:PostCSS 插件开发最佳实践
  • 汉诺塔算法
  • 解析 Webpack中import、require、按需加载的执行过程
  • 我是如何设计 Upload 上传组件的
  • 线上 python http server profile 实践
  • 详解移动APP与web APP的区别
  • 项目管理碎碎念系列之一:干系人管理
  • 译米田引理
  • 新海诚画集[秒速5センチメートル:樱花抄·春]
  • ​人工智能之父图灵诞辰纪念日,一起来看最受读者欢迎的AI技术好书
  • (Demo分享)利用原生JavaScript-随机数-实现做一个烟花案例
  • (Java实习生)每日10道面试题打卡——JavaWeb篇
  • (MIT博士)林达华老师-概率模型与计算机视觉”
  • (Python) SOAP Web Service (HTTP POST)
  • (Redis使用系列) Springboot 实现Redis 同数据源动态切换db 八
  • (Spark3.2.0)Spark SQL 初探: 使用大数据分析2000万KF数据
  • (附源码)基于SSM多源异构数据关联技术构建智能校园-计算机毕设 64366
  • (算法)Game
  • (一)使用IDEA创建Maven项目和Maven使用入门(配图详解)
  • (原创)攻击方式学习之(4) - 拒绝服务(DOS/DDOS/DRDOS)
  • (原創) 如何刪除Windows Live Writer留在本機的文章? (Web) (Windows Live Writer)
  • .describe() python_Python-Win32com-Excel
  • .dwp和.webpart的区别
  • .net core 6 redis操作类
  • .NET/C# 解压 Zip 文件时出现异常:System.IO.InvalidDataException: 找不到中央目录结尾记录。
  • .net最好用的JSON类Newtonsoft.Json获取多级数据SelectToken
  • /var/log/cvslog 太大
  • @TableLogic注解说明,以及对增删改查的影响
  • [ HTML + CSS + Javascript ] 复盘尝试制作 2048 小游戏时遇到的问题
  • [2009][note]构成理想导体超材料的有源THz欺骗表面等离子激元开关——
  • [28期] lamp兄弟连28期学员手册,请大家务必看一下
  • [Asp.net mvc]国际化
  • [C++] Boost智能指针——boost::scoped_ptr(使用及原理分析)
  • [CentOs7]iptables防火墙安装与设置
  • [C进阶] 数据在内存中的存储——浮点型篇