本技能使用 Python 标准库(smtplib + imaplib)实现完整的邮件收发管理,无需安装第三方依赖。
支持功能:
| 脚本 | 功能 |
|------|------|
| scripts/send_email.py | 邮件发送(SMTP,支持HTML+附件+回复/转发+重试)|
| scripts/mail_client.py | 邮件接收与管理(IMAP,兼容163 Unsafe Login)|
| scripts/email_config.py | 邮箱配置自动识别(根据邮箱地址匹配服务器配置)|
Python 运行路径(优先使用):
C:\Users\admin\.workbuddy\binaries\python\versions\3.13.12\python.exe
python scripts/email_config.py user@163.com
# 自动输出:SMTP服务器、IMAP服务器、端口、认证方式
大多数邮箱不支持账户密码,需生成授权码/应用密码:
见下方详细用法。
常用快查表(完整配置见 references/email_providers.md,也可用 email_config.py 自动查询):
| 邮箱 | SMTP | IMAP | SSL端口 | 认证方式 |
|------|------|------|---------|---------|
| 163 | smtp.163.com | imap.163.com | 465/993 | 授权码 |
| QQ | smtp.qq.com | imap.qq.com | 465/993 | 授权码 |
| 126 | smtp.126.com | imap.126.com | 465/993 | 授权码 |
| Gmail | smtp.gmail.com | imap.gmail.com | 587/993 | 应用密码 |
| Outlook | smtp-mail.outlook.com | imap-mail.outlook.com | 587/993 | 应用密码 |
| Yahoo | smtp.mail.yahoo.com | imap.mail.yahoo.com | 465/993 | 应用密码 |
| iCloud | smtp.mail.me.com | imap.mail.me.com | 587/993 | 应用密码 |
| Aliyun | smtp.aliyun.com | imap.aliyun.com | 465/993 | 授权码 |
| 139 | smtp.139.com | imap.139.com | 465/993 | 授权码 |
| 189 | smtp.189.cn | imap.189.cn | 465/993 | 授权码 |
python scripts/send_email.py \
--host smtp.163.com --port 465 \
--user tnktechqa@163.com --password "授权码" \
--to "收件人@example.com" \
--subject "邮件主题" \
--body "邮件正文内容"
# 直接传入HTML字符串
python scripts/send_email.py \
--host smtp.163.com --port 465 \
--user tnktechqa@163.com --password "授权码" \
--to "收件人@example.com" \
--subject "HTML邮件" \
--html "<h1>你好</h1><p>这是一封 <b>HTML</b> 邮件。</p>"
# 传入HTML文件路径(自动读取文件内容)
python scripts/send_email.py \
--host smtp.163.com --port 465 \
--user tnktechqa@163.com --password "授权码" \
--to "收件人@example.com" \
--subject "月报" \
--html "/path/to/report.html"
python scripts/send_email.py \
--host smtp.163.com --port 465 \
--user tnktechqa@163.com --password "授权码" \
--to "收件人@example.com" \
--subject "请查收报告" \
--html "<p>请查收附件中的报告。</p>" \
--attach "/path/to/report.pdf" "/path/to/data.xlsx"
python scripts/send_email.py \
--host smtp.163.com --port 465 \
--user tnktechqa@163.com --password "授权码" \
--to "a@example.com,b@example.com" \
--cc "boss@company.com" \
--subject "通知" \
--html "<p>内容</p>" \
--from-name "技术团队"
# 传入原始邮件的 Message-ID(查看邮件详情时获取)
python scripts/send_email.py \
--host smtp.163.com --port 465 \
--user tnktechqa@163.com --password "授权码" \
--to "sender@example.com" \
--subject "Re: 原始主题" \
--body "收到,谢谢!" \
--reply-to "<msgid@example.com>"
python scripts/send_email.py \
--host smtp.163.com --port 465 \
--user tnktechqa@163.com --password "授权码" \
--to "boss@company.com" \
--subject "紧急通知" \
--html "<p>紧急事项</p>" \
--priority high
import sys
sys.path.insert(0, "C:/Users/admin/.workbuddy/skills/email-master/scripts")
from send_email import send_email
result = send_email(
host="smtp.163.com",
port=465,
user="tnktechqa@163.com",
password="授权码",
to=["收件人@example.com"],
subject="主题",
html_body="<h1>HTML内容</h1><p>正文</p>",
attachments=["/path/to/file.pdf"],
from_name="发件人名称",
reply_to_msgid="<original-msg-id@example.com>",
priority="high",
max_retries=2,
)
print(result) # {"success": True, "message": "..."}
> 163/126/Yeah 邮箱注意:首次使用 IMAP 可能触发「Unsafe Login」安全限制。
> 本脚本已内置 ID 命令绕过机制。若仍被拒绝,请登录网页版邮箱确认授权。
> 详见下方「常见问题」。
python scripts/mail_client.py \
--host imap.163.com --user tnktechqa@163.com --password "授权码" \
folders
python scripts/mail_client.py \
--host imap.163.com --user tnktechqa@163.com --password "授权码" \
list --limit 20
python scripts/mail_client.py ... list --unread
# 按主题搜索
python scripts/mail_client.py ... search --subject "发票"
# 按发件人搜索
python scripts/mail_client.py ... search --from "hr@company.com"
# 按日期范围搜索(格式:DD-Mon-YYYY,如 01-May-2024)
python scripts/mail_client.py ... search --since "01-May-2024" --before "31-May-2024"
# 组合搜索
python scripts/mail_client.py ... search --subject "合同" --from "legal@firm.com" --unread
# 查看并自动标记已读
python scripts/mail_client.py ... view --uid 123
# 查看但不标记已读
python scripts/mail_client.py ... view --uid 123 --no-mark-read
# 删除单封
python scripts/mail_client.py ... delete --uid 123
# 批量删除
python scripts/mail_client.py ... delete --uid "123,456,789"
# 标记为已读
python scripts/mail_client.py ... mark --uid 123 --flag read
# 批量标记未读
python scripts/mail_client.py ... mark --uid "123,456" --flag unread
# 添加星标
python scripts/mail_client.py ... mark --uid 123 --flag flagged
python scripts/mail_client.py ... move --uid 123 --dest "已处理"
# 下载指定附件
python scripts/mail_client.py ... download --uid 123 --filename "report.pdf" --dir "/path/to/save"
# 下载全部附件
python scripts/mail_client.py ... download --uid 123 --dir "/path/to/save"
python scripts/mail_client.py ... stats
python scripts/mail_client.py ... list --json
python scripts/mail_client.py ... view --uid 123 --json
import sys
sys.path.insert(0, "C:/Users/admin/.workbuddy/skills/email-master/scripts")
from mail_client import MailClient
with MailClient("imap.163.com", 993, "tnktechqa@163.com", "授权码") as client:
# 列出邮件
emails = client.list_emails(limit=10)
for e in emails:
print(e["subject"], e["from"], e["date"])
# 搜索
results = client.search_emails(subject="发票", unread_only=True)
# 查看详情
mail = client.get_email("123", mark_read=False)
print(mail["subject"], mail["message_id"])
# 批量删除
client.delete_email("123,456,789")
# 下载全部附件
saved = client.download_all_attachments("123", save_dir="./downloads")
# 查询邮箱配置
python scripts/email_config.py user@163.com
python scripts/email_config.py someone@gmail.com --json
# 作为模块调用
from email_config import get_email_config
config = get_email_config("user@163.com")
print(config["smtp_host"], config["smtp_port"])
print(config["imap_host"], config["imap_port"])
print(config["auth_guide"])
支持的邮箱域名:163.com、126.com、yeah.net、qq.com、sina.com、sohu.com、aliyun.com、139.com、189.cn、21cn.com、gmail.com、outlook.com、hotmail.com、live.com、yahoo.com、icloud.com、me.com、zoho.com、fastmail.com、protonmail.com 等 30+ 域名。
| 错误 | 原因 | 解决方案 |
|------|------|---------|
| Authentication failed | 使用了账户密码而非授权码 | 在邮箱设置中生成授权码/应用密码 |
| Connection refused | 端口错误或服务未开启 | 检查端口,163/QQ需在网页端开启SMTP/IMAP |
| SSL error | 端口与加密方式不匹配 | 465用SSL,587用STARTTLS |
| 163 Unsafe Login | 网易邮箱安全保护 | 登录163网页版 → 设置 → POP3/SMTP/IMAP → 确认授权/开启IMAP;本脚本已内置ID命令绕过机制 |
| 163发送失败 | 未开启SMTP服务 | 登录163网页 → 设置 → POP3/SMTP/IMAP → 开启 |
| Gmail认证失败 | 未使用应用密码 | Google账号 → 安全 → 两步验证 → 应用密码 |
| SMTPServerDisconnected | 网络不稳定 | 已内置重试机制(默认2次),可 --retries 3 增加重试 |
| IMAP select失败 | 文件夹名错误 | 先用 folders 命令查看可用文件夹名 |
ID 命令自动发送客户端标识,可绕过大部分 Unsafe Login 检测
references/email_providers.md
scripts/email_config.py <邮箱地址>
scripts/send_email.py --help
scripts/mail_client.py --help
共 1 个版本