Install
openclaw skills install qq-mail-reader读取QQ邮箱邮件。使用IMAP协议连接QQ邮箱,支持多种编码解析。触发场景:用户要求查看邮箱、读取邮件、查看最近邮件、筛选特定邮件等。
openclaw skills install qq-mail-reader通过 IMAP 协议读取 QQ 邮箱邮件。
用户提到以下场景时使用:
需要提前配置 QQ 邮箱 IMAP:
~/.openclaw/secrets/mail_qq.env
MAIL_USER=your_email@qq.com
MAIL_PASS=your_auth_code
600(仅所有者可读),防止其他用户读取敏感信息~/.openclaw/secrets/ 目录默认已经被 gitignore,不会被提交到版本控制import imaplib
import email
from email.header import decode_header
IMAP_HOST = 'imap.qq.com'
IMAP_PORT = 993
def connect_mail(user, password):
mail = imaplib.IMAP4_SSL(IMAP_HOST, IMAP_PORT)
mail.login(user, password)
mail.select('INBOX')
return mail
# 搜索最近7天的邮件
week_ago = (datetime.now() - timedelta(days=7)).strftime('%d-%b-%Y')
typ, msg_ids = mail.search(None, f'SINCE {week_ago}')
def decode_header_value(header_value):
"""兼容多种编码的解码"""
if not header_value:
return ""
decoded_parts = []
for content, encoding in decode_header(header_value):
if isinstance(content, bytes):
try:
decoded = content.decode(encoding if encoding else 'utf-8')
except:
# 尝试其他常见编码
for enc in ['gbk', 'gb2312', 'utf-8', 'big5', 'latin1']:
try:
decoded = content.decode(enc)
break
except:
decoded = content.decode('utf-8', errors='ignore')
else:
decoded = str(content)
decoded_parts.append(decoded)
return ''.join(decoded_parts)
邮件正文可能包含 HTML,需要清理:
import re
def get_text_body(msg):
body = ''
if msg.is_multipart():
for part in msg.walk():
if part.get_content_type() == 'text/html':
payload = part.get_payload(decode=True)
encoding = part.get_content_charset() or 'utf-8'
body = payload.decode(encoding, errors='ignore')
break
else:
payload = msg.get_payload(decode=True)
encoding = msg.get_content_charset() or 'utf-8'
body = payload.decode(encoding, errors='ignore')
# 清理HTML标签
text = re.sub(r'<[^>]+>', ' ', body)
text = text.replace(' ', ' ')
text = text.replace('&', '&')
text = re.sub(r'\s+', ' ', text)
return text
根据用户需求过滤邮件,常见关键词类型: