5.7 KiB
5.7 KiB
name, description
| name | description |
|---|---|
| Send and receive emails via SMTP/IMAP with support for attachments, CC/BCC, HTML content, and multiple accounts. Use when user wants to send/receive emails, check unread messages, or view attachments. |
Email Sender / 邮件收发工具
通过 SMTP 发送邮件,通过 IMAP 接收邮件,支持附件、抄送/密送、HTML 格式和多账号管理。
快速开始
1. 配置邮箱账号
cd ~/.openclaw/workspace-coder/skills/email-sender/scripts
python3 send_email.py config my-email \
--server mail.tphai.com \
--port 587 \
--email guwen@tphai.com \
--password "your-password" \
--no-tls
参数说明:
my-email: 配置名称(可自定义)--server: SMTP 服务器地址--port: SMTP 端口--email: 邮箱地址--password: 邮箱密码--no-tls: 不使用 TLS 加密(默认使用 TLS)
配置后会自动用于发送和接收邮件。
2. 查看配置
python3 send_email.py list
发送邮件
简单邮件
python3 send_email.py send \
--to wlq@tphai.com \
--subject "测试邮件" \
--body "这是一封测试邮件"
带附件的邮件
python3 send_email.py send \
--to wlq@tphai.com \
--subject "带附件的邮件" \
--body "请查收附件" \
--attach /path/to/file1.pdf \
--attach /path/to/file2.jpg
HTML 邮件
python3 send_email.py send \
--to wlq@tphai.com \
--subject "HTML 邮件" \
--body "<h1>标题</h1><p>内容</p>" \
--html
抄送和密送
python3 send_email.py send \
--to wlq@tphai.com \
--cc "cc1@example.com,cc2@example.com" \
--bcc "bcc@example.com" \
--subject "抄送测试" \
--body "这是一封抄送测试邮件"
接收邮件
查看未读邮件
python3 receive_email.py unread
输出示例:
📬 未读邮件 (3 封)
============================================================
1. [42] 📎
主题: 项目报告
发件人: boss@company.com
时间: Fri, 10 Apr 2026 10:30:00 +0800
2. [41]
主题: 会议通知
发件人: hr@company.com
时间: Fri, 10 Apr 2026 09:15:00 +0800
📎表示有附件[数字]是邮件ID,用于后续读取
读取邮件详情
python3 receive_email.py read 42
输出示例:
📧 邮件详情
============================================================
ID: 42
主题: 项目报告
发件人: boss@company.com
收件人: me@company.com
时间: Fri, 10 Apr 2026 10:30:00 +0800
正文:
----------------------------------------
请查看附件中的项目报告...
附件:
----------------------------------------
📝 report.txt (1234 bytes)
内容预览: 第一行内容...
📄 data.csv (5678 bytes)
📝表示文本附件(已自动读取内容)📄表示非文本附件
查看附件内容
# 查看指定邮件的文本附件
python3 receive_email.py attachment 42
# 查看指定附件
python3 receive_email.py attachment 42 --filename report.txt
附件会自动保存到 scripts/attachments/<邮件ID>/ 目录。
Python API 使用
发送邮件
from send_email import send_email, setup_account
# 配置 SMTP
setup_account(
name="work",
smtp_server="mail.tphai.com",
smtp_port=587,
email="guwen@tphai.com",
password="your-password",
use_tls=False
)
# 发送简单邮件
send_email(
to="wlq@tphai.com",
subject="测试",
body="内容"
)
# 发送带附件的邮件
send_email(
to="wlq@tphai.com",
subject="附件测试",
body="请查收附件",
attachments=["/path/to/file.pdf"],
verbose=True
)
# 发送 HTML 邮件
send_email(
to="wlq@tphai.com",
subject="HTML 测试",
body="<h1>Hello</h1><p>World</p>",
html=True
)
接收邮件
from receive_email import list_unread, read_email
# 列出未读邮件
emails = list_unread(limit=10, verbose=True)
for e in emails:
print(f"[{e['id']}] {e['subject']} - {e['from']}")
# 读取邮件详情
email_data = read_email(email_id="42")
print(f"主题: {email_data['subject']}")
print(f"正文: {email_data['body']}")
# 查看附件
for att in email_data['attachments']:
print(f"附件: {att['filename']}")
if att['is_text']:
print(f"内容: {att['content']}")
支持的文本附件类型
自动识别并读取以下类型的附件:
| 类型 | 扩展名 |
|---|---|
| 纯文本 | .txt, .md, .log |
| 数据文件 | .json, .csv, .xml, .yaml, .yml |
| 配置文件 | .ini, .cfg, .conf |
| 代码文件 | .py, .js, .html, .css, .sh, .bat |
配置文件
配置保存在 scripts/smtp_config.json 中:
{
"accounts": [
{
"name": "my-email",
"smtp_server": "mail.tphai.com",
"smtp_port": 587,
"email": "guwen@tphai.com",
"password": "your-password",
"use_tls": false
}
]
}
常用邮箱服务器设置
| 服务商 | SMTP | IMAP | TLS |
|---|---|---|---|
| Gmail | smtp.gmail.com:587 | imap.gmail.com:993 | 是 |
| Outlook | smtp.office365.com:587 | outlook.office365.com:993 | 是 |
| QQ邮箱 | smtp.qq.com:587 | imap.qq.com:993 | 是 |
| 163邮箱 | smtp.163.com:465 | imap.163.com:993 | 是 |
| 企业邮箱 | mail.tphai.com:587 | mail.tphai.com:143 | 否 |
故障排除
认证失败:
- 检查邮箱密码是否正确
- Gmail 需要使用「应用专用密码」而非登录密码
- QQ邮箱需要使用授权码而非登录密码
连接失败:
- 检查服务器地址和端口
- 检查防火墙设置
- 尝试切换 TLS/SSL 模式
IMAP 连接失败:
- 确认邮箱支持 IMAP 协议
- 检查 IMAP 端口是否正确(通常是 993 或 143)
- 某些邮箱需要在设置中启用 IMAP