{"id":20613867,"url":"https://github.com/python-tools/sanic-mail","last_synced_at":"2026-03-09T11:31:54.849Z","repository":{"id":57463854,"uuid":"122711821","full_name":"Python-Tools/sanic-mail","owner":"Python-Tools","description":"async email sender for sanic,","archived":false,"fork":false,"pushed_at":"2023-02-07T01:55:31.000Z","size":450,"stargazers_count":8,"open_issues_count":2,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-02-19T11:04:15.916Z","etag":null,"topics":["email-sender","python36","sanic"],"latest_commit_sha":null,"homepage":null,"language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Python-Tools.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-02-24T06:41:01.000Z","updated_at":"2024-02-02T14:08:50.000Z","dependencies_parsed_at":"2023-02-19T14:15:35.859Z","dependency_job_id":null,"html_url":"https://github.com/Python-Tools/sanic-mail","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Python-Tools%2Fsanic-mail","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Python-Tools%2Fsanic-mail/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Python-Tools%2Fsanic-mail/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Python-Tools%2Fsanic-mail/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Python-Tools","download_url":"https://codeload.github.com/Python-Tools/sanic-mail/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242268603,"owners_count":20100067,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["email-sender","python36","sanic"],"created_at":"2024-11-16T11:11:17.279Z","updated_at":"2026-03-09T11:31:54.795Z","avatar_url":"https://github.com/Python-Tools.png","language":"HTML","funding_links":[],"categories":[],"sub_categories":[],"readme":"# sanic_mail\n\n+ version: 0.0.3\n+ status: dev\n+ author: hsz\n+ email: hsz1273327@gmail.com\n\n## Description\n\n使用`async await`异步执行邮件发送的任务\n\nkeywords:email,sanic\n\n## Feature\n\n+ 异步发邮件\n+ 支持html,html内嵌图片,附件\n\n## 使用方法\n\n### 设置\n\n```python\napp.config.update({\n    'MAIL_SENDER': \u003c 你的发送邮箱 \u003e,\n    'MAIL_SENDER_PASSWORD': \u003c 你的密码 \u003e,\n    'MAIL_SEND_HOST': \u003c 邮箱服务器地址 \u003e,\n    'MAIL_SEND_PORT': \u003c 端口 \u003e,\n    'MAIL_TLS': \u003c 是否使用TLS \u003e,\n    'MAIL_START_TLS': \u003c 是否启动TLS,注意与MAIL_TLS冲突 \u003e\n})\n```\n\n### 插件初始化\n\n和其他sanic插件一致有两种初始化方法:\n\n1. 使用`Sanic_Mail(app)`初始化.\n2. 先实例化`sm=Sanic_Mail()`再初始化`sm.init_app(app)`\n\n### 发送\n\n发送邮件的方法有两个:\n\n+ 协程`send_email`\n+ 方法`send_email_nowait`\n\n其中`send_email_nowait`意为将任务交给协程发送而不等待发送完毕,同时会返回发送的task.\n\n这个两个方法除了在`Sanic_Mail`实例上绑定外也会被绑定在`app.ctx`对象上\n\n在蓝图中或者比较复杂的项目中,app对象可以通过回掉函数的参数`request`上的`app`字段上获取到\n\n```python\nrequest.app.ctx.send_email(xxxx)\n```\n\n### 使用时的注意点\n\n+ html邮件中的图片\n\n    html邮件中可以插入图片,不过要求其中的cid为图片名去掉后缀的结果.\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': \u003c 你的发送邮箱 \u003e,\n    'MAIL_SENDER_PASSWORD': \u003c 你的密码 \u003e,\n    'MAIL_SEND_HOST': \u003c 邮箱服务器地址 \u003e,\n    'MAIL_SEND_PORT': \u003c 端口 \u003e,\n    'MAIL_TLS': \u003c 是否使用TLS \u003e\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/猫.jpg', \"rb\") as f:\n        attachments['猫.jpg'] = await f.read()\n    await request.app.ctx.send_email(\n        targetlist=\"hsz1273327@gmail.com\",\n        subject=\"测试发送\",\n        content=\"测试发送uu\",\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/猫.jpg', \"rb\") as f:\n        attachments['猫.jpg'] = await f.read()\n        msgimgs['猫.jpg'] = attachments['猫.jpg']\n\n    content = jinja.env.get_template('default.html').render(\n        name='sanic!',pic1=\"猫\"\n    )\n    await app.ctx.send_email(\n        targetlist=\"hsz1273327@gmail.com\",\n        subject=\"测试发送\",\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 \u003chttps://github.com/Sanic-Extensions/sanic-mail\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpython-tools%2Fsanic-mail","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpython-tools%2Fsanic-mail","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpython-tools%2Fsanic-mail/lists"}