Package: mailables
Introductionmailablesis a developer-centric utility for sending asynchronous mails.RequirementsPython 3.7+Installation$pipinstallmailablesUsagefrommailablesimportMailer,EmailMessagemessage=EmailMessage(to='User Name <user@example.com>',from_address='sender@example.com',subject='Hey',text_body='This is sent using mailables!',html_body='<b>This is sent using mailables!</b>')mailer=Mailer('smtp://localhost:25')awaitmailer.send(message)DependenciesMailables does not have any hard dependencies, but the following are optional:aiofiles- required byFileTransport.aiosmtpdlib- required bySMTPTransport.You can install all of these withpip install mailables[full].ConfigurationIn order to send email you need to configure transport and mailer.
Mailer is a class with which you would work all time. Think it is a public interface tomailables.
And transport is a concrete adapter which does actual sending.frommailablesimportMailermailer=Mailer('smtp://localhost:25')When you need to fine-grained control on the transport configuration
you may pass the transport instance instead of URL string:frommailablesimportMailer,SMTPTransportmailer=Mailer(SMTPTransport(host='localhost',port=25))This approach gives you full control on transport construction.Note, that you are not limited to one mailer only,
your application may have multiple mailers with different transports/settings.Mail messageThe mail message is represented byEmailMessageclass.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',)Attaching filesfrommailablesimportEmailMessage,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'))TransportsHere is the list of included transportsSMTPTransportSends messages using SMTP protocol.frommailablesimportSMTPTransporttransport=SMTPTransport(host='localhost',port=25,use_ssl=True)FileTransportFile transport does not send email to anywhere. It dumps messages into mailbox directory in*.emlformat.frommailablesimportFileTransporttransport=FileTransport(directory='/tmp/mailbox')InMemoryTransportStores sent emails in a local variable.frommailablesimportInMemoryTransport,EmailMessagestorage=[]transport=InMemoryTransport(storage=storage)# example:transport.send(EmailMessage(subject='Hey',to='root@localhost'))assertlen(storage)==1NullTransportThis transport does not send anything at all. Use it when you want to completely silence the mailer.frommailablesimportNullTransporttransport=NullTransport()Building custom transportsExtendmailables.transport.BaseTransportclass and
implementasync def send(self, message: EmailMessage)method:For example, we will create a simple transport class for writing mails to a stdout:frommailablesimportBaseTransport,EmailMessage,MailerclassConsoleTransport(BaseTransport):asyncdefsend(self,message:EmailMessage):print(str(message))mailer=Mailer(ConsoleTransport())