sanic-mail


Namesanic-mail JSON
Version 0.0.3 PyPI version JSON
download
home_pagehttps://github.com/Python-Tools/sanic-mail
Summaryasync email sender for sanic
upload_time2023-02-07 01:55:56
maintainer
docs_urlNone
authorhsz
requires_python
licenseMIT License
keywords email sanic
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # sanic_mail

+ version: 0.0.3
+ status: dev
+ author: hsz
+ email: hsz1273327@gmail.com

## Description

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

keywords:email,sanic

## Feature

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

## 使用方法

### 设置

```python
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`字段上获取到

```python
request.app.ctx.send_email(xxxx)
```

### 使用时的注意点

+ html邮件中的图片

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

## Example

```python
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.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Python-Tools/sanic-mail",
    "name": "sanic-mail",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "email,sanic",
    "author": "hsz",
    "author_email": "hsz1273327@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/39/bd/01d205d54839e023c9c1bea73d7c9f958c5e2bf763242480ace35f8ab378/sanic-mail-0.0.3.tar.gz",
    "platform": null,
    "description": "# sanic_mail\n\n+ version: 0.0.3\n+ status: dev\n+ author: hsz\n+ email: hsz1273327@gmail.com\n\n## Description\n\n\u4f7f\u7528`async await`\u5f02\u6b65\u6267\u884c\u90ae\u4ef6\u53d1\u9001\u7684\u4efb\u52a1\n\nkeywords:email,sanic\n\n## Feature\n\n+ \u5f02\u6b65\u53d1\u90ae\u4ef6\n+ \u652f\u6301html,html\u5185\u5d4c\u56fe\u7247,\u9644\u4ef6\n\n## \u4f7f\u7528\u65b9\u6cd5\n\n### \u8bbe\u7f6e\n\n```python\napp.config.update({\n    'MAIL_SENDER': < \u4f60\u7684\u53d1\u9001\u90ae\u7bb1 >,\n    'MAIL_SENDER_PASSWORD': < \u4f60\u7684\u5bc6\u7801 >,\n    'MAIL_SEND_HOST': < \u90ae\u7bb1\u670d\u52a1\u5668\u5730\u5740 >,\n    'MAIL_SEND_PORT': < \u7aef\u53e3 >,\n    'MAIL_TLS': < \u662f\u5426\u4f7f\u7528TLS >,\n    'MAIL_START_TLS': < \u662f\u5426\u542f\u52a8TLS,\u6ce8\u610f\u4e0eMAIL_TLS\u51b2\u7a81 >\n})\n```\n\n### \u63d2\u4ef6\u521d\u59cb\u5316\n\n\u548c\u5176\u4ed6sanic\u63d2\u4ef6\u4e00\u81f4\u6709\u4e24\u79cd\u521d\u59cb\u5316\u65b9\u6cd5:\n\n1. \u4f7f\u7528`Sanic_Mail(app)`\u521d\u59cb\u5316.\n2. \u5148\u5b9e\u4f8b\u5316`sm=Sanic_Mail()`\u518d\u521d\u59cb\u5316`sm.init_app(app)`\n\n### \u53d1\u9001\n\n\u53d1\u9001\u90ae\u4ef6\u7684\u65b9\u6cd5\u6709\u4e24\u4e2a:\n\n+ \u534f\u7a0b`send_email`\n+ \u65b9\u6cd5`send_email_nowait`\n\n\u5176\u4e2d`send_email_nowait`\u610f\u4e3a\u5c06\u4efb\u52a1\u4ea4\u7ed9\u534f\u7a0b\u53d1\u9001\u800c\u4e0d\u7b49\u5f85\u53d1\u9001\u5b8c\u6bd5,\u540c\u65f6\u4f1a\u8fd4\u56de\u53d1\u9001\u7684task.\n\n\u8fd9\u4e2a\u4e24\u4e2a\u65b9\u6cd5\u9664\u4e86\u5728`Sanic_Mail`\u5b9e\u4f8b\u4e0a\u7ed1\u5b9a\u5916\u4e5f\u4f1a\u88ab\u7ed1\u5b9a\u5728`app.ctx`\u5bf9\u8c61\u4e0a\n\n\u5728\u84dd\u56fe\u4e2d\u6216\u8005\u6bd4\u8f83\u590d\u6742\u7684\u9879\u76ee\u4e2d,app\u5bf9\u8c61\u53ef\u4ee5\u901a\u8fc7\u56de\u6389\u51fd\u6570\u7684\u53c2\u6570`request`\u4e0a\u7684`app`\u5b57\u6bb5\u4e0a\u83b7\u53d6\u5230\n\n```python\nrequest.app.ctx.send_email(xxxx)\n```\n\n### \u4f7f\u7528\u65f6\u7684\u6ce8\u610f\u70b9\n\n+ html\u90ae\u4ef6\u4e2d\u7684\u56fe\u7247\n\n    html\u90ae\u4ef6\u4e2d\u53ef\u4ee5\u63d2\u5165\u56fe\u7247,\u4e0d\u8fc7\u8981\u6c42\u5176\u4e2d\u7684cid\u4e3a\u56fe\u7247\u540d\u53bb\u6389\u540e\u7f00\u7684\u7ed3\u679c.\n\n## Example\n\n```python\nimport aiofiles\nimport base64\nfrom sanic import Sanic\nfrom sanic_jinja2 import SanicJinja2\nfrom sanic.response import json\nfrom sanic_mail import Sanic_Mail\n\napp = Sanic(__name__)\njinja = SanicJinja2(app)\napp.config.update({\n    'MAIL_SENDER': < \u4f60\u7684\u53d1\u9001\u90ae\u7bb1 >,\n    'MAIL_SENDER_PASSWORD': < \u4f60\u7684\u5bc6\u7801 >,\n    'MAIL_SEND_HOST': < \u90ae\u7bb1\u670d\u52a1\u5668\u5730\u5740 >,\n    'MAIL_SEND_PORT': < \u7aef\u53e3 >,\n    'MAIL_TLS': < \u662f\u5426\u4f7f\u7528TLS >\n})\nsender = Sanic_Mail(app)\n\n\n@app.get('/send')\nasync def send(request):\n    attachments = {}\n    async with aiofiles.open(\"source/README.md\", \"rb\") as f:\n        attachments[\"README.md\"] = await f.read()\n    async with aiofiles.open('source/\u732b.jpg', \"rb\") as f:\n        attachments['\u732b.jpg'] = await f.read()\n    await request.app.ctx.send_email(\n        targetlist=\"hsz1273327@gmail.com\",\n        subject=\"\u6d4b\u8bd5\u53d1\u9001\",\n        content=\"\u6d4b\u8bd5\u53d1\u9001uu\",\n        attachments=attachments\n    )\n    return json({\"result\": \"ok\"})\n\n\n@app.get('/send_html')\nasync def send_html(request):\n    attachments = {}\n    msgimgs = {}\n    async with aiofiles.open(\"source/README.md\", \"rb\") as f:\n        attachments[\"README.md\"] = await f.read()\n    async with aiofiles.open('source/\u732b.jpg', \"rb\") as f:\n        attachments['\u732b.jpg'] = await f.read()\n        msgimgs['\u732b.jpg'] = attachments['\u732b.jpg']\n\n    content = jinja.env.get_template('default.html').render(\n        name='sanic!',pic1=\"\u732b\"\n    )\n    await app.ctx.send_email(\n        targetlist=\"hsz1273327@gmail.com\",\n        subject=\"\u6d4b\u8bd5\u53d1\u9001\",\n        content=content,\n        html=True,\n        msgimgs = msgimgs,\n        attachments=attachments\n    )\n    return json({\"result\": \"ok\"})\n\nif __name__ == \"__main__\":\n    app.run(host='127.0.0.1', port=5000, debug=True)\n````\n\n## Install\n\n`python -m pip install sanic_mail`\n\n## Documentation\n\nDocumentation on github page <https://github.com/Sanic-Extensions/sanic-mail>\n\n# v0.0.3\n\nfix bug and adapter to latest sanic\nMIT License\n\nCopyright (c) 2018 Sanic Extensions Builder\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n",
    "bugtrack_url": null,
    "license": "MIT License",
    "summary": "async email sender for sanic",
    "version": "0.0.3",
    "split_keywords": [
        "email",
        "sanic"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "57df640abdb462deca470708b16aa00399f81470fc380cc3073121e6e390307a",
                "md5": "ad9846bda4e9c24dd205940e9ece8b28",
                "sha256": "88803d9842bd51b53f3ba4967036c86e015a531fa05a6813b949f5b3e1f3d5ce"
            },
            "downloads": -1,
            "filename": "sanic_mail-0.0.3-py3.11.egg",
            "has_sig": false,
            "md5_digest": "ad9846bda4e9c24dd205940e9ece8b28",
            "packagetype": "bdist_egg",
            "python_version": "0.0.3",
            "requires_python": null,
            "size": 6844,
            "upload_time": "2023-02-07T01:55:57",
            "upload_time_iso_8601": "2023-02-07T01:55:57.710578Z",
            "url": "https://files.pythonhosted.org/packages/57/df/640abdb462deca470708b16aa00399f81470fc380cc3073121e6e390307a/sanic_mail-0.0.3-py3.11.egg",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "05de617f914c4d1186514da415b56c741b8c6f1938386f20c85dcb21ba8ab23d",
                "md5": "41bd0450b1c5042878fd8ea5ae209a07",
                "sha256": "5327b14f47c0862995c405d42b7c13613821e99c01725ae3b1f0aac757f6e177"
            },
            "downloads": -1,
            "filename": "sanic_mail-0.0.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "41bd0450b1c5042878fd8ea5ae209a07",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 7822,
            "upload_time": "2023-02-07T01:55:54",
            "upload_time_iso_8601": "2023-02-07T01:55:54.206347Z",
            "url": "https://files.pythonhosted.org/packages/05/de/617f914c4d1186514da415b56c741b8c6f1938386f20c85dcb21ba8ab23d/sanic_mail-0.0.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "39bd01d205d54839e023c9c1bea73d7c9f958c5e2bf763242480ace35f8ab378",
                "md5": "ebae2afa360e4be7334d0b552051be01",
                "sha256": "614c925204a8d84b5e89946fd86ca88502e3bab7f4c0bfdaba505837bc4ab8f7"
            },
            "downloads": -1,
            "filename": "sanic-mail-0.0.3.tar.gz",
            "has_sig": false,
            "md5_digest": "ebae2afa360e4be7334d0b552051be01",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 6448,
            "upload_time": "2023-02-07T01:55:56",
            "upload_time_iso_8601": "2023-02-07T01:55:56.060803Z",
            "url": "https://files.pythonhosted.org/packages/39/bd/01d205d54839e023c9c1bea73d7c9f958c5e2bf763242480ace35f8ab378/sanic-mail-0.0.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-02-07 01:55:56",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "Python-Tools",
    "github_project": "sanic-mail",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "sanic-mail"
}
        
hsz
Elapsed time: 0.03666s