Skip to main content

A simple and easy-to-use email sending library with template support

Project description

MailLite

一款简单易用的Python邮件发送库,支持HTML模板渲染、附件发送、SSL/TLS加密连接,内置精美的邮件模板。

PyPI version Python License


特性

  • 简单集成 — 几行代码即可发送邮件
  • 模板引擎 — 基于Jinja2HTML / 纯文本模板渲染,主题(subject)也支持变量
  • 内置模板 — 开箱即用的 welcome(欢迎)和 admonition(系统通知/告警)模板
  • 自定义模板 — 支持自定义模板目录,优先级高于内置模板
  • 附件支持 — 可附加任意本地文件
  • 多种加密 — 支持 SSL(端口 465)和 STARTTLS(端口 587
  • 完整 MIME — 纯文本、HTML、多收件人(To/CC/BCC)、Reply-To

安装

pip install maillite

快速开始

发送纯文本邮件

from maillite import EasyMailer

mailer = EasyMailer(
    host="smtp.qq.com",
    port=465,
    username="your@qq.com",
    password="your_auth_code",   # QQ 邮箱请使用授权码
    use_ssl=True,
)

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

使用模板发送邮件

mailer.send_template(
    to="test@qq.com",
    subject="欢迎 {{ name }}",
    template_name="welcome",
    context={
        "name": "张三",
        "title": "欢迎加入",
        "message": "感谢您的注册!",
        "action_url": "https://example.com/dashboard",
        "action_text": "进入控制台",
    },
)

发送服务异常告警

import traceback

mailer.send_template(
    to="test@qq.com",
    subject="【{{ severity }}】{{ service_name }} 服务异常",
    template_name="admonition",
    context={
        "name": "运维同学",
        "title": "服务异常通知",
        "subtitle": "系统检测到异常,请及时关注",
        "severity": "严重",
        "service_name": "API Gateway",
        "code": "ERR_502",
        "message": "Connection refused: upstream server 10.0.0.5:8080",
        "occurred_at": "2026-07-08 20:30:00",
        "action_url": "https://monitor.example.com/incidents/123",
    },
)


try:
    a
except Exception as err:
    mailer.send_template(
    to="test@qq.com",
    subject="【{{ severity }}】{{ service_name }} 服务异常",
    template_name="admonition",
    context={
        "name": "运维同学",
        "severity": "严重",
        "service_name": "API Gateway",
        "code": "ERR_502",
        "message": traceback.format_exc(),
    },
)

API 参考

EasyMailer(...)

初始化邮件发送器。

参数 类型 默认值 说明
host str SMTP 服务器地址
port int SMTP 端口(SSL: 465, TLS: 587, 明文: 25)
username str SMTP 登录用户名
password str SMTP 登录密码 / 授权码
use_ssl bool True 使用 SSL 连接
use_tls bool False 使用 STARTTLS(不可与 use_ssl 同时开启)
default_sender str None 默认发件人地址,不填则使用 username
template_dir str None 自定义模板目录路径

mailer.send(...)

发送邮件。

参数 类型 说明
to str / list[str] 收件人
subject str 邮件主题
body str 纯文本正文
html str HTML 正文(优先于 body)
cc str / list[str] 抄送
bcc str / list[str] 密送
sender str 发件人(覆盖 default_sender)
sender_name str 发件人显示名
attachments list[str / dict] 附件列表(见下方说明)
reply_to str 回复地址

mailer.send_template(...)

使用模板发送邮件。模板通过 Jinja2 渲染,subject 同样支持模板变量。

参数 类型 说明
to str / list[str] 收件人
subject str 邮件主题(支持 Jinja2 变量)
template_name str 模板名称(不含扩展名)
context dict 模板变量字典
cc str / list[str] 抄送
bcc str / list[str] 密送
sender str 发件人
sender_name str 发件人显示名
attachments list[str / dict] 附件列表(见下方说明)
reply_to str 回复地址

attachments 附件说明

attachments 列表中的每个元素支持两种格式:

1. 文件路径(str)

attachments=["d:/report.pdf", "d:/data.xlsx"]

2. 二进制数据(dict) — 适用于动态生成的图片、文件等

attachments=[
    {
        "filename": "chart.png",
        "data": image_bytes,          # bytes 类型
        "content_type": "image/png",  # 可选,自动根据 filename 推断
    }
]

两种格式可以混合使用:attachments=["d:/report.pdf", {"filename": "chart.png", "data": image_bytes}]

如果同时存在 template_name.htmltemplate_name.txt,HTML 版本作为富文本正文,TXT 版本作为纯文本备用正文。

内置模板

welcome — 欢迎模板

紫蓝渐变主题,适用于用户注册、邀请等场景。

变量 说明 默认值
title 头部标题 "欢迎使用 MailLite"
name 收件人称呼
message 正文内容 内置默认文案
action_url CTA 按钮链接
action_text CTA 按钮文案 "立即查看"
team_name 团队署名 "MailLite 团队"
help_url 底部帮助链接

admonition — 系统通知/告警模板

多主题告警模板,根据 severity 自动切换颜色(红/橙/绿/蓝),适用于系统监控、故障通知、服务恢复、日常通知等场景。

变量 说明 默认值
title 头部标题 "系统通知"
subtitle 头部副标题(小标题)
name 收件人称呼
body 正文描述段落
severity 严重程度
service_name 服务标识
code 事件编号
message 信息内容
occurred_at 发生时间 自动填充当前时间(YYYY-MM-DD HH:MM:SS
action_url 查看详情链接
action_text 按钮文案 "查看详情"
team_name 团队署名 "MailLite 监控团队"
help_url 底部帮助链接

severity 支持以下值:"严重" / "critical" (红色)、"警告" / "warning" (橙色)、"成功" / "success" (绿色)、"信息" / "info" (蓝色),其他值默认蓝色。

自定义模板

创建自己的模板目录并放置 .html / .txt 文件:

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

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

自定义目录优先级高于内置模板。如果同名模板存在于两个目录,自定义版本会被使用。也可以在运行时通过 mailer._template_engine.add_template_dir(path) 动态添加模板目录。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

maillite-0.1.6-cp314-cp314-win_amd64.whl (78.4 kB view details)

Uploaded CPython 3.14Windows x86-64

maillite-0.1.6-cp314-cp314-manylinux_2_17_x86_64.whl (473.3 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

maillite-0.1.6-cp313-cp313-win_amd64.whl (76.6 kB view details)

Uploaded CPython 3.13Windows x86-64

maillite-0.1.6-cp313-cp313-manylinux_2_17_x86_64.whl (479.1 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

maillite-0.1.6-cp312-cp312-win_amd64.whl (77.7 kB view details)

Uploaded CPython 3.12Windows x86-64

maillite-0.1.6-cp312-cp312-manylinux_2_17_x86_64.whl (491.1 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

maillite-0.1.6-cp311-cp311-win_amd64.whl (78.2 kB view details)

Uploaded CPython 3.11Windows x86-64

maillite-0.1.6-cp311-cp311-manylinux_2_17_x86_64.whl (463.5 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

maillite-0.1.6-cp310-cp310-win_amd64.whl (78.0 kB view details)

Uploaded CPython 3.10Windows x86-64

maillite-0.1.6-cp310-cp310-manylinux_2_17_x86_64.whl (444.9 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

maillite-0.1.6-cp39-cp39-win_amd64.whl (100.9 kB view details)

Uploaded CPython 3.9Windows x86-64

maillite-0.1.6-cp39-cp39-manylinux_2_17_x86_64.whl (442.3 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

maillite-0.1.6-cp38-cp38-win_amd64.whl (103.2 kB view details)

Uploaded CPython 3.8Windows x86-64

maillite-0.1.6-cp38-cp38-manylinux_2_17_x86_64.whl (473.6 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

Details for the file maillite-0.1.6-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: maillite-0.1.6-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 78.4 kB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for maillite-0.1.6-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 664beef7feaa46d5c05acb25cd6e8203a650aa4dfeca987bad54bba4d562a4ca
MD5 88c42d3977f2320494a766d72bf2074a
BLAKE2b-256 32a5b9e575d24fdbe852d9bde339c223d97480fb58a9caabba71a4250b244789

See more details on using hashes here.

Provenance

The following attestation bundles were made for maillite-0.1.6-cp314-cp314-win_amd64.whl:

Publisher: build.yml on zhenzi0322/maillite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file maillite-0.1.6-cp314-cp314-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for maillite-0.1.6-cp314-cp314-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 1dfb57b1e9bce5e2fcab22ab1ff15643259203142a48fcb146bbef7006126738
MD5 2519caf649cdf9717932aa62e92957b1
BLAKE2b-256 a1caee01f6e869397d14391c4deca4d1aab5ee3e41eafd7979e515bf80f7da79

See more details on using hashes here.

Provenance

The following attestation bundles were made for maillite-0.1.6-cp314-cp314-manylinux_2_17_x86_64.whl:

Publisher: build.yml on zhenzi0322/maillite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file maillite-0.1.6-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: maillite-0.1.6-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 76.6 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for maillite-0.1.6-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 75e4da9bffffcd011f8e0d21ec20cbc3665a5f6d9e2509b5637400bd290d2a89
MD5 1955bd07c603db7bc60c113d1f499d00
BLAKE2b-256 0e4664c4bc454f7a5b424a54b07201068d29d0815bd23b95c6d4234cc2b4dd87

See more details on using hashes here.

Provenance

The following attestation bundles were made for maillite-0.1.6-cp313-cp313-win_amd64.whl:

Publisher: build.yml on zhenzi0322/maillite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file maillite-0.1.6-cp313-cp313-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for maillite-0.1.6-cp313-cp313-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 6a9eaf0209cbf111fde47ad095bebb3df054c0bc016de8849df8aba91d509968
MD5 d893bb891833d385b03af57f6ba9bde2
BLAKE2b-256 b43d978f74e409a735371b959b72d8e197fc45845ed319cef2ea37cff632d78b

See more details on using hashes here.

Provenance

The following attestation bundles were made for maillite-0.1.6-cp313-cp313-manylinux_2_17_x86_64.whl:

Publisher: build.yml on zhenzi0322/maillite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file maillite-0.1.6-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: maillite-0.1.6-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 77.7 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for maillite-0.1.6-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 9f159ed429ce105431cdb129a3d8dda0b20327a271e3ce8c6f4305e86ec927a5
MD5 e990f2ff812e92347c30b74fb63ef8b0
BLAKE2b-256 9e7ec4a36adf690bd48dc0704e6b5b05555959e527fe8ad4541a33b6545e7e32

See more details on using hashes here.

Provenance

The following attestation bundles were made for maillite-0.1.6-cp312-cp312-win_amd64.whl:

Publisher: build.yml on zhenzi0322/maillite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file maillite-0.1.6-cp312-cp312-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for maillite-0.1.6-cp312-cp312-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 656ed736b75cd216e1485cda638503036879582c9371061e7cdc98c86531b82d
MD5 42dfd534d1bc948f4b2d6fa081f1519c
BLAKE2b-256 2f777171567a04ab6abfafb16e5775745bd1b5bb226defcc82423af0427e6c1f

See more details on using hashes here.

Provenance

The following attestation bundles were made for maillite-0.1.6-cp312-cp312-manylinux_2_17_x86_64.whl:

Publisher: build.yml on zhenzi0322/maillite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file maillite-0.1.6-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: maillite-0.1.6-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 78.2 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for maillite-0.1.6-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 2e3da73ae864157f8b6df5b02914866e06f59b34bc1947857d80559ccf9b02f4
MD5 ef98c79e3a8faf5683305948ca427412
BLAKE2b-256 d835406826c13f7ebcc2cea32064b8f1ea22e5f6b9b7811cd7a81c9f2998150c

See more details on using hashes here.

Provenance

The following attestation bundles were made for maillite-0.1.6-cp311-cp311-win_amd64.whl:

Publisher: build.yml on zhenzi0322/maillite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file maillite-0.1.6-cp311-cp311-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for maillite-0.1.6-cp311-cp311-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 372032538d7a64dd456293a2a61e292f6792591ea0c782c764d1d0074ea78833
MD5 6d14a29e00276553df5c9b25defd8518
BLAKE2b-256 98be06dae0f2c20ed30b57f31859f7f926e177d52c9784a7f66fcfdf6a78c450

See more details on using hashes here.

Provenance

The following attestation bundles were made for maillite-0.1.6-cp311-cp311-manylinux_2_17_x86_64.whl:

Publisher: build.yml on zhenzi0322/maillite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file maillite-0.1.6-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: maillite-0.1.6-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 78.0 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for maillite-0.1.6-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 03525f65e0551dad9c935e3497ef7a96147fea6c228c6155f7747c70e3fd3d88
MD5 b2c88460b6dc32f506a8ba94af045033
BLAKE2b-256 a07ca06bc617aa4b34876bd28526a08be6a753a6a351c99fe3beb5ffb897c58e

See more details on using hashes here.

Provenance

The following attestation bundles were made for maillite-0.1.6-cp310-cp310-win_amd64.whl:

Publisher: build.yml on zhenzi0322/maillite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file maillite-0.1.6-cp310-cp310-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for maillite-0.1.6-cp310-cp310-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 0e1c020c9b005506802e41a02b9454f8ac46214e8f80da21350eb9f4c7d539e2
MD5 2b53b0b9d041ed634468c76c817461ff
BLAKE2b-256 a5da96238e9f44962d8e473b89dde1804d219c0347826c875ba868d38dc9a968

See more details on using hashes here.

Provenance

The following attestation bundles were made for maillite-0.1.6-cp310-cp310-manylinux_2_17_x86_64.whl:

Publisher: build.yml on zhenzi0322/maillite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file maillite-0.1.6-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: maillite-0.1.6-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 100.9 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for maillite-0.1.6-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 e6439d4eab7cafdb2358d12f6b5eacad7b0783533dea7861b864e3134afb2cb1
MD5 8434c3d87eed782aa79c448b32653119
BLAKE2b-256 1d63cec8aaecd638deb36fc084e7a40d639183e46b8ed83826c1f164462518b3

See more details on using hashes here.

Provenance

The following attestation bundles were made for maillite-0.1.6-cp39-cp39-win_amd64.whl:

Publisher: build.yml on zhenzi0322/maillite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file maillite-0.1.6-cp39-cp39-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for maillite-0.1.6-cp39-cp39-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 a335ce70c835319bce8407a03014bdbb7f922517e3e19c4d3b15846a85e04563
MD5 5a727677ed4aeaa9c6fd3d4ef6859a55
BLAKE2b-256 a2c3bc5167ded902e9fc940f1717f015d88e87a32d51173c46ab87df1af31462

See more details on using hashes here.

Provenance

The following attestation bundles were made for maillite-0.1.6-cp39-cp39-manylinux_2_17_x86_64.whl:

Publisher: build.yml on zhenzi0322/maillite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file maillite-0.1.6-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: maillite-0.1.6-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 103.2 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for maillite-0.1.6-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 9d7500a54163613df03843708a47c43cd6144015a9e67ff62fc68a70870e67b6
MD5 9abd9d14a002f79359214aec24dddee8
BLAKE2b-256 b4b2882ff91da519da267ef0ad5542fdffde9cbe681d3d3e73a5684850ca9456

See more details on using hashes here.

Provenance

The following attestation bundles were made for maillite-0.1.6-cp38-cp38-win_amd64.whl:

Publisher: build.yml on zhenzi0322/maillite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file maillite-0.1.6-cp38-cp38-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for maillite-0.1.6-cp38-cp38-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 0085b21c5f86ab1caff912b48791a1be28a45cf8999015fc32326b4a164c4767
MD5 4ee527d46de9e4e88290cb468e57d613
BLAKE2b-256 b87f850e6d3da4fcc1201e68e714617479309bea52a5f65263c084611433f2a9

See more details on using hashes here.

Provenance

The following attestation bundles were made for maillite-0.1.6-cp38-cp38-manylinux_2_17_x86_64.whl:

Publisher: build.yml on zhenzi0322/maillite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page