构建和发送电子邮件的工具
mailtools的Python项目详细描述
邮件工具
编写web应用程序?想从中发送一些电子邮件吗?Mailtools可以 救命啊!
- Simple API for sending plain text messages, HTML and messages with attachments.
- ^{tt1}$ sends emails in the background and returns control to your application immediately, even when talking to slow remote servers.
- Temporary sending failures are automatically retried.
- Running your application in test mode? The ^{tt2}$ wrapper routes emails to a test address and not to live email addresses.
用法
创建简单的SMTP邮件程序:
from mailtools import SMTPMailer mailer = SMTPMailer('127.0.0.1')
此邮件程序将被阻止,直到邮件被发送,并且不会重试失败。使用 ThreadedMailer修复此问题:
mailer = ThreadedMailer(SMTPMailer('127.0.0.1'))
支持tls/starttls的邮件发送程序:
mailer = SMTPMailer('127.0.0.1', 465, transport_args={'security': 'TLS'}) mailer = SMTPMailer('127.0.0.1', transport_args={'security': 'STARTTLS'})
发送纯文本消息:
message = u'This is a plain text message' mailer.send_plain( u'sender@example.com', [u'recipient@example.com'], u'hi', message )
发送HTML消息:
message = u'<html><body>Look! HTML!</body></html>' mailer.send_html( u'sender@example.com', [u'recipient@example.com'], u'hi', message )
添加附件:
message = u'index.rst is attached to this message' mailer.send_plain( u'sender@example.com', [u'recipient@example.com'], u'hi', message, attachments=['index.rst'] )