电子邮件实用程序
项目描述
<nav class="contents" id="contents" role="doc-toc">
内容
什么是 iw.email ?
提供一种干净的方式来生成电子邮件。
如何使用 iw.email ?
有多种方法可以使用 iw.email。见下文。
多部分邮件
包的基类是 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=
猎豹邮件
我们也可以使用 Cheetah 模板来生成邮件:
>>> from iw.email import CheetahMail
Path 是 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 以允许您在文档测试中测试电子邮件发送,就像您在本文档中看到的那样。
您还可以设置一些环境变量来发送生成的电子邮件。这是允许的变量:
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 [gawel] 中需要 str(mail)
1.1
在没有提供邮件时提出明确的消息 [gawel]
0.1
IngeniSkel 创建的初始版本