跳转到主要内容

Email工具

项目描述

什么是iw.email ?

提供一个干净的方式来生成电子邮件。

如何使用iw.email ?

可以使用多种方式使用iw.email。请见下文。

MultipartMail

包的基本类是MultipartMail。您可以使用它轻松地以html或文本格式生成电子邮件,并具有正确的编码。

我们需要一些用于电子邮件正文的html

>>> umail = unicode('''<html><body>
... corps du maiil avec caractère unicode:
... utf-8: é à î ö
... cp552: \xe2\x80\x93 \xe2\x80\x99
... </body></html>''', 'utf-8')

以及一个smtp服务器

>>> from smtplib import SMTP
>>> server = SMTP('localhost')

现在我们可以使用MultipartMail类来生成一封电子邮件

>>> from iw.email import MultipartMail

>>> mail = MultipartMail(html=umail,
...             mfrom='sender@ingeniweb.com',
...             mto='recipient@ingeniweb.com',
...             subject=unicode('sujèéèt','utf-8'))

并发送它

>>> server.sendmail('sender@ingeniweb.com','recipient@ingeniweb.com', str(mail))
Content-Type: multipart/related; charset="iso-8859-1";
...
MIME-Version: 1.0
To: recipient@ingeniweb.com
From: sender@ingeniweb.com
Subject: =?iso-8859-1?q?suj=E8=E9=E8t?=
...
Content-Type: multipart/mixed; charset="iso-8859-1";
...
Content-Type: text/html; charset="iso-8859-1"
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
<BLANKLINE>
<html><body>
corps du maiil avec caract=E8re unicode:
utf-8: =E9 =E0 =EE =F6 =
<BLANKLINE>
cp552: - '
</body></html>
...

好的,这很酷,但有时我们想要添加图片。所以,就这样做

>>> image = open(os.path.join(testdir, 'bullet.gif'))
>>> image.read()
'GIF89a\x05\x00\r\x00\x80\x00\x00c\x8c\x9c\xff\xff\xff!\xf9\x04\x01\x00\x00\x01\x00,\x00\x00\x00\x00\x05\x00\r\x00\x00\x02\t\x8c\x8f\xa9\xbb\xe0\x0f\xa3\x84\xa9\x00;'
>>> image.seek(0)

>>> mail.addImage(image, filename='bullet.gif')
>>> mail.images
[<email...MIMEImage instance at ...>]
>>> print mail.images[0].as_string()
Content-Type: image/gif; name="bullet.gif"
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-ID: <bullet.gif>
<BLANKLINE>
R0lGODlhBQANAIAAAGOMnP///yH5BAEAAAEALAAAAAAFAA0AAAIJjI+pu+APo4SpADs=

CheetahMail

我们还可以使用Cheetah模板来生成电子邮件

>>> from iw.email import CheetahMail

路径是cheetah模板的路径

>>> path = os.path.join(testdir, 'mail.tmpl')
>>> print open(path).read()
==========
$title
==========
<BLANKLINE>
$paragraph
<BLANKLINE>

我们需要一些参数

>>> umail = unicode('''
... corps du maiil avec caractère unicode:
... utf-8: é à î ö
... cp552: \xe2\x80\x93 \xe2\x80\x99
... ''', 'utf-8')

然后我们可以使用CheetahMail从模板生成电子邮件

>>> mail = CheetahMail(path=path,
...             title='nice title',
...             paragraph=umail,
...             mfrom='sender@ingeniweb.com',
...             mto='recipient@ingeniweb.com',
...             subject=unicode('sujèéèt','utf-8'))
>>> server.sendmail('sender@ingeniweb.com','recipient@ingeniweb.com', str(mail))
Content-Type: multipart/related; charset="iso-8859-1";
...
To: recipient@ingeniweb.com
From: sender@ingeniweb.com
...
<body>
<div class=3D"document" id=3D"nice-title">
<h1 class=3D"title">nice title</h1>
<p>corps du maiil avec caract=E8re unicode:
iso-8859-1: =E9 =E0 =EE =F6
cp552: - '</p>
</div>
</body>
</html>
<BLANKLINE>
...

测试框架

iw.email提供测试框架。

您只需在测试用例中使用iw.email.testing.smtpSetUp()和iw.email.testing.smtpTearDown()。

这会将smtplib补丁,允许您在doctests中测试电子邮件发送,就像您可以在本文件中看到的那样。

您还可以设置一些环境变量以发送生成的电子邮件。以下是可以允许的变量

TEST_MAIL: the recipient
TEST_MAILFROM: mail from address (default to test@ingeniweb.com)
TEST_MAILHOST: hostname of an smtp server (default to localhost)
TEST_MAILPORT: smtp port (default to 25)

如果设置了TEST_MAIL,测试框架将尝试将其发送。所以如果您有一个本地smtp服务器,只需这个命令

$ TEST_MAIL=gael@ingeniweb.com python setup.py test

更改

1.4 (2009-08-01)

  • 添加EmailTestCase [gawel]

1.3 (2008-11-28)

  • 添加MakoMailTemplate [gawel]

  • 允许在测试中使用自定义文件对象输出 [gawel]

1.2 (2008-04-17)

  • 在doctests中需要str(mail) [gawel]

1.1

  • 当没有提供邮件时引发一个明确的错误信息 [gawel]

0.1

  • 初始版本由 IngeniSkel 创建

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定选择哪个,请了解有关 安装包 的更多信息。

源分布

iw.email-1.4.tar.gz (18.3 kB 查看哈希值)

上传时间

由以下支持