熔融web框架的一个简单电子邮件组件
molten-mail的Python项目详细描述
熔融邮件
提供了一个简单的界面,用于使用Moltenweb应用程序设置smtp并从处理程序函数发送消息。请注意,这项工作主要源于“dan jacob”和贡献者的Flask-Mail扩展,但是已经进行了大量修改,以删除python 2支持并用作熔融组件。
安装
pip install molten-mail
如果计划使用html模板,则需要确保Jinja2已安装。
pip install molten-mail[templates]
用法
一定要检查examples文件夹中的示例应用程序和更复杂的用法。
示例设置
要从视图函数发送邮件消息,必须自己实例化一个Mail
实例,或者使用MailComponent
。MailComponent
将根据molten.Settings
中提供的设置实例化全局Mail
实例。
这里我们有一个最低限度可行的应用程序,能够发送电子邮件并返回204响应代码:
frommoltenimport(App,Route,Settings,SettingsComponent,QueryParams,HTTP_204,HTTP_400,Response,)frommolten_mailimportMailComponent,Mail,Message# Replace with your own SMTP parameterssettings=Settings({"MAIL_SERVER":"smtp.example.com","MAIL_USERNAME":"me@example.com","MAIL_PASSWORD":"dontaddthistoyourversioncontrol","MAIL_PORT":587,"MAIL_USE_TLS":True,"MAIL_DEFAULT_SENDER":"me@example.com",})defsend_message(params:QueryParams,mail:Mail):"""Emails an email address provided in the query string"""addresses=params.get_all("email")ifnotaddresses:returnResponse(HTTP_400,content="Provide emails in the query params to send a welcome message",)msg=Message(subject="Welcome to Molten!",body="Welcome to Molten! Glad to have you here.",recipients=addresses,)mail.send(msg)returnResponse(HTTP_204,content="")routes=[Route("/",send_message,"POST")]components=[SettingsComponent(settings),MailComponent()]app=App(routes=routes,components=components)
配置选项
可以使用包含在molten.Settings
中的选项将单例Mail
组件配置为在依赖注入中使用。这需要在实例中包含MailComponent
。键值可以全部为大写或小写,并以MAIL_
开头。可用选项有:
- “邮件服务器”:默认“本地主机”
- 'mail_username':默认无
- “邮件密码”:默认为无
- “邮件端口”:默认值25
- “mail_use_tls”:默认值为false
- “mail_use_ssl”:默认值为false
- “邮件默认发件人”:默认无
- “mail_debug”:默认值为false
- “mail_max_emails”:默认为无
- “mail_suppress_send”:默认值为false
- “邮件ascii附件”:false
发送消息
要发送消息,请实例化Mail
组件。然后创建Message
的实例,并使用mail.send(msg)
将其传递给您的Mail
组件
frommolten_mailimportMail,Messagemail=Mail(server="localhost",user="me@example.com",password="dontaddthistoyourversioncontrol",port=587,use_tls=True,default_sender="me@example.com")msg=Message(subject="Hey there!",body="Welcome to Molten Mail",recipients=["you@example.com"])mail.send(msg)
您的邮件收件人可以批量设置,也可以单独设置:
msg.recipients=['you@example.com','me@example.com']msg.add_recipient('otherperson@example.com')
如果已包含默认发件人,则无需显式设置邮件发件人,因为它将默认使用此配置值:
msg=Message('Hello',recipients=['you@example.com'])
发送者也可以作为包含名称和电子邮件地址的双元素元组传递,将按如下方式拆分:
msg=Message('Hello',sender=('Me','me@example.com'))assertmsg.sender=='Me <me@example.com>'
消息可以包含正文和/或HTML:
msg.body='message body'msg.html='<b>Hello Molten-mail!</b>'
方便功能send_message
也可用于创建和发送消息:
mail.send_message(subject="Your subject",body="Message body",recipients=["you@example.com"])
HTML电子邮件模板
熔融邮件包含一个方便的组件MailTemplates
,用于使用Jinja2呈现html电子邮件正文。在使用此模块之前,您必须自己安装jinja2
。
必须在应用程序中包含MailTemplatesComponent
,并将路径传递到包含模板的文件夹。
frommoltenimportApp,Route,Response,HTTP_204,Settings,SettingsComponentfrommolten_mailimportMail,MailComponentfrommolten_mail.templatesimportMailTemplates,MailTemplatesComponentsettings=Settings({...})defview_func(mail:Mail,mail_templates:MailTemplates):->Response:mail.send_message(subject="Hello Molten!",html=mail_templates.render("my_email_template.html",somevalue="Key values for the template context"),recipients=["you@example.com"])returnResponse(HTTP_204,content="")app=App(components=[SettingsComponent(settings),MailComponent(),MailTemplatesComponent('./path_to_templates_dir')],routes=[Route('/',view_func,method="POST"])
测试
要运行覆盖率测试套件,请首先以可编辑模式安装软件包,并满足其完整的测试要求:
$ pip install -e ".[dev]"
运行项目的测试
$ pytest --cov
要对多个python解释器运行测试,请使用:
$ tox
历史记录
0.1.0初始释放
- 初次发布。