纤细、灵活、功能齐全的电子邮件库

mailem的Python项目详细描述


构建状态pythons

邮寄

超薄、灵活、功能齐全的电子邮件库。

  • Unicode
  • 简单的附件
  • 内联图像
  • 电子邮件模板
  • 单元测试工具
  • 一劳永逸。简单可爱:)

下面是一个完整的示例:

frommailemimportMessage,Postman,Attachment,ImageAttachmentfrommailem.connectionimportSMTPConnection# Create the messagemessages=[# Message with attachmentsMessage(['kolypto@gmail.com'],u"Mail'em test",u"<b>yeah baby, it works!</b>",attachments=[Attachment(u'test.txt',open('test.txt').read())]),# Message with inline images (!)Message(['kolypto@gmail.com'],u"Mail'em test with inline images",u"Cute: <img src='cid:cute.jpg' />",# cid:<filename>attachments=[ImageAttachment('cute.jpg',open('cute.jpg').read(),'inline')]),]# Initialize a postman with SMTP connection to GMailpostman=Postman('user@gmail.com',SMTPConnection('smtp.gmail.com',587,'user@gmail.com','pass',tls=True))# Send everything we havewithpostman.connect()asc:map(c.sendmail,messages)

另请参见模板

目录

发送消息

消息

Message(recipients,subject,html=None,text=None,sender=None,cc=None,bcc=None,attachments=None,reply_to=None,date=None,headers=None)

构造消息对象。

注意:

  • 完全支持Unicode,Unicode是默认值

  • 您可以提供htmltext内容。如果两者都被指定--消息将有一个"备用"容器, 因此用户将同时接收html和纯文本。客户将选择显示哪一个。

  • 电子邮件地址,例如收件人发件人,可以用下列格式之一指定:

    • 'user@example.com':只有一个电子邮件地址
    • ('user@example.com',u'govered user'):名为的电子邮件地址

参数:

  • 收件人:收件人列表
  • 主题:消息主题
  • html:消息正文,html
  • 文本:消息正文,文本
  • 发件人:发件人电子邮件地址。如果未显式设置,则发送时将使用默认值
  • 抄送:抄送列表
  • 密件抄送:密件抄送列表
  • 附件:附件列表
  • 回复:回复地址
  • < < >代码>日期:发送日期
  • 标题:其他标题

附件

Attachment(filename,data,content_type='application/octet-stream',disposition='attachment',headers=None)

文件附件信息。

这可以提供给构造中的对象。

  • 文件名:附件的文件名
  • 数据:原始文件数据
  • 内容类型:文件类型
  • 处置:内容处置:'附件','内联',…
  • 标题:附件的附加标题

图像附件

ImageAttachment(filename,data,disposition='attachment',headers=None)

