[Checkins] SVN: lovely.mail/trunk/ - added attachments

Juergen Kartnaller juergen at kartnaller.at
Wed Jul 29 11:23:40 EDT 2009


Log message for revision 102365:
   - added attachments
  

Changed:
  U   lovely.mail/trunk/CHANGES.txt
  U   lovely.mail/trunk/src/lovely/mail/README.txt
  U   lovely.mail/trunk/src/lovely/mail/__init__.py

-=-
Modified: lovely.mail/trunk/CHANGES.txt
===================================================================
--- lovely.mail/trunk/CHANGES.txt	2009-07-29 12:30:46 UTC (rev 102364)
+++ lovely.mail/trunk/CHANGES.txt	2009-07-29 15:23:40 UTC (rev 102365)
@@ -5,6 +5,8 @@
 After
 =====
 
+ - added attachments
+
 2009/04/22 0.2.0
 ================
 

Modified: lovely.mail/trunk/src/lovely/mail/README.txt
===================================================================
--- lovely.mail/trunk/src/lovely/mail/README.txt	2009-07-29 12:30:46 UTC (rev 102364)
+++ lovely.mail/trunk/src/lovely/mail/README.txt	2009-07-29 15:23:40 UTC (rev 102365)
@@ -80,7 +80,79 @@
     'Message-Id: ...\nFrom: ich <me at gmail.org>\nTo: du <you at 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 at gmail.org'), [('du','you at gmail.org',)],
+  ...          'my mail body', attachments=[(f1, 'f1.txt', None)])
+  >>> testing.sentMails = []
+  >>> testing.triggerMail()
+  >>> pprint(testing.sentMails)
+  [('ich <me at gmail.org>',
+    ('du <you at 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 at gmail.org>',
+   'To: du <you at 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"',
+   '',
+   'SSBhbSB0aGUgY29udGVudCBvZiBmaWxlIDE=',
+   '--===============...==--']
+
+  >>> f1.seek(0)
+  >>> sendmail('subject', ('ich', 'me at gmail.org'), [('du','you at gmail.org',)],
+  ...          'my mail body', attachments=[(f1, 'f1.txt', ('text','plain'))])
+  >>> testing.sentMails = []
+  >>> testing.triggerMail()
+  >>> pprint(testing.sentMails)
+  [('ich <me at gmail.org>',
+    ('du <you at 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 at gmail.org>',
+   'To: du <you at 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',
+   'MIME-Version: 1.0',
+   'Content-Transfer-Encoding: base64',
+   'Content-Disposition: attachment; filename="f1.txt"',
+   '',
+   'SSBhbSB0aGUgY29udGVudCBvZiBmaWxlIDE=',
+   '--===============...==--']
+
 And clean up.
 
   >>> testing.tearDownSMTPTesting()

Modified: lovely.mail/trunk/src/lovely/mail/__init__.py
===================================================================
--- lovely.mail/trunk/src/lovely/mail/__init__.py	2009-07-29 12:30:46 UTC (rev 102364)
+++ lovely.mail/trunk/src/lovely/mail/__init__.py	2009-07-29 15:23:40 UTC (rev 102365)
@@ -21,29 +21,46 @@
 from zope.sendmail.interfaces import IMailDelivery
 
 from email.MIMEText import MIMEText
+from email.MIMEMultipart import MIMEMultipart
+from email.MIMEBase import MIMEBase
+from email import Encoders
 import email.Charset
 email.Charset.add_charset('utf-8', email.Charset.SHORTEST, None, None)
+
 from datetime import datetime
 
 
-def sendmail(subject, fromaddr, toaddrs, body, replyTo=None, bodytype='plain'):
-
+def sendmail(subject, fromaddr, toaddrs, body,
+             replyTo=None, bodytype='plain',
+             attachments=[],
+            ):
     if isinstance(fromaddr, tuple):
         fromaddr = '%s <%s>'% fromaddr
-
     recipients = []
     for toaddr in toaddrs:
         if isinstance(toaddr, tuple):
             toaddr = '%s <%s>'% toaddr
         recipients.append(toaddr)
-
-    message = MIMEText(body.encode('utf-8'), bodytype, 'utf-8')
+    bodyText = MIMEText(body.encode('utf-8'), bodytype, 'utf-8')
+    if attachments:
+        message = MIMEMultipart()
+        message.attach(bodyText)
+    else:
+        message = bodyText
     message['Subject'] = subject
     message['From'] = fromaddr
     if replyTo:
         message['Reply-To'] = replyTo
     message['To'] = ', '.join(recipients)
     message['Date'] = datetime.now().strftime('%a, %d %b %Y %H:%M:%S +0000')
+    for f, name, mimetype in attachments:
+        if mimetype is None:
+            mimetype = ('application', 'octet-stream')
+        part = MIMEBase(*mimetype)
+        part.set_payload( f.read() )
+        Encoders.encode_base64(part)
+        part.add_header('Content-Disposition', 'attachment; filename="%s"' % name)
+        message.attach(part)
     mailer = component.getUtility(IMailDelivery, name='lovely-mail-delivery')
     mailer.send(fromaddr, recipients, message.as_string())
 



More information about the Checkins mailing list