Skip to content

[建议] 是否可以增加对resend邮件的支持 #542

@Jeremy-ZJL

Description

@Jeremy-ZJL

🏷️ 建议类别

数据源相关(新增平台、改进抓取等)

💭 详细描述你的想法

新增resend邮件的支持

🎯 什么时候会用到这个功能

新增resend邮件的支持

🛠️ 实现想法(可选)

No response

📷 功能示意图(推荐)

`def send_to_resend(
api_key: str,
from_email: str,
to_email: str,
report_type: str,
html_file_path: str,
) -> bool:
"""使用 Resend API 发送邮件通知"""
try:
if not html_file_path or not Path(html_file_path).exists():
print(f"错误:HTML文件不存在或未提供: {html_file_path}")
return False

    print(f"使用HTML文件: {html_file_path}")
    with open(html_file_path, "r", encoding="utf-8") as f:
        html_content = f.read()

    # 设置收件人列表
    recipients = [addr.strip() for addr in to_email.split(",")]

    # 设置邮件主题
    now = get_beijing_time()
    subject = f"TrendRadar 热点分析报告 - {report_type} - {now.strftime('%m月%d日 %H:%M')}"

    # 纯文本内容(作为备选)
    text_content = f"""

TrendRadar 热点分析报告

报告类型:{report_type}
生成时间:{now.strftime('%Y-%m-%d %H:%M:%S')}

请使用支持HTML的邮件客户端查看完整报告内容。
"""

    print(f"正在通过 Resend API 发送邮件到 {to_email}...")
    print(f"发件人: {from_email}")

    # 调用 Resend API
    url = "https://api.resend.com/emails"
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json",
    }
    payload = {
        "from": f"TrendRadar <{from_email}>",
        "to": recipients,
        "subject": subject,
        "html": html_content,
        "text": text_content.strip(),
    }

    response = requests.post(url, headers=headers, json=payload, timeout=30)

    if response.status_code == 200:
        result = response.json()
        print(f"Resend 邮件发送成功 [{report_type}] -> {to_email}, ID: {result.get('id', 'N/A')}")
        return True
    else:
        print(f"Resend 邮件发送失败:HTTP {response.status_code}")
        print(f"响应内容: {response.text}")
        return False

except requests.exceptions.Timeout:
    print("Resend 邮件发送失败:请求超时")
    return False
except requests.exceptions.RequestException as e:
    print(f"Resend 邮件发送失败:网络错误 {e}")
    return False
except Exception as e:
    print(f"Resend 邮件发送失败 [{report_type}]:{e}")
    import traceback
    traceback.print_exc()
    return False

`

📎 其他补充说明

No response

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions