最近公司某个业务需要使用邮件提醒。于是就选了mailx和sendEmail比较,看看那个好用!
mailx
mailx安装我就不介绍了,很简单!我先介绍下系统的版本和mailx的版本
[root@localhost ~]# mailx -V 12.4 7/29/08 [root@localhost ~]# cat /etc/issue CentOS release 6.8 (Final) Kernel \r on an \m
mailx的配置文件在/etc/mail.rc(我是rpm安装的)
在配置文件中添加下面的代码(passwd写开通smtp客户端给的授权码)
set [email protected] set smtp=smtp.163.com set [email protected] set smtp-auth-password=passwd set smtp-auth=login
测试下是否可以使用
cat /opt/tesh |mailx -v -s 'hello' '*****@163.com'
测试发现有时能发送,有时会退信并报以下错误:
554 DT:SPM 163 smtp3,DdGowACX1p2fRn9X_lIIAA--.2018S2 1467958944,please see http://mail.163.com/help/help_spam_16.htm?ip=*.*.*.*&hostid=smtp3&time=1467958944 smtp-server: 554 DT:SPM 163 smtp3,DdGowACX1p2fRn9X_lIIAA--.2018S2 1467958944,please see http://mail.163.com/help/help_spam_16.htm?ip=*.*.*.*&hostid=smtp3&time=1467958944 "/root/dead.letter" 0/0 . . . message not sent.
554 DT:SPM 发送的邮件内容包含了未被许可的信息,或被系统识别为垃圾邮件。请检查是否有用户发送病毒或者垃圾邮件
查了下原因,因为163反垃圾邮件设置的原因,所有我采用了qq邮箱及hostmail
qq邮箱
set from=224******[email protected] set smtp=smtp.qq.com set smtp-auth-user=224******[email protected] set smtp-auth-password=euia********chb #授权码 set smtp-auth=login
错误提示:
530 Error: A secure connection is requiered(such as ssl). More information at http://service.mail.qq.com/cgi-bin/help?id=28
smtp-server: 530 Error: A secure connection is requiered(such as ssl). More information at http://service.mail.qq.com/cgi-bin/help?id=28
"/root/dead.letter" 19/438
. . . message not sent
于是我改了下配置文件添加了几个参数
set from=224******[email protected] set smtp=smtp.qq.com set smtp-auth-user=224******[email protected] set smtp-auth-password=euia********chb set smtp-auth=login set smtp-use-starttls set ssl-verify=ignore set nss-config-dir=/etc/pki/nssdb/
邮件发送成功,在/etc/pki/nssdb/有证书文件
当然还有两个方式可以获取证书文件
mkdir -p /root/.certs/ echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/qq.crt certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt certutil -L -d /root/.certs
然后mail.rc的配置文件改成
set from=224******[email protected] set smtp=smtp.qq.com set smtp-auth-user=224******[email protected] set smtp-auth-password=euia********chb set smtp-auth=login set smtp-use-starttls set ssl-verify=ignore set nss-config-dir=/root/.certs
有些邮箱无法通过上述方式获取证书,我们可以在装有火狐浏览器的服务器中把证书拷贝过来
[root@localhost test]# cd /home/test/.mozilla/firefox/twmiqm5n.default/ [root@localhost twmiqm5n.default]# ll *db -rw-------. 1 test test 65536 Jul 7 16:29 cert8.db -rw-------. 1 test test 16384 Jul 7 16:29 key3.db -rw-------. 1 test test 16384 May 28 20:38 secmod.db
把这个三个文件拷贝到指定文件下。比如:/root/.certs1
set from=224******[email protected] set smtp=smtp.qq.com set smtp-auth-user=224******[email protected] set smtp-auth-password=euia********chb set smtp-auth=login set smtp-use-starttls set ssl-verify=ignore set nss-config-dir=/root/.certs1
mailx的操作还是蛮简单的,就是一直报一个错误
Error in certificate: Peer's certificate issuer is not recognized.
证书没有获得认可
解决方法:找个存放证书的目录
[root@localhost .certs]# pwd /root/.certs [root@localhost .certs]# ll total 80 -rw-------. 1 root root 65536 Jul 8 15:13 cert8.db -rw-r--r--. 1 root root 2293 Jul 7 16:25 qq.crt -rw-------. 1 root root 16384 Jul 8 15:13 key3.db -rw-------. 1 root root 16384 Jul 7 16:24 secmod.db [root@localhost .certs]# certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i qq.crt Notice: Trust flag u is set automatically if the private key is present.
这样就不会报错了。
Resolving host smtp.qq.com . . . done. Connecting to *.*.*.* . . . connected. 220 smtp.qq.com Esmtp QQ Mail Server >>> EHLO localhost 250-smtp.qq.com 250-PIPELINING 250-SIZE 73400320 250-STARTTLS 250-AUTH LOGIN PLAIN 250-AUTH=LOGIN 250-MAILCOMPRESS 250 8BITMIME >>> STARTTLS 220 Ready to start TLS Comparing DNS name: "mx2.qq.com" ................................ Comparing DNS name: "smtp.qq.com" SSL parameters: cipher=AES-256, keysize=256, secretkeysize=256, issuer=CN=GeoTrust SSL CA - G3,O=GeoTrust Inc.,C=US subject=CN=pop.qq.com,OU=R&D,O=Shenzhen Tencent Computer Systems Company Limited,L=Shenzhen,ST=Guangdong,C=CN >>> EHLO localhost 250-smtp.qq.com 250-PIPELINING 250-SIZE 73400320 250-AUTH LOGIN PLAIN 250-AUTH=LOGIN 250-MAILCOMPRESS 250 8BITMIME >>> AUTH LOGIN 334 VXNlcm5hbWU6 >>> MjI0Njk0NTU1M0BxcS5jb20= 334 UGFzc3dvcmQ6 >>> ZXVpYXl1a3JrZ3JoZWNoYg== 235 Authentication successful >>> MAIL FROM:<22******[email protected]> 250 Ok >>> RCPT TO:250 Ok >>> DATA 354 End data with . >>> . 250 Ok: queued as >>> QUIT 221 Bye
sendEmail
下面来介绍sendEmail的使用
wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz tar -zxvf sendEmail-v1.56.tar.gz cd sendEmail-v1.56 mv sendEmail /usr/local/bin/ yum -y install perl-IO-Socket-SSL openssl-perl openssl-devel
sendEmail就可以使用了。应为sendEmail没有配置文件,所以在发送邮件的时候需要填上发件人的账户密码,没有mailx方便.
这是我们需要脚本辅助
#!/bash/sh from_email='[email protected]' smtp='smtp-mail.outlook.com:587' title='标题' to_email=`[email protected]` passwd='passwd' body=`cat test` /usr/sbin/sendEmail -o tls=yes -f "$from_email" -s "$smtp" -u "$title" -o message-content-type=text -o message-charset=utf8 -t "$to_email" -xu "$from_email" -xp "$passwd" -m "$body"
以上是个简单的脚本,当然标题和内容以及发件人你都可以传参进来。这样的话就不会再命令行中暴露密码。