记Linux下古老的发送邮件的方式-mail命令发送

今天同事突然说在阿里云ECS上面采用系统命令mail 发送邮件报错,勾起历史回忆,刚毕业参加工作那会写脚本工具,貌似用这样的方式居多。现在这种方式应该很少使用了吧,都是python封装工具,这里把使用 Linux mail如果正确配置发送邮件,重新梳理下


默认情况安装 mailx 之后,然后在 /etc/mail.rc 中配置好账号密码就可以正常使用 mail 来发送,比如操作如下:

# install mailx first
yum install -y mailx 

# add these information into /etc/mail.rc file 
# Add to send mail by linux system
set [email protected]
set smtp=smtp.exmail.qq.com
# notice: user must with @ksjgs.com as suffix
set [email protected]
set smtp-auth-password=xxxxxxpsw
set smtp-auth=login
set ssl-verify=ignore

验证发送

# send demo
echo "Linux mail with exmail" | mail -s 'Subjuct: Linux Mail Test' [email protected]

执行之后默认就会收到测试邮件


但是这种方式在阿里云上执行会报错如下:

could not connect: Connection timed out
“/root/dead.letter” 11/318
. . . message not sent.

这种方式在非阿里云上面是没有问题的,但是阿里云限制了25端口,所以如上方式是发送失败的,需要采用 tls 465 端口才行

那如何在 Linux下利用 mail 配置smtp采用TLS协议发送邮件 呢,具体实践如下:

1、获取对应服务的证书保存为crt文件

注意:一般我们通过浏览器 https 可以看到证书的信息,但是该信息是不包括证书的内容的,内容可以通过 openssl s_client 来获取,具体如下

openssl s_client.png

2、利用 certutil 命令进行证书管理,具体详见 man certutil 或者 certutil -H command
注意:GeoTrust SSL证书是数字证书的一种

certutil-1.png

certutil-2.png

3、列举打印证书


show certs.png

4、Try to verify sending mail

# modify /etc/mail.rc to add nss-config-dir item
set nss-config-dir=/etc/mail-certs

# send demo
echo "Linux mail with exmail" | mail -s 'Subjuct: Linux Mail Test' [email protected]

Refer:

https://blog.espnlol.com/?p=335

https://www.cnblogs.com/kungfupanda/p/4637740.html


补充知识

1、报错信息如下,就是因为阿里云屏蔽25端口导致

smtp ssl协议端口465;非ssl协议端口25

could not connect: Connection timed out
“/root/dead.letter” 11/318
. . . message not sent.

2、报错信息如下,一般是因为配置 smtp-auth-user 没有添加邮箱后缀,也就是说 user是 [email protected] 而不是 userxxx

root@pts/0 $ smtp-server: 535 Error: ��ʹ����Ȩ���¼�������뿴: 
"/root/dead.letter" 11/336
. . . message not sent.

你可能感兴趣的:(记Linux下古老的发送邮件的方式-mail命令发送)