Skip to main content

MailLite

PyPI version Python License Documentation Status

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


特性

  • 简单集成 — 几行代码即可发送邮件
  • 模板引擎 — 基于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) 动态添加模板目录。

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.9-cp314-cp314-win_amd64.whl (81.0 kB view details)

Uploaded CPython 3.14Windows x86-64

maillite-0.1.9-cp314-cp314-manylinux_2_17_x86_64.whl (475.6 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

maillite-0.1.9-cp313-cp313-win_amd64.whl (79.2 kB view details)

Uploaded CPython 3.13Windows x86-64

maillite-0.1.9-cp313-cp313-manylinux_2_17_x86_64.whl (481.9 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

maillite-0.1.9-cp312-cp312-win_amd64.whl (80.3 kB view details)

Uploaded CPython 3.12Windows x86-64

maillite-0.1.9-cp312-cp312-manylinux_2_17_x86_64.whl (494.3 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

maillite-0.1.9-cp311-cp311-win_amd64.whl (80.8 kB view details)

Uploaded CPython 3.11Windows x86-64

maillite-0.1.9-cp311-cp311-manylinux_2_17_x86_64.whl (466.2 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

maillite-0.1.9-cp310-cp310-win_amd64.whl (80.6 kB view details)

Uploaded CPython 3.10Windows x86-64

maillite-0.1.9-cp310-cp310-manylinux_2_17_x86_64.whl (448.1 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

maillite-0.1.9-cp39-cp39-win_amd64.whl (103.6 kB view details)

Uploaded CPython 3.9Windows x86-64

maillite-0.1.9-cp39-cp39-manylinux_2_17_x86_64.whl (445.6 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

maillite-0.1.9-cp38-cp38-win_amd64.whl (105.8 kB view details)

Uploaded CPython 3.8Windows x86-64

maillite-0.1.9-cp38-cp38-manylinux_2_17_x86_64.whl (476.5 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

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

File metadata

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

File hashes

Hashes for maillite-0.1.9-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 bfdbfda9e1cbcfe4457ca0d83731d067a8e6d75101747459c808a1106024b1a6
MD5 50c6b4c7b3bc8ba24e16c0456db7194e
BLAKE2b-256 65076a886db8990a75a0c37d93a75a63458613df743c65defdcca43280523194

See more details on using hashes here.

Provenance

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

Publisher: build.yml on zhenzi0322-package/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.9-cp314-cp314-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for maillite-0.1.9-cp314-cp314-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 62163fead3b332406f6dd7a6e34d3a9f603a3d7d7d2119700381addf18ff782b
MD5 c1b24184151b898f9d3ba4bb69e68724
BLAKE2b-256 334550a0c41a7430dca80ed5c785cabc537cb6738adbde8425858261b55a16d7

See more details on using hashes here.

Provenance

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

Publisher: build.yml on zhenzi0322-package/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.9-cp313-cp313-win_amd64.whl.

File metadata

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

File hashes

Hashes for maillite-0.1.9-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 c79daea8b6066ff2e98717f42e469660fccd5192ed72cc709025cc2993802bd5
MD5 8a21b3d1bc44232a85317b2521b7c326
BLAKE2b-256 efa3dd49af7e327057e7e817dfc7f9a1742b2d55bb4203d74d27e7f99f0bcf37

See more details on using hashes here.

Provenance

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

Publisher: build.yml on zhenzi0322-package/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.9-cp313-cp313-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for maillite-0.1.9-cp313-cp313-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 92f2b3e9a60ea93f3958443b503978e8be27491d00b6eaf4839cb8a166a5af5f
MD5 243e504b932f4886bbd3320ab29743a4
BLAKE2b-256 ace9fd77a5809b7801f3746c7a70da9b0d63aec9465ac6ec06e376bf8b8cca3f

See more details on using hashes here.

Provenance

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

Publisher: build.yml on zhenzi0322-package/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.9-cp312-cp312-win_amd64.whl.

File metadata

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

File hashes

Hashes for maillite-0.1.9-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 bb42a1b216d7d09235e5c4512412e9be2feda9ae8b822361faa42977356a3f1f
MD5 5d209224de30aeb97bb468739a7fa5d3
BLAKE2b-256 da8caa8291f9669a6aeaebaf73d9db5411e71ea331afb0519fa420096eac3e52

See more details on using hashes here.

Provenance

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

Publisher: build.yml on zhenzi0322-package/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.9-cp312-cp312-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for maillite-0.1.9-cp312-cp312-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 fc9e9a97a6a7fa89fd5a155cad090774c53e17d0772c23a3595be90b638a9d70
MD5 21f6871fadd9b3064e1bc50da6ebb96e
BLAKE2b-256 5380f0bcc049fa6eac02f5211c8e1500da0fab64b9c6f3a308801182a21740b8

See more details on using hashes here.

Provenance

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

Publisher: build.yml on zhenzi0322-package/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.9-cp311-cp311-win_amd64.whl.

File metadata

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

File hashes

Hashes for maillite-0.1.9-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 0a405eece12af7b0d68bc261f229ecd76cc0f8ace34873c047782cac03a35fe9
MD5 a8e26bc1691e08f9185d89c5855f3dd5
BLAKE2b-256 af8a3e2b92ff6f7357f9c27d47bfa42c5daf553829a76395077f77c0c6ff1ddb

See more details on using hashes here.

Provenance

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

Publisher: build.yml on zhenzi0322-package/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.9-cp311-cp311-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for maillite-0.1.9-cp311-cp311-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 829ab4a7dcdb7a54fd5f8f59e9cce1c09fdc4aab310f77a2a0eab2620ae6b84d
MD5 420a558ea6c8ff2937bf5e9548e50b6d
BLAKE2b-256 00d6f0ef5580c1a4916cc1aec4b437003614fd5294c59c147d6bd8780d610fc9

See more details on using hashes here.

Provenance

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

Publisher: build.yml on zhenzi0322-package/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.9-cp310-cp310-win_amd64.whl.

File metadata

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

File hashes

Hashes for maillite-0.1.9-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 951096d7a4f26e56f9923038b4c9344994fe039aa00665f7a62325d9e5e7f712
MD5 8ea649276d3bb2c8002d48aee0ad0257
BLAKE2b-256 77cd7b66b06141b01aca3e768d20c70fe37147a795546ed0232abb81b7c73dff

See more details on using hashes here.

Provenance

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

Publisher: build.yml on zhenzi0322-package/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.9-cp310-cp310-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for maillite-0.1.9-cp310-cp310-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 c3c26b562b97917328c45b662ed8d27072c8df72be5855d129b0a44d2cca5828
MD5 5fed9e80278fd6e70b1a4b075e28f751
BLAKE2b-256 8b761104d3b6b12f74152b1fe939c68e0f1c2bd637b5da3c7a9c650fb72c86a3

See more details on using hashes here.

Provenance

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

Publisher: build.yml on zhenzi0322-package/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.9-cp39-cp39-win_amd64.whl.

File metadata

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

File hashes

Hashes for maillite-0.1.9-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 18d6830d85449857064f7fdf6b66ec947b84b2aa0fc92714f108fb5226796a68
MD5 f2120c313aa742b8a94cc78a9e5b21f2
BLAKE2b-256 1147bdb083036b19814cfda5814079e4ce7ffb4a30d837aefd6eb7c90d73f734

See more details on using hashes here.

Provenance

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

Publisher: build.yml on zhenzi0322-package/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.9-cp39-cp39-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for maillite-0.1.9-cp39-cp39-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 4ba35a88e42b5660cbb764181a423337c00002c11fe3c2ea0b24245db38aaed9
MD5 3abd6d63fc287b983bf2be5407e9f219
BLAKE2b-256 d67cce07f074d39d66bbc413ae37fce2dcec52dad2aa81ebe67b4e553f594032

See more details on using hashes here.

Provenance

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

Publisher: build.yml on zhenzi0322-package/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.9-cp38-cp38-win_amd64.whl.

File metadata

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

File hashes

Hashes for maillite-0.1.9-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 72a44c9bd31544afae2af11a8f1b57e72053001fa191fbaee7948a8b46f7e797
MD5 9f8bc55bb4c31e28c5d70f0f06da5280
BLAKE2b-256 4ce3a87f2a91d6179ee65d5b226f80b501732f2fe0094ca5bba9ab9fd3b91f8b

See more details on using hashes here.

Provenance

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

Publisher: build.yml on zhenzi0322-package/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.9-cp38-cp38-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for maillite-0.1.9-cp38-cp38-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 be37659f88bfae02d03a80f8c814ef451c7a3f3a9985bc614e9da0400e02d5ef
MD5 f580be08eb26cd9870fe1178a4f8e745
BLAKE2b-256 c5b5837be85e60342c7b0e57ea23321c64f1708fb0edf8f686b2f8b7d6177256

See more details on using hashes here.

Provenance

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

Publisher: build.yml on zhenzi0322-package/maillite

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

Release history Release notifications | RSS feed

This release

0.1.9 This release

14 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page