Welcome, guest | Sign In | My Account | Store | Cart

Notice! PyPM is being replaced with the ActiveState Platform, which enhances PyPM’s build and deploy capabilities. Create your free Platform account to download ActivePython or customize Python with the packages you require and get automatic updates.

Download
ActivePython
INSTALL>
pypm install iw.email

How to install iw.email

  1. Download and install ActivePython
  2. Open Command Prompt
  3. Type pypm install iw.email
 Python 2.7Python 3.2Python 3.3
Windows (32-bit)
1.4 Available View build log
Windows (64-bit)
1.4 Available View build log
Mac OS X (10.5+)
1.4 Available View build log
Linux (32-bit)
1.4 Available View build log
Linux (64-bit)
1.4 Available View build log
 
Author
License
GPL
Dependencies
Lastest release
version 1.4 on Jan 5th, 2011

iw.email package

What is iw.email ?

Provide a clean way to generate emails.

How to use iw.email ?

There is multiple way to use iw.email. See bellow.

MultipartMail

The base class of the package is the MultipartMail. You can use it to easily generate email both in html or text format with a correct encoding.

We need some html for email body:

>>> umail = unicode('''<html><body>

System Message: ERROR/3 (<string>, line 28)

Inconsistent literal block quoting.

... corps du maiil avec caractère unicode: ... utf-8: é à î ö ... cp552: xe2x80x93 xe2x80x99 ... </body></html>''', 'utf-8')

And a smtp server:

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

Now we can use the MultipartMail class to generate an email:

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

And send it:

>>> server.sendmail('sender@ingeniweb.com','recipient@ingeniweb.com', str(mail))

System Message: ERROR/3 (<string>, line 51)

Inconsistent literal block quoting.

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> ...

Ok, that's cool but sometimes we want to add images. So, just do it:

>>> image = open(os.path.join(testdir, 'bullet.gif'))
>>> image.read()

System Message: ERROR/3 (<string>, line 78)

Inconsistent literal block quoting.

'GIF89ax05x00rx00x80x00x00cx8cx9cxffxffxff!xf9x04x01x00x00x01x00,x00x00x00x00x05x00rx00x00x02tx8cx8fxa9xbbxe0x0fxa3x84xa9x00;' >>> 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

We can also use Cheetah template to generate an email:

>>> from iw.email import CheetahMail

Path is the path to the cheetah template:

>>> path = os.path.join(testdir, 'mail.tmpl')
>>> print open(path).read()

System Message: ERROR/3 (<string>, line 103)

Inconsistent literal block quoting.

$title

<BLANKLINE> $paragraph <BLANKLINE>

We need a few arguments:

>>> umail = unicode('''

System Message: ERROR/3 (<string>, line 115)

Inconsistent literal block quoting.

... corps du maiil avec caractère unicode: ... utf-8: é à î ö ... cp552: xe2x80x93 xe2x80x99 ... ''', 'utf-8')

Then we can use the CheetahMail to generate an email from the template:

>>> mail = CheetahMail(path=path,

System Message: ERROR/3 (<string>, line 123)

Inconsistent literal block quoting.

... 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> ...

Testing framework

iw.email provide a testing framework.

You just need to use iw.email.testing.smtpSetUp() and iw.email.testing.smtpTearDown() in your test case.

This will patch smtplib to allow you to test email sending in your doctests like you can see in this document.

You can also set some environment variable to also send the generated email. Here is the allowed variable:

System Message: WARNING/2 (<string>, line 162)

Literal block expected; none found.

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)

If the TEST_MAIL is set, the testing framework will try to send emails to it. So you just need this command if you have a local smtp server:

$ TEST_MAIL=gael@ingeniweb.com python setup.py test
Changes
1.4 (2009-08-01)
  • add EmailTestCase [gawel]
1.3 (2008-11-28)
  • add MakoMailTemplate [gawel]
  • allow to use a custom file like object for output in testing [gawel]
1.2 (2008-04-17)
  • need str(mail) in doctests [gawel]
1.1
  • raise an explicit message when no mail are provide [gawel]
0.1
  • initial version created by IngeniSkel

Subscribe to package updates

Last updated Jan 5th, 2011

Download Stats

Last month:3

What does the lock icon mean?

Builds marked with a lock icon are only available via PyPM to users with a current ActivePython Business Edition subscription.

Need custom builds or support?

ActivePython Enterprise Edition guarantees priority access to technical support, indemnification, expert consulting and quality-assured language builds.

Plan on re-distributing ActivePython?

Get re-distribution rights and eliminate legal risks with ActivePython OEM Edition.