Package: sanic-mail
sanic_mailversion: 0.0.3status: devauthor: hszemail:hsz1273327@gmail.comDescription使用async await异步执行邮件发送的任务keywords:email,sanicFeature异步发邮件支持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插件一致有两种初始化方法:使用Sanic_Mail(app)初始化.先实例化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为图片名去掉后缀的结果.Exampleimportaiofilesimportbase64fromsanicimportSanicfromsanic_jinja2importSanicJinja2fromsanic.responseimportjsonfromsanic_mailimportSanic_Mailapp=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')asyncdefsend(request):attachments={}asyncwithaiofiles.open("source/README.md","rb")asf:attachments["README.md"]=awaitf.read()asyncwithaiofiles.open('source/猫.jpg',"rb")asf:attachments['猫.jpg']=awaitf.read()awaitrequest.app.ctx.send_email(targetlist="hsz1273327@gmail.com",subject="测试发送",content="测试发送uu",attachments=attachments)returnjson({"result":"ok"})@app.get('/send_html')asyncdefsend_html(request):attachments={}msgimgs={}asyncwithaiofiles.open("source/README.md","rb")asf:attachments["README.md"]=awaitf.read()asyncwithaiofiles.open('source/猫.jpg',"rb")asf:attachments['猫.jpg']=awaitf.read()msgimgs['猫.jpg']=attachments['猫.jpg']content=jinja.env.get_template('default.html').render(name='sanic!',pic1="猫")awaitapp.ctx.send_email(targetlist="hsz1273327@gmail.com",subject="测试发送",content=content,html=True,msgimgs=msgimgs,attachments=attachments)returnjson({"result":"ok"})if__name__=="__main__":app.run(host='127.0.0.1',port=5000,debug=True)Installpython -m pip install sanic_mailDocumentationDocumentation on github pagehttps://github.com/Sanic-Extensions/sanic-mailv0.0.3fix bug and adapter to latest sanic
MIT LicenseCopyright (c) 2018 Sanic Extensions BuilderPermission 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.