name: email-operations description: | 郵件操作技能,通過 SMTP/IMAP 協議讀取和傳送郵件。當用戶想要檢視郵件、搜尋郵件內容、 獲取收件箱列表、拉取特定郵件詳情、傳送郵件、或需要與郵箱服務互動時,使用此技能。 無論使用者是否明確提到 "IMAP" 或 "SMTP",只要涉及郵件收發操作,均可觸發此技能。 適用場景:檢視收件箱、搜尋郵件、按條件過濾、獲取郵件正文和附件資訊、傳送郵件、群發郵件等。 version: 2.0.0
通過 SMTP/IMAP 協議讀取和管理郵件的技能,支援郵件收發全流程操作。
重要:憑證儲存在技能目錄下的 .env 檔案中,而非系統環境變數。
使用此技能前,請先配置 skills/email-operations/.env 檔案:
# 郵箱基礎配置
EMAIL_ADDRESS=your@email.com
EMAIL_IMAP_PASSWORD=your_imap_password
EMAIL_SMTP_PASSWORD=your_smtp_password
# IMAP 伺服器配置(可選,有預設值)
EMAIL_IMAP_HOST=imap.gmail.com
EMAIL_IMAP_PORT=993
# SMTP 伺服器配置(可選,有預設值)
EMAIL_SMTP_HOST=smtp.gmail.com
EMAIL_SMTP_PORT=587
EMAIL_SMTP_SSL=false
Gmail:
EMAIL_ADDRESS=your@gmail.com
EMAIL_IMAP_HOST=imap.gmail.com
EMAIL_IMAP_PORT=993
EMAIL_SMTP_HOST=smtp.gmail.com
EMAIL_SMTP_PORT=587
注意:需要開啟"應用專用密碼"
QQ/foxmail:
EMAIL_ADDRESS=your@qq.com
EMAIL_IMAP_HOST=imap.qq.com
EMAIL_IMAP_PORT=993
EMAIL_SMTP_HOST=smtp.qq.com
EMAIL_SMTP_PORT=587
EMAIL_SMTP_SSL=false
163/188:
EMAIL_ADDRESS=your@163.com
EMAIL_IMAP_HOST=imap.163.com
EMAIL_IMAP_PORT=993
EMAIL_SMTP_HOST=smtp.163.com
EMAIL_SMTP_PORT=465
EMAIL_SMTP_SSL=true
Outlook:
EMAIL_ADDRESS=your@outlook.com
EMAIL_IMAP_HOST=outlook.office365.com
EMAIL_IMAP_PORT=993
EMAIL_SMTP_HOST=smtp.office365.com
EMAIL_SMTP_PORT=587
| 功能 | 方法 | 說明 |
|---|---|---|
| 獲取收件箱 | get_inbox() |
返回郵件列表摘要 |
| 獲取詳情 | get_email() |
返回完整郵件內容 |
| 搜尋郵件 | search_emails() |
按關鍵詞/發件人/日期搜尋 |
| 儲存附件 | save_attachment() |
下載附件到本地 |
| 資料夾列表 | get_folders() |
獲取郵箱資料夾 |
| 功能 | 方法 | 說明 |
|---|---|---|
| 傳送文本郵件 | send_email() |
傳送純文本郵件 |
| 傳送HTML郵件 | send_html_email() |
傳送 HTML 格式郵件 |
| 傳送帶附件 | send_email_with_attachments() |
新增附件傳送 |
from scripts.email_client import EmailClient
client = EmailClient()
# 獲取收件箱郵件列表
emails = client.get_inbox(count=10)
for email in emails:
print(f"主題: {email['subject']}")
print(f"發件人: {email['from']}")
print(f"時間: {email['date']}")
print("---")
# 按 UID 獲取郵件詳情
email = client.get_email(uid=123)
print(f"正文: {email['body']['plain']}")
print(f"附件: {email['attachments']}")
# 搜尋郵件
results = client.search_emails(
keyword="發票",
from_addr="zhangsan@company.com",
date_from="2024-01-01",
date_to="2024-12-31"
)
# 傳送文本郵件
client.send_email(
to="recipient@example.com",
subject="郵件主題",
body="這是郵件正文內容"
)
# 傳送 HTML 郵件
client.send_html_email(
to="recipient@example.com",
subject="HTML 郵件",
html_body="<h1>標題</h1><p>正文內容</p>"
)
# 傳送帶附件的郵件
client.send_email_with_attachments(
to="recipient@example.com",
subject="帶附件的郵件",
body="請查收附件",
attachments=[
"./files/document.pdf",
"./files/image.png"
]
)
# 群發郵件
recipients = ["user1@example.com", "user2@example.com", "user3@example.com"]
for recipient in recipients:
client.send_email(
to=recipient,
subject="群發郵件",
body="這是一封群發郵件"
)
| 方法 | 說明 | 引數 |
|---|---|---|
get_inbox() |
獲取收件箱郵件列表 | count=10, offset=0, folder="INBOX" |
get_email() |
獲取單封郵件詳情 | uid, folder="INBOX" |
search_emails() |
搜尋郵件 | keyword, from_addr, to_addr, date_from, date_to, folder |
save_attachment() |
儲存附件 | email_uid, attachment_index, save_path |
get_folders() |
獲取資料夾列表 | 無 |
| 方法 | 說明 | 引數 |
|---|---|---|
send_email() |
傳送文本郵件 | to, subject, body, cc=None, bcc=None |
send_html_email() |
傳送HTML郵件 | to, subject, html_body, cc=None, bcc=None |
send_email_with_attachments() |
傳送帶附件郵件 | to, subject, body, attachments, cc, bcc |
訪問小蔥技能站7w4.net,解鎖更多實用的AI技能外掛。
{
"uid": 12345,
"subject": "郵件主題",
"from": "sender@example.com",
"from_name": "發件人姓名",
"to": "recipient@example.com",
"date": "2024-01-15 10:30:00",
"size": 1024,
"flags": ["已讀", "星標"],
"is_read": True
}
{
"uid": 12345,
"subject": "郵件主題",
"from": {"name": "發件人", "email": "sender@example.com"},
"to": [{"name": "收件人", "email": "recipient@example.com"}],
"date": "2024-01-15 10:30:00",
"body": {
"plain": "純文本內容",
"html": "<html>...</html>"
},
"attachments": [
{"index": 0, "name": "file.pdf", "size": 102400, "type": "application/pdf"}
],
"headers": {"Message-ID": "...", "References": "..."}
}
# 列出收件箱郵件
python -m scripts.email_client --action list --count 10
# 檢視郵件詳情
python -m scripts.email_client --action show --uid 12345
# 搜尋郵件
python -m scripts.email_client --action search --keyword "發票"
# 檢視資料夾
python -m scripts.email_client --action folders
# 傳送測試郵件
python -m scripts.email_client --action send \
--to "recipient@example.com" \
--subject "測試郵件" \
--body "這是測試郵件的內容"
# 從 UTF-8 檔案讀取正文後傳送,適合 Windows 下發送中文郵件
python -m scripts.email_client --action send \
--to "recipient@example.com" \
--subject "天氣簡報" \
--body-file "./mail_body.txt"
# 傳送 HTML 檔案內容
python -m scripts.email_client --action send \
--to "recipient@example.com" \
--subject "HTML 郵件" \
--html-body-file "./mail_body.html"
A: 請確保: 1. 郵箱地址正確 2. 使用的是"應用專用密碼"而非登入密碼(Gmail/QQ等) 3. IMAP 和 SMTP 服務已在郵箱設定中開啟
A: 指令碼已內建編碼檢測和轉換,會自動處理 GBK、UTF-8 等編碼。
A: 一般郵箱對附件大小有限制(Gmail 通常 25MB),請確保附件在此範圍內。
========================================
📧 收件箱 - 最新郵件
========================================
[1] 主題: 會議通知
發件人: zhangsan@company.com
時間: 2024-01-15 10:30:00
狀態: 已讀
[2] 主題: 發票報銷
發件人: finance@company.com
時間: 2024-01-15 09:15:00
狀態: 未讀 ⭐
========================================
共 2 封郵件
========================================
這個郵件技能做得相當不錯,功能覆蓋全面,從查郵件到發郵件都能搞定。文件寫得很詳細,配置說明清晰,還附帶多種常用郵箱的設定示例,新手也能快速上手。使用Python標準庫開發,不需要額外安裝依賴,穩定可靠。美中不足的是缺少自動化測試,對於重要功能來說不夠穩妥,另外錯誤提示資訊也可以更友好一些。總體而言,這是一個成熟度高、實用性強的郵件處理技能。