Lovely.Mail and Mail Testing
==============================
This package mainly provides a simple way to test the mail delivery using the
current configuration. There is no need to change the mailing configuration
for the functional tests.
>>> from lovely.mail import testing
Before we can set up the mail testing we need to register the mailer
utilities.
>>> from zope import component
>>> from zope.sendmail.mailer import SMTPMailer
>>> component.provideUtility(SMTPMailer(),
... name='lovely-mailer')
>>> from zope.sendmail.delivery import QueuedMailDelivery
>>> component.provideUtility(QueuedMailDelivery('some_path'),
... name='lovely-mail-delivery')
Now we set up testing. This is the code which should go into you
setUp-function for your tests.
>>> testing.setUpSMTPTesting('lovely-mailer', 'lovely-mail-delivery', unit_test=True)
Testing simply replaces the smtp mailer of the utility to a test smtp mailer.
>>> from zope.sendmail.interfaces import IMailer, IMailDelivery
>>> mailer = component.getUtility(IMailer, 'lovely-mailer')
>>> mailer.smtp
<class 'lovely.mail.testing.TestMailerConnection'>
And the mail delivery gets a temporary directory.
>>> delivery = component.getUtility(IMailDelivery, 'lovely-mail-delivery')
>>> delivery._queuePath != 'some_path'
True
Testing provides a list with already sent mails.
>>> testing.sentMails
[]
Now we send a mail.
>>> messageId = delivery.send('README', ['MAILQUEUE',], 'I am a testing mail')
>>> testing.sentMails
[]
The mail is not sent yet because we need to trigger mail delivery.
>>> testing.triggerMail()
>>> from pprint import pprint
>>> pprint(testing.sentMails)
[('README',
('MAILQUEUE',),
'Message-Id: <...>\nI am a testing mail')]
We also provide a simple function to send mails.
>>> from lovely.mail import sendmail
>>> sendmail('subject', 'me@gmail.org', ['you@gmail.org'], 'my mail body')
>>> testing.sentMails = []
>>> testing.triggerMail()
>>> pprint(testing.sentMails)
[('me@gmail.org',
('you@gmail.org',),
'Message-Id: ...\nFrom: me@gmail.org\nTo: you@gmail.org\n...\nmy mail body')]
If we provide tuples for the addresses we get this :
>>> sendmail('subject', ('ich', 'me@gmail.org'), [('du','you@gmail.org',)], 'my mail body')
>>> testing.sentMails = []
>>> testing.triggerMail()
>>> pprint(testing.sentMails)
[('ich <me@gmail.org>',
('du <you@gmail.org>',),
'Message-Id: ...\nFrom: ich <me@gmail.org>\nTo: du <you@gmail.org>\n...\nmy mail body')]
Attachments
-----------
Attachments must be provided as a list of tuples containing a file like object
providing "read", the filename and the mime type of the attachment (if known).
>>> from StringIO import StringIO
>>> f1 = StringIO("I am the content of file 1")
>>> sendmail('subject', ('ich', 'me@gmail.org'), [('du','you@gmail.org',)],
... 'my mail body', attachments=[(f1, 'f1.txt', None)])
>>> testing.sentMails = []
>>> testing.triggerMail()
>>> pprint(testing.sentMails)
[('ich <me@gmail.org>',
('du <you@gmail.org>',),
'Message-Id: ...')]
>>> pprint(testing.sentMails[0][2].split('\n'))
['Message-Id: <...>',
'Content-Type: multipart/mixed; boundary="===============...=="',
'MIME-Version: 1.0',
'Subject: subject',
'From: ich <me@gmail.org>',
'To: du <you@gmail.org>',
'Date: ...',
'',
'--===============...==',
'Content-Type: text/plain; charset="utf-8"',
'MIME-Version: 1.0',
'Content-Transfer-Encoding: 7bit',
'',
'my mail body',
'--===============...==',
'Content-Type: application/octet-stream',
'MIME-Version: 1.0',
'Content-Transfer-Encoding: base64',
'Content-Disposition: attachment; filename="f1.txt"',
'',
'SS...',
'--===============...==--']
>>> f1.seek(0)
>>> sendmail('subject', ('ich', 'me@gmail.org'), [('du','you@gmail.org',)],
... 'my mail body', attachments=[(f1, 'f1.txt', ('text','plain'))])
>>> testing.sentMails = []
>>> testing.triggerMail()
>>> pprint(testing.sentMails)
[('ich <me@gmail.org>',
('du <you@gmail.org>',),
'Message-Id: ...')]
>>> pprint(testing.sentMails[0][2].split('\n'))
['Message-Id: <...>',
'Content-Type: multipart/mixed; boundary="===============...=="',
'MIME-Version: 1.0',
'Subject: subject',
'From: ich <me@gmail.org>',
'To: du <you@gmail.org>',
'Date: ...',
'',
'--===============...==',
'Content-Type: text/plain; charset="utf-8"',
'MIME-Version: 1.0',
'Content-Transfer-Encoding: 7bit',
'',
'my mail body',
'--===============...==',
'Content-Type: text/plain; charset="us-ascii"',
'MIME-Version: 1.0',
'Content-Transfer-Encoding: 7bit',
'Content-Disposition: attachment; filename="f1.txt"',
'',
'I am the content of file 1',
'--===============...==--']
And clean up.
>>> testing.tearDownSMTPTesting()
>>> mailer.smtp
<class smtplib.SMTP at ...>
>>> delivery._queuePath == 'some_path'
True
>>> testing.sentMails
[]
=======================
Changes for lovely.mail
=======================
After
=====
2009/09/08 0.3.1
================
- allow the use of directMailDelivery for testing
- handle attachments according to their type
2009/07/30 0.3.0
================
- added attachments
2009/04/22 0.2.0
================
- stop remaining QueueProcessThread on tearDown because python 2.5 doesn't
terminate.
2008/04/22 0.1.3
================
- make the testmailer compatible to the latest zope.sendmail package
2007/12/10 0.1.2
================
- move stuff to zope.org and pypi
2007/08/23 0.1.1
================
- added remotemail.py for remote sendmail tasks
2007/06/20 0.1.0a1
==================
- added TestMailDelivery utility implementation
- changed to current bootstrap.py
- added this file
- cleaned imports
Download
========
Metadata
Release files for lovely.mail 0.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| lovely.mail-0.3.1.tar.gz | 9.5 kB | Without provenance | Details for lovely.mail-0.3.1.tar.gz |
Release files / lovely.mail-0.3.1.tar.gz
| Download URL | lovely.mail-0.3.1.tar.gz |
|---|---|
| Size | 9.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e7721ec93c65ac029dbd2005e64b5fafa5081393a1c350d33d24275e88291c3d
|
|
BLAKE2b-256 checksum How to use checksums |
cd1e1748e3cf25db13ffd3477e93887313ff614eecd32485497403d615880b1d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for lovely.mail 0.3.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.