图像附件。

  • 它从数据流中猜测内容类型

  • 支持"内联"图像:嵌入电子邮件中的图像。对模板有用。

    创建"inline"图像后,其文件名将用于"content id",允许在html正文中引用它:

    frommailemimportMessage,Attachment,ImageAttachmentmsg=Message(['test@example.com'],'Hello','<img src="cid:flowers.jpg" />',# Referenced with "cid:<filename>"attachments=[ImageAttachment('flowers.jpg',open('flowers.jpg').read(),'inline')])

参数:

  • 文件名:图像附件文件名。内联时也将成为"内容ID"。
  • 数据:原始文件数据

邮递员

Postman(sender,connection)

postman是用于通过配置的连接对象发送消息的对象。

示例:

frommailemimportMessage,Postmanfrommailem.connectionimportSMTPConnection# Construct the messagemsg=Message(['kolypto@gmail.com'],u"Mail'em test",u"<b>yeah baby, it works!</b>")# Create the postman (see SMTPConnection)postman=Postman('user@gmail.com',SMTPConnection(...))# Connect, and send the messagewithpostman.connect()asc:c.sendmail(msg)
  • 发件人:默认发件人:电子邮件或(姓名、电子邮件)。 用于未明确指定发件人地址的邮件。
  • 连接:要使用的连接对象。见下文。

邮递员。连接

connect()

连接邮递员上下文管理器。

返回:mailem.postman.connectedpostman

邮递员.回送

loopback()

获取一个上下文管理器,该管理器在此邮递员上安装环回连接。

这允许您通过模仿邮递员来记录传出的邮件。 请参见环回连接

返回:mockedpostmancontext manager,它循环回传出消息

连接

connection对象表示到可以为我们发送电子邮件的服务的连接。

smtpconnection

frommailemimportMessage,Postman,Attachment,ImageAttachmentfrommailem.connectionimportSMTPConnection# Create the messagemessages=[# Message with attachmentsMessage(['kolypto@gmail.com'],u"Mail'em test",u"<b>yeah baby, it works!</b>",attachments=[Attachment(u'test.txt',open('test.txt').read())]),# Message with inline images (!)Message(['kolypto@gmail.com'],u"Mail'em test with inline images",u"Cute: <img src='cid:cute.jpg' />",# cid:<filename>attachments=[ImageAttachment('cute.jpg',open('cute.jpg').read(),'inline')]),]# Initialize a postman with SMTP connection to GMailpostman=Postman('user@gmail.com',SMTPConnection('smtp.gmail.com',587,'user@gmail.com','pass',tls=True))# Send everything we havewithpostman.connect()asc:map(c.sendmail,messages)
0

SMTP连接。

有关可能发生的异常列表,请参见smtplib

示例:

frommailemimportMessage,Postman,Attachment,ImageAttachmentfrommailem.connectionimportSMTPConnection# Create the messagemessages=[# Message with attachmentsMessage(['kolypto@gmail.com'],u"Mail'em test",u"<b>yeah baby, it works!</b>",attachments=[Attachment(u'test.txt',open('test.txt').read())]),# Message with inline images (!)Message(['kolypto@gmail.com'],u"Mail'em test with inline images",u"Cute: <img src='cid:cute.jpg' />",# cid:<filename>attachments=[ImageAttachment('cute.jpg',open('cute.jpg').read(),'inline')]),]# Initialize a postman with SMTP connection to GMailpostman=Postman('user@gmail.com',SMTPConnection('smtp.gmail.com',587,'user@gmail.com','pass',tls=True))# Send everything we havewithpostman.connect()asc:map(c.sendmail,messages)
1

参数:

  • 主机:SMTP服务器主机名
  • 端口:SMTP服务器端口号。
  • 用户名:要验证的用户名
  • 密码:密码
  • 本地主机名:helo/ehlo命令的本地主机的fqdn。当none时,将自动检测。
  • ssl:使用ssl协议?
  • tls:使用tls握手?

环回连接

frommailemimportMessage,Postman,Attachment,ImageAttachmentfrommailem.connectionimportSMTPConnection# Create the messagemessages=[# Message with attachmentsMessage(['kolypto@gmail.com'],u"Mail'em test",u"<b>yeah baby, it works!</b>",attachments=[Attachment(u'test.txt',open('test.txt').read())]),# Message with inline images (!)Message(['kolypto@gmail.com'],u"Mail'em test with inline images",u"Cute: <img src='cid:cute.jpg' />",# cid:<filename>attachments=[ImageAttachment('cute.jpg',open('cute.jpg').read(),'inline')]),]# Initialize a postman with SMTP connection to GMailpostman=Postman('user@gmail.com',SMTPConnection('smtp.gmail.com',587,'user@gmail.com','pass',tls=True))# Send everything we havewithpostman.connect()asc:map(c.sendmail,messages)
2

环回连接允许记录所有传出的消息,而不是发送它们。

您可以手动安装:

frommailemimportMessage,Postman,Attachment,ImageAttachmentfrommailem.connectionimportSMTPConnection# Create the messagemessages=[# Message with attachmentsMessage(['kolypto@gmail.com'],u"Mail'em test",u"<b>yeah baby, it works!</b>",attachments=[Attachment(u'test.txt',open('test.txt').read())]),# Message with inline images (!)Message(['kolypto@gmail.com'],u"Mail'em test with inline images",u"Cute: <img src='cid:cute.jpg' />",# cid:<filename>attachments=[ImageAttachment('cute.jpg',open('cute.jpg').read(),'inline')]),]# Initialize a postman with SMTP connection to GMailpostman=Postman('user@gmail.com',SMTPConnection('smtp.gmail.com',587,'user@gmail.com','pass',tls=True))# Send everything we havewithpostman.connect()asc:map(c.sendmail,messages)
3

或者可以使用loopback()helper来模拟现有的邮递员:

frommailemimportMessage,Postman,Attachment,ImageAttachmentfrommailem.connectionimportSMTPConnection# Create the messagemessages=[# Message with attachmentsMessage(['kolypto@gmail.com'],u"Mail'em test",u"<b>yeah baby, it works!</b>",attachments=[Attachment(u'test.txt',open('test.txt').read())]),# Message with inline images (!)Message(['kolypto@gmail.com'],u"Mail'em test with inline images",u"Cute: <img src='cid:cute.jpg' />",# cid:<filename>attachments=[ImageAttachment('cute.jpg',open('cute.jpg').read(),'inline')]),]# Initialize a postman with SMTP connection to GMailpostman=Postman('user@gmail.com',SMTPConnection('smtp.gmail.com',587,'user@gmail.com','pass',tls=True))# Send everything we havewithpostman.connect()asc:map(c.sendmail,messages)
4

环回可以多次安装,只有顶级环回才能捕获消息:

frommailemimportMessage,Postman,Attachment,ImageAttachmentfrommailem.connectionimportSMTPConnection# Create the messagemessages=[# Message with attachmentsMessage(['kolypto@gmail.com'],u"Mail'em test",u"<b>yeah baby, it works!</b>",attachments=[Attachment(u'test.txt',open('test.txt').read())]),# Message with inline images (!)Message(['kolypto@gmail.com'],u"Mail'em test with inline images",u"Cute: <img src='cid:cute.jpg' />",# cid:<filename>attachments=[ImageAttachment('cute.jpg',open('cute.jpg').read(),'inline')]),]# Initialize a postman with SMTP connection to GMailpostman=Postman('user@gmail.com',SMTPConnection('smtp.gmail.com',587,'user@gmail.com','pass',tls=True))# Send everything we havewithpostman.connect()asc:map(c.sendmail,messages)
5

还要注意loopbackconnection子类list,因此所有列表方法(包括迭代)都可用。

模板化

模板

frommailemimportMessage,Postman,Attachment,ImageAttachmentfrommailem.connectionimportSMTPConnection# Create the messagemessages=[# Message with attachmentsMessage(['kolypto@gmail.com'],u"Mail'em test",u"<b>yeah baby, it works!</b>",attachments=[Attachment(u'test.txt',open('test.txt').read())]),# Message with inline images (!)Message(['kolypto@gmail.com'],u"Mail'em test with inline images",u"Cute: <img src='cid:cute.jpg' />",# cid:<filename>attachments=[ImageAttachment('cute.jpg',open('cute.jpg').read(),'inline')]),]# Initialize a postman with SMTP connection to GMailpostman=Postman('user@gmail.com',SMTPConnection('smtp.gmail.com',587,'user@gmail.com','pass',tls=True))# Send everything we havewithpostman.connect()asc:map(c.sendmail,messages)
6

模板化的电子邮件。

默认情况下,模板使用python的templaterenderer,它允许简单的php样式替换, 但这可以使用set_renderer()覆盖。

首先,定义一个模板:

frommailemimportMessage,Postman,Attachment,ImageAttachmentfrommailem.connectionimportSMTPConnection# Create the messagemessages=[# Message with attachmentsMessage(['kolypto@gmail.com'],u"Mail'em test",u"<b>yeah baby, it works!</b>",attachments=[Attachment(u'test.txt',open('test.txt').read())]),# Message with inline images (!)Message(['kolypto@gmail.com'],u"Mail'em test with inline images",u"Cute: <img src='cid:cute.jpg' />",# cid:<filename>attachments=[ImageAttachment('cute.jpg',open('cute.jpg').read(),'inline')]),]# Initialize a postman with SMTP connection to GMailpostman=Postman('user@gmail.com',SMTPConnection('smtp.gmail.com',587,'user@gmail.com','pass',tls=True))# Send everything we havewithpostman.connect()asc:map(c.sendmail,messages)
7

现在,有了模板,您可以通过调用它来将其呈现为a消息

frommailemimportMessage,Postman,Attachment,ImageAttachmentfrommailem.connectionimportSMTPConnection# Create the messagemessages=[# Message with attachmentsMessage(['kolypto@gmail.com'],u"Mail'em test",u"<b>yeah baby, it works!</b>",attachments=[Attachment(u'test.txt',open('test.txt').read())]),# Message with inline images (!)Message(['kolypto@gmail.com'],u"Mail'em test with inline images",u"Cute: <img src='cid:cute.jpg' />",# cid:<filename>attachments=[ImageAttachment('cute.jpg',open('cute.jpg').read(),'inline')]),]# Initialize a postman with SMTP connection to GMailpostman=Postman('user@gmail.com',SMTPConnection('smtp.gmail.com',587,'user@gmail.com','pass',tls=True))# Send everything we havewithpostman.connect()asc:map(c.sendmail,messages)
8

准备发送!:)< >

  • 主题:消息主题模板
  • html:html消息模板(如果有)
  • 文本:文本消息模板(如果有)
  • 附件:t的附件他是模版。很可能是内联元素。
  • 默认值:默认模板值(如果需要)。用户可以稍后覆盖这些设置。

