Skip to main content

sanic_mail

Description

使用async await异步执行邮件发送的任务

keywords:email,sanic

Feature

  • 异步发邮件
  • 支持html,html内嵌图片,附件

使用方法

设置

app.config.update({
    'MAIL_SENDER': < 你的发送邮箱 >,
    'MAIL_SENDER_PASSWORD': < 你的密码 >,
    'MAIL_SEND_HOST': < 邮箱服务器地址 >,
    'MAIL_SEND_PORT': < 端口 >,
    'MAIL_TLS': < 是否使用TLS >,
    'MAIL_START_TLS': < 是否启动TLS,注意与MAIL_TLS冲突 >
})

插件初始化

和其他sanic插件一致有两种初始化方法:

  1. 使用Sanic_Mail(app)初始化.
  2. 先实例化sm=Sanic_Mail()再初始化sm.init_app(app)

发送

发送邮件的方法有两个:

  • 协程send_email
  • 方法send_email_nowait

其中send_email_nowait意为将任务交给协程发送而不等待发送完毕,同时会返回发送的task.

这个两个方法除了在Sanic_Mail实例上绑定外也会被绑定在app.ctx对象上

在蓝图中或者比较复杂的项目中,app对象可以通过回掉函数的参数request上的app字段上获取到

request.app.ctx.send_email(xxxx)

使用时的注意点

  • html邮件中的图片

    html邮件中可以插入图片,不过要求其中的cid为图片名去掉后缀的结果.

Example

import aiofiles
import base64
from sanic import Sanic
from sanic_jinja2 import SanicJinja2
from sanic.response import json
from sanic_mail import Sanic_Mail

app = Sanic(__name__)
jinja = SanicJinja2(app)
app.config.update({
    'MAIL_SENDER': < 你的发送邮箱 >,
    'MAIL_SENDER_PASSWORD': < 你的密码 >,
    'MAIL_SEND_HOST': < 邮箱服务器地址 >,
    'MAIL_SEND_PORT': < 端口 >,
    'MAIL_TLS': < 是否使用TLS >
})
sender = Sanic_Mail(app)


@app.get('/send')
async def send(request):
    attachments = {}
    async with aiofiles.open("source/README.md", "rb") as f:
        attachments["README.md"] = await f.read()
    async with aiofiles.open('source/猫.jpg', "rb") as f:
        attachments['猫.jpg'] = await f.read()
    await request.app.ctx.send_email(
        targetlist="hsz1273327@gmail.com",
        subject="测试发送",
        content="测试发送uu",
        attachments=attachments
    )
    return json({"result": "ok"})


@app.get('/send_html')
async def send_html(request):
    attachments = {}
    msgimgs = {}
    async with aiofiles.open("source/README.md", "rb") as f:
        attachments["README.md"] = await f.read()
    async with aiofiles.open('source/猫.jpg', "rb") as f:
        attachments['猫.jpg'] = await f.read()
        msgimgs['猫.jpg'] = attachments['猫.jpg']

    content = jinja.env.get_template('default.html').render(
        name='sanic!',pic1="猫"
    )
    await app.ctx.send_email(
        targetlist="hsz1273327@gmail.com",
        subject="测试发送",
        content=content,
        html=True,
        msgimgs = msgimgs,
        attachments=attachments
    )
    return json({"result": "ok"})

if __name__ == "__main__":
    app.run(host='127.0.0.1', port=5000, debug=True)

Install

python -m pip install sanic_mail

Documentation

Documentation on github page https://github.com/Sanic-Extensions/sanic-mail

v0.0.3

fix bug and adapter to latest sanic MIT License

Copyright (c) 2018 Sanic Extensions Builder

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Metadata

Release files for sanic-mail 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sanic-mail 0.0.3
File Size Uploaded Provenance Actions
sanic-mail-0.0.3.tar.gz 6.4 kB Without provenance Details for sanic-mail-0.0.3.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for sanic-mail 0.0.3
File Interpreter ABI Platform Actions
sanic_mail-0.0.3-py3.11.egg Legacy Egg format - - Details for sanic_mail-0.0.3-py3.11.egg
sanic_mail-0.0.3-py3-none-any.whl Python 3 none any Details for sanic_mail-0.0.3-py3-none-any.whl

Total release size: 21.1 kB

Release files / sanic-mail-0.0.3.tar.gz

Download URL sanic-mail-0.0.3.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
614c925204a8d84b5e89946fd86ca88502e3bab7f4c0bfdaba505837bc4ab8f7
BLAKE2b-256 checksum
How to use checksums
39bd01d205d54839e023c9c1bea73d7c9f958c5e2bf763242480ace35f8ab378
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.1

Release files / sanic_mail-0.0.3-py3.11.egg

Download URL sanic_mail-0.0.3-py3.11.egg
Size 6.8 kB
Tags Egg
SHA-256 checksum
How to use checksums
88803d9842bd51b53f3ba4967036c86e015a531fa05a6813b949f5b3e1f3d5ce
BLAKE2b-256 checksum
How to use checksums
57df640abdb462deca470708b16aa00399f81470fc380cc3073121e6e390307a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.1

Release files / sanic_mail-0.0.3-py3-none-any.whl

Download URL sanic_mail-0.0.3-py3-none-any.whl
Size 7.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5327b14f47c0862995c405d42b7c13613821e99c01725ae3b1f0aac757f6e177
BLAKE2b-256 checksum
How to use checksums
05de617f914c4d1186514da415b56c741b8c6f1938386f20c85dcb21ba8ab23d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.1

Security for sanic-mail 0.0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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