Skip to main content

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

Project description

MailLite

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

特性

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

安装

pip install maillite

从源码安装(开发模式):

git clone <repo-url>
cd maillite
pip install -e ".[dev]"

快速开始

发送纯文本邮件

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(),
        "occurred_at": "2026-07-08 20:30:00"
    },
)

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 按钮文案 "立即查看"
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 按钮文案 "查看详情"
sender_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) 动态添加模板目录。

运行测试

# 单元测试
python -m pytest tests/ -v

# 集成测试(真实 SMTP 发送)
# PowerShell:
$env:MAILLITE_INTEGRATION="1"
$env:SMTP_HOST="smtp.qq.com"
$env:SMTP_PORT="465"
$env:SMTP_USER="your@qq.com"
$env:SMTP_PASS="your_auth_code"
python -m pytest tests/test_mailer.py -k integration -v

许可证

LICENSE 文件。

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

Uploaded CPython 3.14Windows x86-64

maillite-0.1.4-cp314-cp314-manylinux_2_17_x86_64.whl (473.5 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

maillite-0.1.4-cp313-cp313-win_amd64.whl (76.8 kB view details)

Uploaded CPython 3.13Windows x86-64

maillite-0.1.4-cp313-cp313-manylinux_2_17_x86_64.whl (479.3 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

maillite-0.1.4-cp312-cp312-win_amd64.whl (77.8 kB view details)

Uploaded CPython 3.12Windows x86-64

maillite-0.1.4-cp312-cp312-manylinux_2_17_x86_64.whl (491.3 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

maillite-0.1.4-cp311-cp311-win_amd64.whl (78.4 kB view details)

Uploaded CPython 3.11Windows x86-64

maillite-0.1.4-cp311-cp311-manylinux_2_17_x86_64.whl (463.6 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

maillite-0.1.4-cp310-cp310-win_amd64.whl (78.1 kB view details)

Uploaded CPython 3.10Windows x86-64

maillite-0.1.4-cp310-cp310-manylinux_2_17_x86_64.whl (445.1 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

maillite-0.1.4-cp39-cp39-win_amd64.whl (101.1 kB view details)

Uploaded CPython 3.9Windows x86-64

maillite-0.1.4-cp39-cp39-manylinux_2_17_x86_64.whl (442.5 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

maillite-0.1.4-cp38-cp38-win_amd64.whl (103.3 kB view details)

Uploaded CPython 3.8Windows x86-64

maillite-0.1.4-cp38-cp38-manylinux_2_17_x86_64.whl (473.8 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

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

File metadata

  • Download URL: maillite-0.1.4-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 78.6 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.4-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 c10cd116a2089e4447d116d21b7099406855fe6afeef4c630fc8143be9cc4082
MD5 f9588bd02e5e7131864f9c0b9856687e
BLAKE2b-256 d636db2548be3d2831c885cb081728b2bb76a75dbf2246655055199fd55b499d

See more details on using hashes here.

Provenance

The following attestation bundles were made for maillite-0.1.4-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.4-cp314-cp314-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for maillite-0.1.4-cp314-cp314-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 74e2fa787827204f6c47360723d3261c5d344273d79b97a6bf177bc16582d683
MD5 917df59ebe2e6a5478cdc566af2746a9
BLAKE2b-256 41ddc2854b390dd75a956198b004036b34ff43be7b31a09cc138a69b5ac6de2a

See more details on using hashes here.

Provenance

The following attestation bundles were made for maillite-0.1.4-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.4-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: maillite-0.1.4-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 76.8 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.4-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 344e519d6940e049e4c48f725c93f8a6cca206c615ab7aff34faf3496acc24b2
MD5 9610710bc2aabe29d49b1e16b2b26fd5
BLAKE2b-256 75edb318e7939e8e7af9906a8c9162ac2f0bddac3962813bb756d39c1a043660

See more details on using hashes here.

Provenance

The following attestation bundles were made for maillite-0.1.4-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.4-cp313-cp313-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for maillite-0.1.4-cp313-cp313-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 71fb4b50a196157ad61f599f41a7b3e1cd9ffe4eeb7dd143ed9a9512624601f4
MD5 8e755088c337ea9aea2299184fab0d2a
BLAKE2b-256 1dd5e8dd6da46dbfe495e041091be804926af9a1a9f907a2558d7b4c73a5ed2d

See more details on using hashes here.

Provenance

The following attestation bundles were made for maillite-0.1.4-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.4-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: maillite-0.1.4-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 77.8 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.4-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 e3d9cf6dacca5a68723b17e71749bd3ba83274371658afddca8adefc4a7b79dd
MD5 b856ba31c7e3be8b1c8f418254fa748c
BLAKE2b-256 f65e26fb5b8669a60f35483dd49674a20d0a8ecaeaf01d67e7f3a65c35834a49

See more details on using hashes here.

Provenance

The following attestation bundles were made for maillite-0.1.4-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.4-cp312-cp312-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for maillite-0.1.4-cp312-cp312-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 47f1f538a197e7680cde75a03559ba84c221a9323e9e2ba575084df44cdddf3d
MD5 be33e1afd902238a691d4332b69cfa25
BLAKE2b-256 9f0e8d57417b8e955e23ee078e953c7ef3361b016c592ae614d4c22853125e2d

See more details on using hashes here.

Provenance

The following attestation bundles were made for maillite-0.1.4-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.4-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: maillite-0.1.4-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 78.4 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.4-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 3ff2ab0eafa30a3b707741a83f45cbec0745c17db439b39135bf3d235eac0cdf
MD5 16db28354d8352fd8ab2a82683dd10c8
BLAKE2b-256 06e8540df1b054c72c85da829c45e508e1a67a792b07a524067285108cd5f536

See more details on using hashes here.

Provenance

The following attestation bundles were made for maillite-0.1.4-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.4-cp311-cp311-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for maillite-0.1.4-cp311-cp311-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 34d7ec873102a6249f2b023775aa19d0af081c5f8bb744467f5c10e9dcec1c11
MD5 07718ac0a404bf4a10885596a58cae38
BLAKE2b-256 d9ea6d7fd56ae3ebadc94d5d7ff6b6427984c0bdc178309ecd4bdefb68e58b84

See more details on using hashes here.

Provenance

The following attestation bundles were made for maillite-0.1.4-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.4-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: maillite-0.1.4-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 78.1 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.4-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 0b8d465676e2adb85c73edf0e1b840a0ce418856d9fd0a12b823bb3520493771
MD5 00e8be2bc0f10a386c8963492f7d5b6e
BLAKE2b-256 51121d527ce467a121e3be3632ae3a668602928eb0306557d91485b2af563344

See more details on using hashes here.

Provenance

The following attestation bundles were made for maillite-0.1.4-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.4-cp310-cp310-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for maillite-0.1.4-cp310-cp310-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 c7ebb892887e8c5c0b23dd4c8f5a646691b3ad6c83ace5d6ee51e81327c9ef1a
MD5 49def2fab189fc87ce2c3b37738d94a8
BLAKE2b-256 459cd60e3689a559ca44ba31ff6f706df5ab024e11889a6e69ebf5d99a779cf6

See more details on using hashes here.

Provenance

The following attestation bundles were made for maillite-0.1.4-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.4-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: maillite-0.1.4-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 101.1 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.4-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 d447361744787fcc5739e279c2140d90345cb5e8413f9dd3c5d48748f30b2e0a
MD5 c481d9c9b23e62f780fd9b28a34a1641
BLAKE2b-256 83af0ce001130142c4d99c91d29bfccb01fe347251d4dc7ede932fc583300010

See more details on using hashes here.

Provenance

The following attestation bundles were made for maillite-0.1.4-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.4-cp39-cp39-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for maillite-0.1.4-cp39-cp39-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 4b98b6094189b84cc37596b2922b9d7f34d5a81b4e79e343f6c4822d8bc76744
MD5 71d4363125e61c25fd9df885e1023931
BLAKE2b-256 56ea4f52e53f787137d9844c93a835a05824feac26f4ad81a8867daf6c99f6f7

See more details on using hashes here.

Provenance

The following attestation bundles were made for maillite-0.1.4-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.4-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: maillite-0.1.4-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 103.3 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.4-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 2d2cbb13273e51ab7ef9f908e566823abd3f0ffab71746036988c242a7a991bd
MD5 09661a64ac34ecb62efb358119e4cdcc
BLAKE2b-256 b1e67d43828a30d90b0ebc6a56f0d9a1108502d61d2fecc4e66f9a9e5f99612b

See more details on using hashes here.

Provenance

The following attestation bundles were made for maillite-0.1.4-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.4-cp38-cp38-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for maillite-0.1.4-cp38-cp38-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 a70cc6718720ace82125a4b34a81a19722478c94cea93db0ee395ded9d92ea9d
MD5 b957e967225abb62eb461d33e2dde674
BLAKE2b-256 e1813bc1c6a21efd6e70bb1a585c33c3ba38a753c1d0be24b8b6096469e6aedf

See more details on using hashes here.

Provenance

The following attestation bundles were made for maillite-0.1.4-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