#!/usr/bin/env python3
"""
G5 周报发送脚本
用法：构造多段邮件（纯文本 + HTML），存入钉邮草稿箱
"""
import email
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
import imaplib
import time
import sys

def send_to_drafts(plain_body: str, html_body: str, date_range: str):
    """构造多段邮件并存入钉邮草稿箱"""
    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() + ' <wurd@witsoft.cn>, '
        + Header('陈明', 'utf-8').encode() + ' <chenm@witsoft.cn>, '
        + Header('曾莉', 'utf-8').encode() + ' <zengli@witsoft.cn>'
    )
    msg['Subject'] = Header(f'G5开发周报 ({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.mxhichina.com', 993)
    conn.login('yangxuan@witsoft.cn', '5gynj20J')

    drafts = '"&g0l6Pw-"'
    result = conn.append(drafts, None, imaplib.Time2Internaldate(time.time()), raw_bytes)
    conn.logout()
    return result

if __name__ == '__main__':
    # 测试
    plain = """项目名称： 维云智造G5
主要任务： 测试

本周工作内容
周一（2026-07-13）
 - 测试

存在问题
无"""
    html = "<p>test</p>"
    result = send_to_drafts(plain, html, "2026-07-13 ~ 2026-07-17")
    print(result)
