用于发送异步邮件的developercentric实用程序。
mailables的Python项目详细描述
简介
mailables
是一个以开发人员为中心的实用程序,用于发送异步邮件。在
要求
Python 3.7+
安装
$ pip install mailables
使用
^{pr2}$依赖关系
Mailables没有任何硬依赖项,但以下是可选的:
您可以使用pip install mailables[full]
安装所有这些。在
配置
为了发送电子邮件,您需要配置传输和邮件程序。
Mailer是一个你可以一直工作的类。认为它是mailables
的公共接口。
而transport是一个具体的适配器,它执行实际的发送。在
frommailablesimportMailermailer=Mailer('smtp://localhost:25')
当您需要对传输配置进行细粒度控制时 您可以传递传输实例而不是URL字符串:
frommailablesimportMailer,SMTPTransportmailer=Mailer(SMTPTransport(host='localhost',port=25))
这种方法使您可以完全控制交通建设。在
请注意,您不仅限于一个邮寄者, 您的应用程序可能有多个具有不同传输/设置的邮件发送程序。在
邮件
邮件消息由EmailMessage
类表示。在
frommailablesimportEmailMessagemessage=EmailMessage(to=['root@localhost','admin@localhost'],from_address='user@localhost',subject='This is a test email',text_body='And this is a body',html_body='And HTML body <b>supported</b> as well',)
附加文件
frommailablesimportEmailMessage,Attachmentmessage=EmailMessage(attachments=[Attachment(contents='CONTENTS',file_name='file.txt')])# or alternatively using `attach` method:message.attach(contents='CONTENTS',file_name='file.txt')# or you can add attachment instance using `add_attachment` method:message.add_attachment(Attachment('contents'))
运输
这是包含的运输工具清单
SMTPTransport
使用SMTP协议发送邮件。在
frommailablesimportSMTPTransporttransport=SMTPTransport(host='localhost',port=25,use_ssl=True)
文件传输
文件传输不会向任何地方发送电子邮件。{cds}格式的{cds}将邮件转储到目录中。在
frommailablesimportFileTransporttransport=FileTransport(directory='/tmp/mailbox')
内存传输
商店在本地变量中发送电子邮件。在
frommailablesimportInMemoryTransport,EmailMessagestorage=[]transport=InMemoryTransport(storage=storage)# example:transport.send(EmailMessage(subject='Hey',to='root@localhost'))assertlen(storage)==1
空传输
这根本就不发送任何信息。当你想让邮件完全静音时就用它。在
frommailablesimportNullTransporttransport=NullTransport()
构建自定义传输
扩展mailables.transport.BaseTransport
类和
实现async def send(self, message: EmailMessage)
方法:
例如,我们将创建一个简单的传输类,用于将邮件写入标准输出:
frommailablesimportBaseTransport,EmailMessage,MailerclassConsoleTransport(BaseTransport):asyncdefsend(self,message:EmailMessage):print(str(message))mailer=Mailer(ConsoleTransport())
- 项目
标签: