centos配置邮箱服务

1、安装邮箱服务

yum -y install sendmail mailx

2、修改配置 vim /etc/mail.rc

set smtp=smtps://smtp.163.com
set from=aaa@aaa.com   #发送方
set smtp-auth-user=aaa@aaa.com #发送方用户名
set smtp-auth-password=*********  #密码(非邮箱登录密码)或者密钥
set nss-config-dir=/root/.certs     #证书位置
set ssl-verify=ignore
set smtp-auth=login

3、生成证书文件:
顺序执行如下命令即可生成证书:

echo -n | openssl s_client -connect smtp.163.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/163.crt
certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/163.crt
certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/163.crt
certutil -L -d /root/.certs

生成证书时,根据邮件服务器的不同可以自己调整

4、为了防止出现前文所说的发送邮件警告提示,还需要进入邮箱 SSL 证书存放目录 /root/.certs 里执行如下命令

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

5、可以发送一封邮箱测试一下

echo date | mailx -s "data" -a "/root/a.txt" 123456@qq.com

mailx只支持单个附件发送,如果需要发送多个,可以压缩成文件

 

注意:在脚本中添加证书更新代码

# 检查证书是否即将过期(30天内)
check_cert_expiry() {
    local cert_file="/root/.certs/163.crt"
    local expiry_date=$(openssl x509 -in "$cert_file" -noout -enddate | cut -d= -f2)
    local expiry_timestamp=$(date -d "$expiry_date" +%s)
    local current_timestamp=$(date +%s)
    local days_remaining=$(( (expiry_timestamp - current_timestamp) / 86400 ))

    echo "警告: 证书将在 $days_remaining 天后过期!"

    if [ "$days_remaining" -lt 30 ]; then
        echo "警告: 证书将在 $days_remaining 天后过期!"
        # 自动重新获取证书
        echo -n | openssl s_client -connect smtp.163.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > /root/.certs/163.crt
        echo "证书已更新"
    fi
}

check_cert_expiry "$@"

 

posted @ 2024-07-30 09:21  大司徒  阅读(134)  评论(0)    收藏  举报