跳转至

自定义模板用法

模板相关

my_templates/invoice.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h2>{{ invoice_no }}</h2>
    <p>{{ amount }}</p>
</body>
</html>

my_templates/invoice.txt:

{{ invoice_no }} -- {{ amount }}

模板用法

发送纯文本邮件

from maillite import EasyMailer

mailer = EasyMailer(
    host="smtp.126.com",
    port=465,
    username="your@qq.com",
    password="your_auth_code",
    use_ssl=True,
    template_dir="./my_templates",   # 自定义模板目录
)

mailer.send(
    to="xxxx@qq.com",
    subject="测试邮件",
    body="这是一封来自 MailLite 的测试邮件。",
)

使用模板发送邮件

from maillite import EasyMailer

mailer = EasyMailer(
    host="smtp.126.com",
    port=465,
    username="your@qq.com",
    password="your_auth_code",
    use_ssl=True,
    template_dir="./my_templates",   # 自定义模板目录
)

mailer.send_template(
    to="xxxxx@qq.com",
    subject="发票 #{{ invoice_no }}",
    template_name="invoice",
    context={"invoice_no": "20260708-001", "amount": "¥1,280.00"},
)