模板。设置渲染器

frommailemimportMessage,Postman,Attachment,ImageAttachmentfrommailem.connectionimportSMTPConnection# Create the messagemessages=[# Message with attachmentsMessage(['kolypto@gmail.com'],u"Mail'em test",u"<b>yeah baby, it works!</b>",attachments=[Attachment(u'test.txt',open('test.txt').read())]),# Message with inline images (!)Message(['kolypto@gmail.com'],u"Mail'em test with inline images",u"Cute: <img src='cid:cute.jpg' />",# cid:<filename>attachments=[ImageAttachment('cute.jpg',open('cute.jpg').read(),'inline')]),]# Initialize a postman with SMTP connection to GMailpostman=Postman('user@gmail.com',SMTPConnection('smtp.gmail.com',587,'user@gmail.com','pass',tls=True))# Send everything we havewithpostman.connect()asc:map(c.sendmail,messages)
9

将渲染器设置为此模板使用。

呈现器是任何可以用模板字符串参数构造的类, 并使用模板值dict调用以呈现它。

如果未显式设置渲染器,则默认为pythontemplaterenderer。

请参见mailem/template/renderer.py" rel="nofollow">mailem/template/renderer.py:使用自定义行为很容易实现renderer!

  • 渲染器:渲染器类。
  • **kwargs:渲染器的附加参数(如果支持的话)

