阅读量:1
在 Ubuntu 上配置 Python 邮件发送功能
一 准备与方案选择
- 准备环境:确保已安装 Python 3 与 pip,Ubuntu 通常自带 smtplib 与 email 标准库,无需额外安装。
- 两种常见方案:
- 使用外部邮箱的 SMTP(如 Gmail/QQ/Outlook),简单快速,适合应用报警、注册验证等。
- 在服务器上部署 Postfix 作为本地 MTA,由 Python 通过本机 localhost:25 发送,适合系统级通知与内部服务。
二 方案一 使用外部邮箱 SMTP 发送(推荐)
- 开启邮箱的 SMTP 并获取凭证:
- 例如 QQ 邮箱需在设置中开启 SMTP 服务并获取授权码(注意:登录密码≠授权码)。
- Gmail 建议使用应用专用密码(App Password)。
- 常用 SMTP 服务器与端口:
- Gmail:smtp.gmail.com,587(STARTTLS)/ 465(SSL)
- Outlook:smtp-mail.outlook.com,587(STARTTLS)
- Yahoo:smtp.mail.yahoo.com,587(STARTTLS)
- 安全建议:将邮箱账号与密码放入环境变量,避免硬编码。
- 示例脚本(Python 3,发送文本与附件):
import os, smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
# 从环境变量读取敏感信息
sender = os.getenv("MAIL_USER")
password = os.getenv("MAIL_PASS")
receiver = "recipient@example.com"
# 创建邮件
msg = MIMEMultipart()
msg["From"] = sender
msg["To"] = receiver
msg["Subject"] = "Python 邮件测试"
# 正文
msg.attach(MIMEText("这是一封来自 Python 的测试邮件(文本)", "plain", "utf-8"))
# 附件
filename = "example.py"
with open(filename, "rb") as f:
part = MIMEBase("application", "octet-stream")
part.set_payload(f.read())
encoders.encode_base64(part)
part.add_header("Content-Disposition", f"attachment; filename={filename}")
msg.attach(part)
# 发送
try:
with smtplib.SMTP("smtp.gmail.com", 587) as server: # 如用 SSL 可改为 smtp.gmail.com:465 并使用 SMTP_SSL
server.starttls()
server.login(sender, password)
server.sendmail(sender, receiver, msg.as_string())
print("邮件发送成功")
except Exception as e:
print("发送失败:", e)
- 运行前准备:
- 安装依赖(如使用虚拟环境):
pip install py-email(可选,标准库已包含 email) - 设置环境变量:
export MAIL_USER=你的邮箱、export MAIL_PASS=你的授权码/应用专用密码
- 安装依赖(如使用虚拟环境):
三 方案二 使用 Postfix 作为本地 MTA 发送
- 安装与基础配置:
- 安装:
sudo apt update && sudo apt install postfix - 安装向导选择 Internet Site,并设置 系统邮件名称(FQDN)。
- 编辑
/etc/postfix/main.cf,确保包含:myhostname = your_fqdnmydomain = your_domainmyorigin = $mydomaininet_interfaces = all
- 重启:
sudo systemctl restart postfix
- 安装:
- Python 侧脚本(连接本机 SMTP,端口 25,通常无需认证):
import smtplib
from email.mime.text import MIMEText
msg = MIMEText("这是通过本机 Postfix 发送的测试邮件。", "plain", "utf-8")
msg["From"] = "you@your_domain"
msg["To"] = "recipient@example.com"
msg["Subject"] = "Postfix 测试"
try:
with smtplib.SMTP("localhost", 25) as server:
server.sendmail(msg["From"], [msg["To"]], msg.as_string())
print("邮件发送成功")
except Exception as e:
print("发送失败:", e)
- 说明:若本机作为公网 MTA 对外发信,需正确配置 DNS/MX、SPF、DKIM、DMARC 等,否则易被拒收或标记为垃圾邮件。
四 常见问题与排查
- 身份验证失败:确认是否开启 SMTP 并使用授权码/应用专用密码;核对账号与密码是否正确。
- 连接或端口被拒:
- 使用外部 SMTP 时,云厂商可能限制 25 端口,优先用 587/465;
- 本机 Postfix 需确保服务运行并监听 25 端口。
- 被标记为垃圾邮件:完善发信域名的 SPF/DKIM/DMARC 记录,避免高频群发与垃圾内容。
- 编码与附件:附件请使用二进制读取并按 Base64 编码,正文可指定 utf-8。
五 Web 框架与第三方库
- Flask 应用可使用 Flask-Mail:配置
MAIL_SERVER/MAIL_PORT/MAIL_USE_TLS/MAIL_USERNAME/MAIL_PASSWORD,通过环境变量管理凭证,示例见常见实践。 - 第三方库 yagmail 可进一步简化发送流程(支持 HTML、附件、内嵌图片等)。
以上就是关于“如何在Ubuntu上配置Python邮件发送功能”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm