mailx及sendEmail的基本用法比较

简介:

最近公司某个业务需要使用邮件提醒。于是就选了mailx和sendEmail比较,看看那个好用!

mailx

mailx安装我就不介绍了,很简单!我先介绍下系统的版本和mailx的版本

1
2
3
4
5
[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客户端给的授权码)

1
2
3
4
5
set  from=usernam@163.com
set  smtp=smtp.163.com
set  smtp-auth-user=username@163.com
set  smtp-auth-password= passwd
set  smtp-auth=login

测试下是否可以使用

1
cat  /opt/tesh  |mailx - v  -s  'hello'  '*****@163.com'

测试发现有时能发送,有时会退信并报以下错误:

1
2
3
4
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邮箱

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"

以上是个简单的脚本,当然标题和内容以及发件人你都可以传参进来。这样的话就不会再命令行中暴露密码。



本文转自 irow10 51CTO博客,原文链接:http://blog.51cto.com/irow10/1812638,如需转载请自行联系原作者

相关文章
|
存储 安全 Linux
|
人工智能 机器人 C++
【C++/Python】Windows用Swig实现C++调用Python(史上最简单详细,80岁看了都会操作)
【C++/Python】Windows用Swig实现C++调用Python(史上最简单详细,80岁看了都会操作)
|
Linux
Linux通过QQ邮箱账号使用mailx发送邮件
Linux通过QQ邮箱账号使用mailx发送邮件
491 2
|
11月前
|
存储 消息中间件 NoSQL
每日大厂面试题大汇总 —— 今日的是“京东-后端开发-一面”
文章汇总了京东后端开发一面的面试题目,包括ArrayList与LinkedList的区别、HashMap的数据结构和操作、线程安全问题、线程池参数、MySQL存储引擎、Redis性能和线程模型、分布式锁处理、HTTP与HTTPS、Kafka等方面的问题。
367 0
|
存储 Prometheus 监控
性能监控之初识 Prometheus
【8月更文挑战第2天】性能监控之初识 Prometheus
1601 17
|
存储 安全 数据安全/隐私保护
恶意软件 (Malware)
【8月更文挑战第17天】
463 2
|
机器学习/深度学习 定位技术
ICLR 2024 Spotlight:连续数值分布式表征加持,浙大UIUC让语言模型擅长表格预测
【6月更文挑战第23天】在ICLR 2024会议上,浙大和UIUC的研究团队推出TP-BERTa,一种改进的BERT模型,专为表格预测。通过将连续数值特征转为文本并利用自注意力机制,TP-BERTa能有效处理高维、异构表格数据,提高预测性能。预训练和微调策略使其在XGBoost等传统方法及FT-Transformer等深度学习模型中脱颖而出。论文链接:[anzIzGZuLi](https://openreview.net/pdf?id=anzIzGZuLi)
267 5
|
JavaScript 前端开发 数据安全/隐私保护
雪球JS逆向:阿里系加密acw_sc__v2和反debugger
雪球JS逆向:阿里系加密acw_sc__v2和反debugger
1234 1
|
数据采集 Python
python 如何url解码
【4月更文挑战第14天】
363 1
|
网络协议 Linux 网络架构
默认网关详解:网络通信的无声守护者
【4月更文挑战第22天】
4419 3