Read QQ Mail emails via the IMAP protocol.
Trigger Conditions
Use when the user mentions the following scenarios:
- "Check the mailbox"
- "Read emails"
- "View recent emails"
- "View today's emails"
- "Search emails"
- "Are there any new emails"
Configuration
QQ Mail IMAP needs to be configured in advance:
- Enable the IMAP service in QQ Mail settings
- Create an authorization code (not the login password)
- Configure the secrets file:
~/.openclaw/secrets/mail_qq.envMAIL_USER=your_email@qq.com MAIL_PASS=your_auth_code
Security Notes
- The credentials file must have permissions set to
600(readable only by the owner) to prevent other users from reading sensitive information ~/.openclaw/secrets/The directory is gitignored by default and will not be committed to version control- Do not commit the authorization code to a code repository or share it with others
Usage
1. Connect to the mailbox
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
2. Search emails
# 搜索最近7天的邮件
week_ago = (datetime.now() - timedelta(days=7)).strftime('%d-%b-%Y')
typ, msg_ids = mail.search(None, f'SINCE {week_ago}')
3. Parse emails
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)
4. Extract the body
The email body may contain HTML and needs to be cleaned:
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
Email Filtering
Filter emails according to user needs; common keyword types:
- Notification type: notification, reminder, announcement
- Business type: order, shipment, payment
- Job type: interview, invitation, invitation (only when the user explicitly needs it)
Notes
- QQ Mail must be logged in with an authorization code, not the login password
- Email encodings may be GBK/GB2312/UTF-8, etc., so multiple encodings need to be tried
- IMAP search does not support Chinese; use SINCE to retrieve all emails and then filter locally