- 阿里云服务器是不允许使用25端口发邮件,25端口默认是被占用的,因此我们一般使用465端口号进行邮件的发送,先在阿里云上将465端口号开放
通过以上步骤获取授权码,复制下来
-
启动postfix
service postfix start
chkconfig postfix on
如果启动失败,运行
postfix check
如果出现 postfix: error while loading shared libraries: libmysqlclient.so.16: cannot open shared object file: No such file or directory ,则运行下面两行
rpm -qa|grep mysql
yum install mysql-libs
-
创建认证(关键)
- 在命令行中依次执行以下命令
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
cd /root/.certs
certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i qq.crt
-
配置main.rc
vim /etc/mail.rc set from=1614674987@qq.com ##你发邮件的邮箱 set smtp=smtps://smtp.qq.com:465 set smtp-auth-user=1614674987@qq.com ## qq邮箱 set smtp-auth-password=dqiqzlqpslvxdieb #你自己的授权码 set smtp-auth=login set smtp-use-starttls set ssl-verify=ignore set nss-config-dir=/root/.certs
-
下载 mailx
yum -y install mailx
-
配置阿里云的证书
chmod 644 /root/.cert/* cd /root/.certs # 一定要进入该目录下,要不然之后执行的命令会报错,或者是权限不是644 certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i qiye.aliyun.com.crt #qiye.aliyun.com.crt是阿里云的证书 echo -n | openssl s_client -connect smtp.qiye.aliyun.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/qiye.aliyun.com.crt certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/qiye.aliyun.com.crt certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/qiye.aliyun.com.crt #执行完以后再次查看发现qiye.aliyun.com.crt存在了,之后执行 certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i qiye.aliyun.com.crt # Notice: Trust flag u is set automatically if the private key is present. 【这才是正确的结果】
测试一下
echo "hello word" | mail -s "title" 1614674987@qq.com
(收邮件的邮箱)# 如果出现 SSL/TLS handshake failed: SSL received a malformed Alert record. "/root/dead.letter" 11/306 . . . message not sent.
在
/etc/main.rc
中把set smtp-use-starttls
给去掉, 之后再测试一下,即可收到邮箱
-
作用
用命令行发邮件的作用是可以将命令写在脚本之中,从而实现自动化,可以用在自动化部署当中,如果部署完成了可以发一封邮件告诉你部署成功或失败
感谢大佬的文章使用阿里云服务器写shell脚本发送qq邮件的三个形式