当前位置: 首页 > news >正文

Linux服务器自动发送邮件

一、安装sendmail和mailx

yum -y install sendmail mailx

二、在qq邮箱官网生成授权码

 

三、运行如下脚本

smtp-auth-password为生成的授权码

#!/bin/bash
###########################################################   
# Script Name: addSmtpConf.sh   
# Description: SMTP Configuration Script   
#              - Automatically retrieves QQ Mail SSL certificate   
#              - Configures NSS certificate database   
#              - Updates mail client configuration   
# Author: XXX   
# Created: 2025-04-02   
# Version: 1.1   
# Usage:    
#    ./addSmtpConf.sh   
#    Requires root privileges   
############################################################ Start the sendmail service
systemctl start sendmail# Configuration variables
CERT_DIR="/root/.certs"
CERT_FILE="${CERT_DIR}/qq.crt"
MAILRC="/etc/mail.rc"# Create certificate directory if not exists
if [ ! -d "${CERT_DIR}" ]; thenmkdir -p "${CERT_DIR}"echo "Created certificate directory: ${CERT_DIR}"
fi# Retrieve QQ mail SSL certificate
echo "Fetching SSL certificate from smtp.qq.com..."
if ! echo | openssl s_client -connect smtp.qq.com:465 -servername smtp.qq.com 2>/dev/null | \sed -n '/BEGIN CERTIFICATE/,/END CERTIFICATE/p' > "${CERT_FILE}"
thenecho "Error: Failed to retrieve certificate"exit 1
fi# Add certificates to NSS database
for cert_name in "GeoTrust SSL CA" "GeoTrust Global CA"; docertutil -A -n "${cert_name}" -t "TC,," -d "${CERT_DIR}" -i "${CERT_FILE}" >/dev/null 2>&1
done
echo "Certificates added to NSS database:"
certutil -L -d "${CERT_DIR}"# Mail configuration parameters
declare -A MAIL_CONFIG=(["from"]="?????????@qq.com"["smtp"]="smtps://smtp.qq.com:465"["smtp-auth-user"]="?????????@qq.com"["smtp-auth-password"]="????????????????"["ssl-verify"]="ignore"["nss-config-dir"]="/root/.certs"["smtp-auth"]="login"
)# Update mail configuration
echo "Checking mail configuration..."
for key in "${!MAIL_CONFIG[@]}"; dovalue="${MAIL_CONFIG[$key]}"config_line="set ${key}=${value}"# Check if configuration existsif ! grep -qF "${config_line}" "${MAILRC}" 2>/dev/null; thenecho "Adding configuration: ${config_line}"echo "${config_line}" | tee -a "${MAILRC}" >/dev/nullfi
doneecho "Configuration completed successfully."

四、测试可否成功发送邮件

echo "test" | mail -s "test" ?????????@qq.com

相关文章:

  • 提示词Prompts(2)
  • 图像处理算法的学习笔记
  • Python6.13打卡(day45)
  • YOLOV11 中的 DFL Loss解读
  • 如何运营一个专业的体育比分网站
  • 【DVWA系列】——xss(Stored)——High详细教程
  • Go并发编程中的内存同步与竞态:从理论到实践
  • 深度学习笔记26-天气预测(Tensorflow)
  • 华为数字化转型进阶——精读188页华为EBPM数字化全要素流程管理方法论【附全文阅读】
  • 泰国电商系统简单纪要
  • Agent 处理流程
  • Arduino学习-红外感应
  • 基于通义灵码2.5的智能天气预警系统开发全记录
  • SQL注入部分理论入门学习
  • LLM-大模型原理
  • Elasticsearch 批量创建索引实践与优化建议
  • LeetCode - 76. 最小覆盖子串
  • Linux文件权限详解:从入门到精通
  • Deepseek+python - 自动图表生成
  • 自定义表单右侧属性使用v-bind绑定渲染
  • 网站设计广州/深圳百度公司地址在哪里
  • 河南省住房城乡建设厅官方网站/品牌线上推广方案
  • 容桂网站制作动态/如皋网站制作
  • soho怎么做网站/网站建设设计
  • 网络营销课程实训报告/广州网站优化服务商
  • 沧州市有建网站的吗/曼联目前积分榜