1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| import json
import yagmail from jinja2 import Template
from common.loguru_conf import Logger from common.get_config import get_config from tools.get_root_path import get_root_path
logger = Logger().logger config = get_config("email")
class SendEmail(object): def __init__(self, email_subject, html_data): self._user_email = config.get("user_email") self._password = config.get("password") self._smtp_server = config.get("smtp_server") self._to_email = config.get("to_email") self.email_subject = email_subject self.html_data = html_data self._send_email()
def _send_email(self): """ 发送邮件 """ to_email_list = self._to_email.split(",") yag = yagmail.SMTP( user=self._user_email, password=self._password, host=self._smtp_server, encoding="GBK", ) yag.send( to=to_email_list, subject=self.email_subject, contents=self._html_message() ) logger.info(f"邮件发送成功: {to_email_list}")
def _html_message(self): """ 使用数据渲染 html 模板 :return: 返回渲染后的 html 数据 """ email_template = get_root_path() + "/configs/email_template.html" html_str = "" with open(email_template, "r") as file: html_list = file.readlines() for html in html_list: html_str += html.strip() template = Template(html_str) rendered_template = template.render( alert_name=self.email_subject, data=self.html_data ) return rendered_template
if __name__ == "__main__": datas = { "versionNumber": "4.2.10", "updateTime": "2024-02-18 09:57:18", "privacyPolicy": "https://app.2uchat.cn/secret/zh3.html", "releaseState": "已上架", "auditOpinion": "\n应用审核意见:\n 通过\n 测试环境:Wi-Fi联网、HarmonyOS 3.0.0(Mate40Pro)、中文环境。", "language": "zh-CN", "appName": "通友", } SendEmail("测试邮件", datas)
|