87 lines
2.6 KiB
Python
Executable File
87 lines
2.6 KiB
Python
Executable File
#!/usr/bin/env python3
|
||
"""
|
||
G6 周报发送脚本
|
||
用法:构造多段邮件(纯文本 + HTML),存入钉邮草稿箱
|
||
"""
|
||
import email
|
||
from email.mime.multipart import MIMEMultipart
|
||
from email.mime.text import MIMEText
|
||
from email.header import Header
|
||
import imaplib
|
||
import os
|
||
import time
|
||
import sys
|
||
|
||
|
||
def load_env(path):
|
||
"""读取 .env 文件,返回键值字典(标准库实现,无额外依赖)。"""
|
||
env = {}
|
||
try:
|
||
with open(path, 'r', encoding='utf-8') as f:
|
||
for line in f:
|
||
line = line.strip()
|
||
if not line or line.startswith('#') or '=' not in line:
|
||
continue
|
||
key, _, value = line.partition('=')
|
||
env[key.strip()] = value.strip().strip('"').strip("'")
|
||
except FileNotFoundError:
|
||
pass
|
||
return env
|
||
|
||
|
||
_ENV = load_env(os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), '.env'))
|
||
|
||
IMAP_HOST = 'imap.mxhichina.com'
|
||
IMAP_PORT = 993
|
||
IMAP_USER = _ENV.get('WITSOFT_IMAP_USER', 'yangxuan@witsoft.cn')
|
||
IMAP_PASSWORD = _ENV.get('WITSOFT_IMAP_PASSWORD', '')
|
||
|
||
def send_to_drafts(plain_body: str, html_body: str, date_range: str, project: str = "G6"):
|
||
"""构造多段邮件并存入钉邮草稿箱
|
||
|
||
Args:
|
||
plain_body: 纯文本正文
|
||
html_body: HTML 正文
|
||
date_range: 日期范围字符串
|
||
project: 项目名称,默认"G6",用于邮件主题
|
||
"""
|
||
msg = MIMEMultipart('related')
|
||
msg['From'] = Header('杨轩', 'utf-8').encode() + ' <yangxuan@witsoft.cn>'
|
||
msg['To'] = Header('刘强', 'utf-8').encode() + ' <liuqiang@witsoft.cn>'
|
||
msg['Cc'] = (
|
||
Header('陈明', 'utf-8').encode() + ' <chenm@witsoft.cn>, '
|
||
+ Header('曾莉', 'utf-8').encode() + ' <zengli@witsoft.cn>, '
|
||
+ Header('袁晴', 'utf-8').encode() + ' <yuanq@witsoft.cn>'
|
||
)
|
||
msg['Subject'] = Header(f'{project}开发周报 ({date_range})', 'utf-8').encode()
|
||
|
||
alt = MIMEMultipart('alternative')
|
||
alt.attach(MIMEText(plain_body, 'plain', 'utf-8'))
|
||
alt.attach(MIMEText(html_body, 'html', 'utf-8'))
|
||
msg.attach(alt)
|
||
|
||
raw_bytes = msg.as_bytes()
|
||
|
||
conn = imaplib.IMAP4_SSL(IMAP_HOST, IMAP_PORT)
|
||
conn.login(IMAP_USER, IMAP_PASSWORD)
|
||
|
||
drafts = '"&g0l6Pw-"'
|
||
result = conn.append(drafts, None, imaplib.Time2Internaldate(time.time()), raw_bytes)
|
||
conn.logout()
|
||
return result
|
||
|
||
if __name__ == '__main__':
|
||
# 测试
|
||
plain = """项目名称: 维云智造 G6
|
||
主要任务: 测试
|
||
|
||
本周工作内容
|
||
周一(2026-07-13)
|
||
- 测试
|
||
|
||
存在问题
|
||
无"""
|
||
html = "<p>test</p>"
|
||
result = send_to_drafts(plain, html, "2026-07-13 ~ 2026-07-17", "G6")
|
||
print(result)
|