This skill provides everything needed to integrate with Aliyun Enterprise Email (阿里企业邮箱) using Python's standard smtplib and imaplib — no third-party libraries required. It covers sending, reading, bounce detection, and invoice attachment validation.
Connection Parameters:
smtp.qiye.aliyun.com port 465 (SSL) or 25 (plain)
imap.qiye.aliyun.com port 993 (SSL)
&XfJT0ZAB- (UTF-7 encoded)
See references/aliyun_email_config.md for full config reference including folder names, SSL troubleshooting, SMTP error codes, and IMAP search syntax.
Use scripts/email_client.py → AliyunEmailClient.send_email():
from scripts.email_client import AliyunEmailClient
client = AliyunEmailClient(
username='your@example.com',
password='your_auth_code',
display_name='财务部'
)
# HTML email with attachment
result = client.send_email(
subject='月度报表',
to=['manager@example.com', 'boss@example.com'],
cc=['assistant@example.com'],
html_content='<h1>月度财务报表</h1><p>请查收附件</p>',
text_content='月度财务报表,请查收附件',
attachments=['/path/to/report.xlsx']
)
print(result) # {'success': True, 'message': '邮件发送成功', 'recipients': [...]}
Parameters:
to / cc / bcc: str or list of str
html_content / text_content: body (use at least one)
attachments: list of local file paths
url_attachments: list of URLs to download and attach
use_ssl=True for port 465; use_ssl=False for port 25
client = AliyunEmailClient(username='...', password='...')
try:
# List inbox emails
emails = client.list_emails(folder='INBOX', limit=20)
for m in emails:
print(f"[{m['uid']}] {m['date']} | {m['subject']}")
# Read sent folder
sent = client.list_emails(folder='&XfJT0ZAB-', since_days=1)
# Get full content
content = client.get_email_content(uid='123', folder='INBOX')
print(content['body_text'])
for att in content['attachments']:
print(att['filename'], att['size'])
# Download attachment
client.download_attachment(uid='123', filename='report.pdf', save_path='/tmp/report.pdf')
finally:
client.close()
list_emails() filter options:
unseen_only=True — unread only
since_days=N — last N days
since_date='25-Mar-2026' / before_date='26-Mar-2026' — date range (DD-Mon-YYYY)
subject_keyword='发票' — filter by subject
Run scripts/check_bounced.py or import as a module:
from scripts.check_bounced import check_bounced_emails
from datetime import date
bounces = check_bounced_emails(
username='your@example.com',
password='auth_code',
check_date=date(2026, 3, 25), # defaults to yesterday if omitted
output_dir='./check_result', # CSV saved here
own_domain='yourdomain.com' # exclude own addresses from recipient extraction
)
# Returns list of bounce dicts, also saves bounce_report_YYYYMMDD.csv
Command line:
python scripts/check_bounced.py your@example.com auth_code 2026-03-25 ./check_result yourdomain.com
CSV output fields: 邮件主题, 发送时间, 收信地址, 退信原因, 解决方案, 退信正文预览
Bounce detection: The script identifies bounces by checking From field (mailer-daemon, postmaster) and Subject keywords (undeliverable, delivery failed, 退信, etc.). It then extracts the original recipient from the body — prioritizing the Aliyun-specific pattern 无法发送到 xxx@xxx.com.
Run scripts/check_invoices.py or import as a module:
from scripts.check_invoices import check_invoice_emails
result = check_invoice_emails(
username='your@example.com',
password='auth_code',
date_str='2026-03-25',
output_dir='./check_result',
alert_recipients=['admin@example.com'], # notify on mismatch
sent_folder='&XfJT0ZAB-' # Aliyun sent folder
)
# Returns {'success': True, 'total_emails': N, 'checked_emails': N,
# 'matched': N, 'mismatched': N, 'report_file': '...'}
Rules:
X张 (e.g., "发票3张")
Command line:
python scripts/check_invoices.py your@example.com auth_code 2026-03-25 ./result admin@example.com
client = AliyunEmailClient(username='...', password='...')
for f in client.list_folders():
print(f['name'])
client.close()
When the user wants a recurring automation (e.g., "每天检查退信"), use WorkBuddy's automation feature to schedule check_bounced.py or check_invoices.py to run daily.
scripts/email_client.py — Full SMTP + IMAP client (AliyunEmailClient class)
scripts/check_bounced.py — Bounce email detection and CSV report
scripts/check_invoices.py — Invoice attachment count validation
references/aliyun_email_config.md — Server config, folder names, SSL notes, SMTP error codes
共 1 个版本