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"

以上是個簡單的腳本,當然標題和內容以及發件人你都可以傳參進來。這樣的話就不會再命令行中暴露密碼。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM