collect all plain text or html message parts

git-svn-id: svn://svn.cy55.de/Zope3/src/loops/trunk@3539 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
helmutm 2009-09-06 09:10:41 +00:00
parent 1af4275aab
commit 2877742faf

View file

@ -90,12 +90,13 @@ class IMAPCollectionProvider(object):
raw_date = msg['Date'].rsplit(' ', 1)[0] raw_date = msg['Date'].rsplit(' ', 1)[0]
fmt = '%a, %d %b %Y %H:%M:%S' fmt = '%a, %d %b %Y %H:%M:%S'
date = datetime(*(time.strptime(raw_date, fmt)[0:6])) date = datetime(*(time.strptime(raw_date, fmt)[0:6]))
parts = self.getPayload(msg, {}) parts = getPayload(msg)
if 'html' in parts: if 'html' in parts:
text = parts['html'] text = '<br /><br /><hr /><br /><br />'.join(parts['html'])
ct = 'text/html' ct = 'text/html'
else: else:
text = parts.get('plain') or u'No message found.' textList = parts.get('plain') or [u'No message found.']
text = '\n\n-------\n\n'.join(textList)
ct = 'text/plain' ct = 'text/plain'
obj = Resource(title) obj = Resource(title)
name = INameChooser(container).chooseName(None, obj) name = INameChooser(container).chooseName(None, obj)
@ -112,20 +113,27 @@ class IMAPCollectionProvider(object):
notify(ObjectModifiedEvent(obj)) notify(ObjectModifiedEvent(obj))
yield obj yield obj
def getPayload(self, msg, parts): def getPayload(msg, parts=None):
def getCharset(ct): if parts is None:
if 'charset=' in ct: parts = {}
cs = ct.split('charset=', 1)[1] if msg.is_multipart():
if ';' in cs: for part in msg.get_payload():
cs = cs.split(';', 1)[0] getPayload(part, parts)
return cs.replace('"', '') else:
if msg.is_multipart(): ct = msg['Content-Type']
for part in msg.get_payload(): if ct:
self.getPayload(part, parts) if ct.startswith('text/html'):
else: parts.setdefault('html', []).append(getText(msg, ct))
ct = msg['Content-Type'] elif ct.startswith('text/plain'):
if ct and ct.startswith('text/html'): parts.setdefault('plain', []).append(getText(msg, ct))
parts['html'] = msg.get_payload(decode=True).decode(getCharset(ct)) return parts
elif ct and ct.startswith('text/plain'):
parts['plain'] = msg.get_payload(decode=True).decode(getCharset(ct)) def getText(msg, ct):
return parts return msg.get_payload(decode=True).decode(getCharset(ct))
def getCharset(ct):
if 'charset=' in ct:
cs = ct.split('charset=', 1)[1]
if ';' in cs:
cs = cs.split(';', 1)[0]
return cs.replace('"', '')