模板。默认值

Message(recipients,subject,html=None,text=None,sender=None,cc=None,bcc=None,attachments=None,reply_to=None,date=None,headers=None)
0

设置默认值。

新值将覆盖以前的值。

  • :默认模板值

模板。呼叫

Message(recipients,subject,html=None,text=None,sender=None,cc=None,bcc=None,attachments=None,reply_to=None,date=None,headers=None)
1

使用模板值创建消息对象。

  • 收件人:邮件收件人列表
  • :带模板值的字典
  • **kwargs:构造函数的关键字参数

返回:消息呈现的消息对象

模板。来自目录

Message(recipients,subject,html=None,text=None,sender=None,cc=None,bcc=None,attachments=None,reply_to=None,date=None,headers=None)
2

将目录导入为模板的便利类方法:

  • subject.txt是主题字符串模板

  • index.htm是HTML模板

  • index.txt是纯文本模板

  • 与"i-(*)"格式匹配的所有文件都作为"inline"附加,因此可以在模板中引用:

    例如,文件"i-flower.jpg"可以内联为<;img src="cid:flower.jpg"/>;

  • 所有其他文件都只是附件。

示例:

Message(recipients,subject,html=None,text=None,sender=None,cc=None,bcc=None,attachments=None,reply_to=None,date=None,headers=None)
3
  • 路径:目录的路径

  • 主题名称:主题模板文件名

  • html名称:html模板文件名

  • 文本名称:纯文本模板文件名

  • inline_rex:匹配应该内联的文件的正则表达式。

    如果regexp定义了捕获组,则组$1将用作事实文件名。

