Centos下发送邮件用mailx或sendEmail
mailx官网:http://www.filewatcher.com/d/CentOS/x86_64/Applications/Internet/mailx-12.4-6.el6.x86_64.rpm.239876.html
一、rpm包下载地址:
wget ftp://ftp.iij.ad.jp/pub/linux/centos-vault/6.2/os/x86_64/Packages/mailx-12.4-6.el6.x86_64.rpm --2017-07-03 08:21:57-- ftp://ftp.iij.ad.jp/pub/linux/centos-vault/6.2/os/x86_64/Packages/mailx-12.4-6.el6.x86_64.rpm => ‘mailx-12.4-6.el6.x86_64.rpm’ Resolving ftp.iij.ad.jp (ftp.iij.ad.jp)... 202.232.140.70, 2001:240:bb8f::1:70 Connecting to ftp.iij.ad.jp (ftp.iij.ad.jp)|202.232.140.70|:21... connected. Logging in as anonymous ... Logged in! ==> SYST ... done. ==> PWD ... done. ==> TYPE I ... done. ==> CWD (1) /pub/linux/centos-vault/6.2/os/x86_64/Packages ... done. ==> SIZE mailx-12.4-6.el6.x86_64.rpm ... 239876 ==> PASV ... done. ==> RETR mailx-12.4-6.el6.x86_64.rpm ... done. Length: 239876 (234K) (unauthoritative) 100%[===========================================================================>] 239,876 177KB/s in 1.3s 2017-07-03 08:22:00 (177 KB/s) - ‘mailx-12.4-6.el6.x86_64.rpm’ saved [239876]
二、安装mailx
[root@sendmail tools]# rpm -ivh mailx-12.4-6.el6.x86_64.rpm warning: mailx-12.4-6.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID c105b9de: NOKEY Preparing... ################################# [100%] Updating / installing... 1:mailx-12.4-6.el6 ################################# [100%] [root@sendmail tools]# ll total 236 -rw-r--r-- 1 root root 239876 Jul 3 08:22 mailx-12.4-6.el6.x86_64.rpm [root@sendmail tools]# rpm -qa mailx mailx-12.4-6.el6.x86_64 [root@sendmail tools]# mailx -V 12.4 7/29/08
三、配置mailx (在配置文件结尾加入下面这段代码)
[root@sendmail tools]# vi /etc/mail.rc #send mail set from=zhhotdoor@163.com set smtp=smtp.163.com set smtp-auth-user=zhhotdoor@163.com set smtp-auth-password=******* #换成你邮件密码 set smtp-auth=login
四、发送邮件命令(测试用了163邮件,发送成功)
[root@sendmail tools]# echo hello word | mailx -v -s " title" zhhotdoor@163.com Resolving host smtp.163.com . . . done. Connecting to 123.125.50.135 . . . connected. 220 163.com Anti-spam GT for Coremail System (163com[20141201]) >>> EHLO sendmail.template.com 250-mail 250-PIPELINING 250-AUTH LOGIN PLAIN 250-AUTH=LOGIN PLAIN 250-coremail 1Uxr2xKj7kG0xkI17xGrU7I0s8FY2U3Uj8Cz28x1UUUUU7Ic2I0Y2UF3mSHyUCa0xDrUUUUj 250-STARTTLS 250 8BITMIME >>> AUTH LOGIN 334 dXNlcm5hbWU6 >>> emhob3Rkb29yQDE2My5jb20= 334 UGFzc3dvcmQ6 >>> aG90ZG9vcjEyMw== 235 Authentication successful >>> MAIL FROM:<zhhotdoor@163.com> 250 Mail OK >>> RCPT TO:<zhhotdoor@163.com> 250 Mail OK >>> DATA 354 End data with <CR><LF>.<CR><LF> >>> . 250 Mail OK queued as smtp5,D9GowADngWRuE1lZypxwAw--.24084S2 1499009903 >>> QUIT 221 Bye
五、查看邮件是否收到
六、错误问题
测试发现有时能发送,有时会退信并报以下错误:
>>> . 554 DT:SPM 163 smtp3,DdGowACnhqntEllZcKFeAw--.22918S2 1499009775,please see http://mail.163.com/help/help_spam_16.htm?ip=27.42.98.89&hostid=smtp3&time=1499009775 smtp-server: 554 DT:SPM 163 smtp3,DdGowACnhqntEllZcKFeAw--.22918S2 1499009775,please see http://mail.163.com/help/help_spam_16.htm?ip=27.42.98.89&hostid=smtp3&time=1499009775 "/root/dead.letter" 0/0 . . . message not sent.
554 DT:SPM 发送的邮件内容包含了未被许可的信息,或被系统识别为垃圾邮件。请检查是否有用户发送病毒或者垃圾邮件
查了下原因,因为163反垃圾邮件设置的原因,所有我采用了qq邮箱及hostmail
qq邮箱
1
2
3
4
5
|
set from=224******53@qq.com set smtp=smtp.qq.com set smtp-auth-user=224******53@qq.com 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
于是我改了下配置文件添加了几个参数
1
2
3
4
5
6
7
8
|
set from=224******53@qq.com set smtp=smtp.qq.com set smtp-auth-user=224******53@qq.com 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/有证书文件
当然还有两个方式可以获取证书文件
1
2
3
4
5
|
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的配置文件改成
1
2
3
4
5
6
7
8
|
set from=224******53@qq.com set smtp=smtp.qq.com set smtp-auth-user=224******53@qq.com set smtp-auth-password=euia********chb set smtp-auth=login set smtp-use-starttls set ssl-verify=ignore set nss-config- dir = /root/ .certs |
有些邮箱无法通过上述方式获取证书,我们可以在装有火狐浏览器的服务器中把证书拷贝过来
1
2
3
4
5
|
[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
1
2
3
4
5
6
7
8
|
set from=224******53@qq.com set smtp=smtp.qq.com set smtp-auth-user=224******53@qq.com 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.
证书没有获得认可
解决方法:找个存放证书的目录
1
2
3
4
5
6
7
8
9
10
|
[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. |
这样就不会报错了。
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
|
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******53@qq.com> 250 Ok >>> RCPT TO:<username@163.com> 250 Ok >>> DATA 354 End data with <CR><LF>.<CR><LF> >>> . 250 Ok: queued as >>> QUIT 221 Bye |
七、sendEmail(目前还没有测试)
下面来介绍sendEmail的使用
1
2
3
4
5
|
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方便.
这是我们需要脚本辅助
1
2
3
4
5
6
7
8
|
#!/bash/sh from_email= 'username@hotmail.com' smtp= 'smtp-mail.outlook.com:587' title= '标题' to_email=`username@qq.com` 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" |
以上是个简单的脚本,当然标题和内容以及发件人你都可以传参进来。这样的话就不会再命令行中暴露密码。