""" 邮件发送服务 支持SMTP邮件发送、附件发送、模板渲染 """ import os import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase from email import encoders from datetime import datetime from flask import current_app class EmailService: """邮件发送服务""" def __init__(self, smtp_host=None, smtp_port=None, smtp_user=None, smtp_pass=None): self.smtp_host = smtp_host or os.environ.get('SMTP_HOST', 'mail.tphai.com') self.smtp_port = int(smtp_port or os.environ.get('SMTP_PORT', '587')) self.smtp_user = smtp_user or os.environ.get('SMTP_USER', 'favor@tphai.com') self.smtp_pass = smtp_pass or os.environ.get('SMTP_PASS', 'favor@!') def send_email(self, to_email, subject, body, attachment_path=None, attachment_name=None): """发送邮件(支持附件)""" try: # 创建邮件对象 msg = MIMEMultipart() msg['From'] = self.smtp_user msg['To'] = to_email msg['Subject'] = subject msg['Date'] = datetime.now().strftime('%a, %d %b %Y %H:%M:%S +0800') msg['Reply-To'] = to_email # 正文 msg.attach(MIMEText(body, 'html', 'utf-8')) # 附件 if attachment_path and os.path.exists(attachment_path): with open(attachment_path, 'rb') as f: part = MIMEBase('application', 'octet-stream') part.set_payload(f.read()) encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment', filename=attachment_name or os.path.basename(attachment_path)) msg.attach(part) # 发送 server = smtplib.SMTP(self.smtp_host, self.smtp_port) server.ehlo() server.login(self.smtp_user, self.smtp_pass) server.sendmail(self.smtp_user, to_email, msg.as_string()) server.quit() return True, "发送成功" except Exception as e: return False, str(e) def send_translation_complete(self, user_email, username, filename, output_path, translation_id): """翻译完成通知""" subject = f"【PDF翻译助手】翻译完成 - {filename}" body = f"""

📄 PDF翻译助手

您好,{username}!

您的翻译任务已完成:

文件:{filename}

状态:✅ 完成

时间:{datetime.now().strftime('%Y-%m-%d %H:%M')}

翻译结果已作为附件发送,您也可以登录网站查看详情。

查看翻译结果

此邮件由系统自动发送,请勿回复。
PDF翻译助手 - 让翻译更简单

""" # 附件名称 attachment_name = f"{filename}_translated.md" return self.send_email(user_email, subject, body, output_path, attachment_name) def send_welcome_email(self, user_email, username, invite_code=None): """欢迎邮件""" subject = "欢迎加入PDF翻译助手" body = f"""

📄 PDF翻译助手

您好,{username}!

欢迎加入PDF翻译助手!您已获得:

✅ 每日免费翻译 10 次

✅ 单文件最大 50 页

✅ 翻译历史记录

✅ 不满意重新翻译

{"

您的专属邀请码:" + invite_code + ",分享给好友可获得奖励!

" if invite_code else ""}
开始使用
""" return self.send_email(user_email, subject, body) def send_expire_reminder(self, user_email, username, expire_date, user_type): """会员到期提醒""" subject = "【PDF翻译助手】会员即将到期提醒" body = f"""

📄 PDF翻译助手

您好,{username}!

⚠️ 您的会员即将到期

会员类型:{user_type}

到期时间:{expire_date}

到期后将降级为免费用户,每日翻译次数限制为10次。续费可继续享受会员权益。

续费会员
""" return self.send_email(user_email, subject, body) def send_invite_reward(self, user_email, username, reward_amount, invitee_count): """邀请奖励通知""" subject = f"【PDF翻译助手】邀请奖励 - ¥{reward_amount}" body = f"""

🎉 邀请奖励已发放

您好,{username}!

奖励金额:¥{reward_amount}

累计邀请:{invitee_count}人

奖励已发放到您的账户余额,可用于购买会员或翻译服务。

查看余额
""" return self.send_email(user_email, subject, body) # 全局邮件服务实例 email_service = EmailService()