centos下利用mail命令进行邮件发送

时间:2023-03-09 05:51:34
centos下利用mail命令进行邮件发送

centos下默认自带mail命令:

可以用如下命令查看存放位置:

which mail

结果如下:

centos下利用mail命令进行邮件发送

如果没有安装可以使用 如下命令安装

yum -y install mailx

利用mail命令进行邮件发送,需要利用到第三方邮件服务器,如163等,需要一个授权码来识别(注意不是邮箱密码),获取授权码过程如下:

centos下利用mail命令进行邮件发送

需要手机号验证开启,具体过程可以百度 163邮箱授权

参考: https://jingyan.baidu.com/article/aa6a2c149f7b250d4c19c4b3.html

授权码获取到后,按如下步骤配置

1、修改/etc/mail.rc

vim /etc/mail.rc

2、在最后添加如下配置:

# mail config
set from=***********@.com
set smtp=smtps://smtp.163.com:465
set smtp-auth-user=********@.com
set smtp-auth-password=*********
set smtp-auth=login
set nss-config-dir=/root/.certs
set ssl-verify=ignore

---说明
from:对方收到邮件时显示的发件人
smtp:指定第三方发邮件的smtp服务器地址
set smtp-auth-user:第三方发邮件的用户名
set smtp-auth-password:邮箱授权码
smtp-auth:SMTP的认证方式,默认是login,也可以改成CRAM-MD5或PLAIN方式
nss-config-dir: SSL验证信息存放位置,需要后面步骤创建
ssl-verify: SSL验证信息忽略

3、创建 /root/.certs 目录

mkdir -p /root/.certs

4、下载证书到 /root/.certs 目录,本例是下载163证书

a、向163请求证书

echo -n | openssl s_client -connect smtp..com: | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/.crt

b、添加一个证书到本地:

certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/.crt

c、再次添加一个证书到本地:

certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/.crt

d、标记证书被信任

cd /root/.certs/
certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i .crt

5、测试邮件发送,有两种方式,可以用mail命令也可以用mailx命令,如下:

a、使用mail命令 ,加上 -v 会显示发送过程,多个接收人直接在后面空格加上其他人邮箱地址:

echo "hallo" | mail -v -s "this is Test Mail" xxxxxx@qq.com bbbbbb@qq.com

b、使用mailx命令:

mailx -s "hello" xxxxxx@qq.com

回车后手动输入邮件正文,如下:

centos下利用mail命令进行邮件发送

然后按ctrl+d结束输入,自动发送

参考:

https://blog.xunxe.com/1579.html

http://blog.51cto.com/12832314/2125950