返回:模板模板

模板注册

Message(recipients,subject,html=None,text=None,sender=None,cc=None,bcc=None,attachments=None,reply_to=None,date=None,headers=None)
4

电子邮件模板注册表。

只需包含所有模板并允许按名称呈现这些模板。 如果您的应用程序中有多个模板,并且希望准备它们,则会很有用。

最初,注册表是空的,您可以逐个添加对象:

Message(recipients,subject,html=None,text=None,sender=None,cc=None,bcc=None,attachments=None,reply_to=None,date=None,headers=None)
5

或者,您可以使用templateregistry.from_directory()加载模板 从文件系统。

现在,要呈现一个模板,您可以按名称:

Message(recipients,subject,html=None,text=None,sender=None,cc=None,bcc=None,attachments=None,reply_to=None,date=None,headers=None)
6

模板注册。添加

Message(recipients,subject,html=None,text=None,sender=None,cc=None,bcc=None,attachments=None,reply_to=None,date=None,headers=None)
7

注册模板

  • 模板:模板对象

返回:mailem.template.template添加的模板(以备设置)

templateregistry.set_渲染器

Message(recipients,subject,html=None,text=None,sender=None,cc=None,bcc=None,attachments=None,reply_to=None,date=None,headers=None)
8

将渲染器设置为与所有模板一起使用。

在添加模板之前和之后都可以调用。

  • 渲染器:要使用的渲染器类
  • **kwargs:渲染器的附加参数

templateregistry.默认值

Message(recipients,subject,html=None,text=None,sender=None,cc=None,bcc=None,attachments=None,reply_to=None,date=None,headers=None)
0

在所有模板上设置默认值。

新值将覆盖以前的值。

在添加模板之前和之后都可以调用。

  • :默认模板值ES

模板注册。获取

Attachment(filename,data,content_type='application/octet-stream',disposition='attachment',headers=None)
0

按名称获取模板

  • 名称:模板名称

返回:mailem.template.template

templateregistry.from_目录

Attachment(filename,data,content_type='application/octet-stream',disposition='attachment',headers=None)
1

构造模板注册表的简便方法 其中每个模板都在一个子目录中

  • 路径:模板路径
  • **kwargs:模板的参数,如果需要的话,可以是

返回:mailem.template.registry.templateregistry

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java在一个问题被连续正确回答三次/并添加差异后,我如何将程序循环回开始   Java中未实例化的匿名类   java如何在Android中录制视频,只允许横向模式和最长时间录制时间   java从另一个活动发送实时消息   多线程java线程和互斥   java禁用Spring安全日志   JAVA伊奥。StreamCorruptedException:在与子级和父级ProcessBuilder通信时写入子级中的标准输出时,流头无效   使用Java(HttpURLConnection)对Restheart进行身份验证(对于Mongodb)   java如何解决Jenkins中的SAXParseException?   java为什么我需要mockito来测试Spring应用程序?   计算sin-cos和tan时缺乏精度(java)   java Hibernate。不同项目中相同一对一映射的不同行为   java图像滑块:如何使用JavaFX将图像放在另一个图像上   java Mockito在使用when时抛出NotAMockException   http Java servlet发送回响应