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

Uploaded CPython 3.14Windows x86-64

maillite-0.1.3-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.3-cp313-cp313-win_amd64.whl (76.8 kB view details)

Uploaded CPython 3.13Windows x86-64

maillite-0.1.3-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.3-cp312-cp312-win_amd64.whl (77.8 kB view details)

Uploaded CPython 3.12Windows x86-64

maillite-0.1.3-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.3-cp311-cp311-win_amd64.whl (78.4 kB view details)

Uploaded CPython 3.11Windows x86-64

maillite-0.1.3-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.3-cp310-cp310-win_amd64.whl (78.1 kB view details)

Uploaded CPython 3.10Windows x86-64

maillite-0.1.3-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.3-cp39-cp39-win_amd64.whl (101.1 kB view details)

Uploaded CPython 3.9Windows x86-64

maillite-0.1.3-cp39-cp39-manylinux_2_17_x86_64.whl (442.4 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

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

Uploaded CPython 3.8Windows x86-64

maillite-0.1.3-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.3-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: maillite-0.1.3-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.3-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 7a5a2f068884a51b7302cd6c11a0f3176948bdea01b8d1febc1bf5ed4db48253
MD5 2fd70ab08431b1514739089b4c4f6c8b
BLAKE2b-256 36f311df9fcbe59688c586caaa67cac8d2364d3b0f6370716c16f5143234cdba

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for maillite-0.1.3-cp314-cp314-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 dff467226a394a46f9a3d76f2f2a5c8ab8f1d3ee52307302477787ac901440ed
MD5 a24c1026635cbb5c0fb9ac8b3f99f1d5
BLAKE2b-256 eaaf76fd240b21df1efc1cecf8157af334cdd7bb8fc1a244ab085944ed3611a6

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: maillite-0.1.3-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.3-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 a6dc97cdcaacf7570e4298be13514fcbe71d21072d4fc3c80977a32377d50ec6
MD5 fd95328e6cb61f47f872b29a041113f0
BLAKE2b-256 e29884005008ad905d31d247b06326b2f132da0a7424a0a4d129f2d556b69ad0

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for maillite-0.1.3-cp313-cp313-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 22c23c9a4fdcece328f0983a72de01db817e6f1ae3c1b2a8766b82629bf38ae0
MD5 e62ce21052b47f2da04d651360533106
BLAKE2b-256 ec0c1372c23696d89dc886e7327e56d4340c34ad0f182efce4bdaa8950e6765e

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: maillite-0.1.3-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.3-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 d52975e32f7956f2a95cc3096db642894d845fb1eb9c80fee72c0be8264831bd
MD5 a539aaade88d5ed4683199856de5d10a
BLAKE2b-256 4d4499f6c106ef99a679124fa08693444a0adafa0cb6c9c692db861b0b8a5248

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for maillite-0.1.3-cp312-cp312-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 5f6e4ba5061665cdab0a0ea7f9658b78693f6b202597d8fb126cf47980d337f6
MD5 987961e935acdef057f5193543b51499
BLAKE2b-256 872d0babf0496ccf44052b85f82ff7214804161627f22ba4b4ccadf3c03a6ada

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: maillite-0.1.3-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.3-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 aab0273812f0c0db947a88bd28f3e772b59345c2966a41a820f36e2381286118
MD5 aa7878bd3285372da2cc496d313aa16b
BLAKE2b-256 4cb7973bb8260f03fbcc8234b539b68609fe845b1a306f5355bd83f9d477655d

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for maillite-0.1.3-cp311-cp311-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 a6f4cc8280d0213c7d03c4f76ae88e290d97996d7e3086bac90fbd505d14171a
MD5 633150583d973456f7c20e880d1821dd
BLAKE2b-256 346025b93a544747391feb9a01a302c16891183446106502fdec4c977c50071a

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: maillite-0.1.3-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.3-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 d15df2d8ac4cdf2d3f137f7f14d6567e86560fbff87af0b2e125a88a33972540
MD5 a5eb24a718f78af2be85e6b8de9775f4
BLAKE2b-256 463a0b264c22a654dd1a8e091f8a633e05bf1fe468839c09b2d0b55d9eb2644f

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for maillite-0.1.3-cp310-cp310-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 761a0d7a69e4bc1bdfb96f07977d597e950ac99348d041303bd9ae7cbf69b92a
MD5 a5095524f10ababc872c06ab08433cb0
BLAKE2b-256 c8a8d8b680e0df208cbd4050e002c23b7f75eb57728f6947da51fd870b0bd33a

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: maillite-0.1.3-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.3-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 1c359ebae239504840f943929be09d22d1a5e76ce5e14275c58dfbff83984894
MD5 365e3cbf8c85f31ca97e5c64fec76955
BLAKE2b-256 40326a49c6b89e33e65a7090dcc6a00912700c488db72d328911e3295737a534

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for maillite-0.1.3-cp39-cp39-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 0da93b012ca20e62ebdc7e9ae757d2365486d889dac12fdc505e465abd25c4bc
MD5 d3dabd4c4405bd1d9ca6577dd5b7cb0f
BLAKE2b-256 4e88a12f82e575e56de19ea32d396fb21981aa234f0604bb694b71705ba4e353

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: maillite-0.1.3-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.3-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 5e99d791d867fb24d1bb3668e94b77ebd56852d962770efa8882bb2e09b29667
MD5 35ec442e1a2ff1ffb03e818f950f31b4
BLAKE2b-256 4f13491a965afc13dcd1fd68933a6f5d9b81418dcbb2399b751dcd768f714b0e

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for maillite-0.1.3-cp38-cp38-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 79bfbf2df311e6a3168b7b893c7c55cdf4e2a083499464382fb98ab1dbca032e
MD5 b39ad3391899c8afb9646d0cce31c10b
BLAKE2b-256 249e63d8f4d6628a67fd41a698f09eb571e8469c0ec5a6cc937c51d7531272da

See more details on using hashes here.

Provenance

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