Changeset 401 for trunk/moap/util/mail.py
- Timestamp:
- 24-06-09 11:38:18 (4 years ago)
- File:
-
- 1 edited
-
trunk/moap/util/mail.py (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/moap/util/mail.py
r203 r401 1 # -*- Mode: Python -*-1 # -*- Mode: Python; test-case-name: moap.test.test_util_mail -*- 2 2 # vi:si:et:sw=4:sts=4:ts=4 3 3 … … 6 6 7 7 import smtplib 8 import MimeWriter 9 import base64 10 import StringIO 8 9 from email import encoders 10 from email.mime import multipart, text, image, audio, base 11 11 12 12 """ … … 47 47 Get the message. 48 48 """ 49 COMMASPACE = ', ' 49 50 50 message = StringIO.StringIO() 51 writer = MimeWriter.MimeWriter(message) 52 writer.addheader('MIME-Version', '1.0') 53 writer.addheader('Subject', self.subject) 54 writer.addheader('To', ", ".join(self.to)) 55 56 writer.startmultipartbody('mixed') 57 58 # start off with a text/plain part 59 part = writer.nextpart() 60 body = part.startbody('text/plain') 61 body.write(self.content) 51 # Create the container (outer) email message. 52 msg = multipart.MIMEMultipart() 53 msg['Subject'] = self.subject 54 msg['From'] = self.fromm 55 msg['To'] = COMMASPACE.join(self.to) 56 msg.preamble = self.content 62 57 63 58 # add attachments 64 59 for a in self.attachments: 65 part = writer.nextpart() 66 part.addheader('Content-Transfer-Encoding', 'base64') 67 body = part.startbody('%(mime)s; name=%(name)s' % a) 68 body.write(base64.encodestring(a['content'])) 60 maintype, subtype = a['mime'].split('/', 1) 61 if maintype == 'text': 62 attach = text.MIMEText(a['content'], _subtype=subtype) 63 elif maintype == 'image': 64 attach = image.MIMEImage(a['content'], _subtype=subtype) 65 elif maintype == 'audio': 66 attach = audio.MIMEAudio(a['content'], _subtype=subtype) 67 else: 68 attach = base.MIMEBase(maintype, subtype) 69 attach.set_payload(a['content']) 70 # Encode the payload using Base64 71 encoders.encode_base64(msg) 72 # Set the filename parameter 73 attach.add_header( 74 'Content-Disposition', 'attachment', filename=a['name']) 75 msg.attach(attach) 76 77 return msg.as_string() 69 78 70 # finish off71 writer.lastpart()72 73 return message.getvalue()74 79 75 80 def send(self, server="localhost"): … … 79 84 80 85 return result 86 87
Note: See TracChangeset
for help on using the changeset viewer.
