阿里云服务器使用命令行上发送邮件

  1. 阿里云服务器是不允许使用25端口发邮件,25端口默认是被占用的,因此我们一般使用465端口号进行邮件的发送,先在阿里云上将465端口号开放
  2. image-20220926142721636

image-20220926142929890

通过以上步骤获取授权码,复制下来

  1. 启动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

  2. 创建认证(关键)

  • 在命令行中依次执行以下命令
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 给去掉, 之后再测试一下,即可收到邮箱

  1. 作用

    用命令行发邮件的作用是可以将命令写在脚本之中,从而实现自动化,可以用在自动化部署当中,如果部署完成了可以发一封邮件告诉你部署成功或失败

感谢大佬的文章使用阿里云服务器写shell脚本发送qq邮件的三个形式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值