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

Uploaded CPython 3.14Windows x86-64

maillite-0.1.5-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.5-cp313-cp313-win_amd64.whl (76.6 kB view details)

Uploaded CPython 3.13Windows x86-64

maillite-0.1.5-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.5-cp312-cp312-win_amd64.whl (77.7 kB view details)

Uploaded CPython 3.12Windows x86-64

maillite-0.1.5-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.5-cp311-cp311-win_amd64.whl (78.2 kB view details)

Uploaded CPython 3.11Windows x86-64

maillite-0.1.5-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.5-cp310-cp310-win_amd64.whl (78.0 kB view details)

Uploaded CPython 3.10Windows x86-64

maillite-0.1.5-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.5-cp39-cp39-win_amd64.whl (100.9 kB view details)

Uploaded CPython 3.9Windows x86-64

maillite-0.1.5-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.5-cp38-cp38-win_amd64.whl (103.2 kB view details)

Uploaded CPython 3.8Windows x86-64

maillite-0.1.5-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.5-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: maillite-0.1.5-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.5-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 6fd933175b569481af0ec34c9709d2de715834a2b85b588c0576c70caf30fe24
MD5 d2ed4df25889279d6368d21984d9de76
BLAKE2b-256 0bd83810b8fd52d56d60c26d53ab0ffe0f95eef2a4ad50cd0ea907a90a4279dd

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for maillite-0.1.5-cp314-cp314-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 4261c44f6cdf476e34a54479b84cc25259015bdcd5dc329d52ec0b94b24d73ed
MD5 fcb333e3129cd4248ee94aa61be3dcf4
BLAKE2b-256 abcc4a163f9ac5518cc5026746f42501ca48535811414d1b26a977d6e61c5513

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: maillite-0.1.5-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.5-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 64d2adf8ebcaddb259a4da19adfe1097f1bc1afd34d901b456d5e6acdb23f159
MD5 b1739c8c991b9ca881cde7bb98aec364
BLAKE2b-256 e7ab5f20d396186919a3ffa29262d13e26ae6f19d7d6ee8d96469746c9fab766

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for maillite-0.1.5-cp313-cp313-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 78957c3072a044a9ac8375aebccfbd84333f3cb85e161b8bc4f2a8b3d3764957
MD5 21e4359735d35d8193fc92ca11cd2cba
BLAKE2b-256 b668fb640c7ec60b387e3bba674cdfaa9ba29bae5af22d97e1cb78ff62fa99e0

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: maillite-0.1.5-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.5-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 ebba20baf60097cb80808dc9b9c6254c6661050bbbcdf9d2c9b6d39466a91316
MD5 915f0c092770e3474b1fef7861f65b7b
BLAKE2b-256 2b6f52174509fde04733cdb79d6195dbda65f3078026b0a599206d48d5f40938

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for maillite-0.1.5-cp312-cp312-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 746098aeb77e3ddedfdaee9ad4f22cdd5d8df8ebda3c3db2099a5098e71d7ee4
MD5 e150e5e0e82f8ab0243a69f735634366
BLAKE2b-256 2d4700550a409d822a99cd2564befe7c8eedc8b85c99851a588e8fd44246c98b

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: maillite-0.1.5-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.5-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 04614193a4f2826c9fa529b40e9c177f7130ecdd9c60db629a70de64c5d4349d
MD5 b81bc88c8fb64267561fced30e80ea0c
BLAKE2b-256 e05675e3775ada6dadfe2ef762bb2c5cf3811e96284c0e245472dbe630d1a20e

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for maillite-0.1.5-cp311-cp311-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 ccda0f589ce987491a3feadc052d5c4acbcf902d6d048e2d8f5439262f97a93e
MD5 a468163880cb2ebf42cd956f7b1dd659
BLAKE2b-256 44fe20840662b1c0dbb38119facadded404f0ce1065a62c3512a845f04d8278b

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: maillite-0.1.5-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.5-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 f65dbc244295dd557f2f6ebcc4a1f633b906b0a98394177ba1199dc7c9916911
MD5 459471d5de774d09e6a5fd454cea147f
BLAKE2b-256 47009518ed060099973bf800d5088acaf6b118c0790c1f9975f317272c5388cd

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for maillite-0.1.5-cp310-cp310-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 b104394e14c36702cc2f8032711278fb85356dc04c283ba77c5d8db96d49cba7
MD5 c37c83f7fadcced2891f65c2d74158cd
BLAKE2b-256 e5b877efcea15da29bc077e36cc7cccfc87cc72f1e49dd6f15fd026a4bed9b91

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: maillite-0.1.5-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.5-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 6e09b3c37f07bc9a08db66e198fbc96035e48ceca49efc54f3effad0dffd9ca5
MD5 6db8ef98b9b99ac591c3da4bdedf27da
BLAKE2b-256 99760914f869bf5c3df5c5f235497fe86bebe2bc1ec90e11ef2ec2ff63381589

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for maillite-0.1.5-cp39-cp39-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 adf6cdc72131692ec08bc9c5c383c683fdafa31abc8ca6a8817da9b8ed752426
MD5 e108deef558cd057fe7bc783abf6c3fa
BLAKE2b-256 9f819282ff1a45e730d05d578fea2da6f04b87a551c5150374217509f0f84c00

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: maillite-0.1.5-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.5-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 6f0ebd44744d20af4bf6d7be39866e223c8edfc62870fc8c1dc60f51f0e67b8a
MD5 0170261dcd76a3fe7963d4788cc0b176
BLAKE2b-256 036445b6c66af9513f2389e49f66ca6c46daa0a59791bca526510466c54e7767

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for maillite-0.1.5-cp38-cp38-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 2fb8a15101bebfb3083f82ad1d4c80d2f966a7e2f35119ad31a13dfe2904f29f
MD5 2708c578c947afc9f66064418682948f
BLAKE2b-256 f71ffaeb4a7e9900f78df77b489292fa2bd4c31328a48ad424545460418af3fb

See more details on using hashes here.

Provenance

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