[Dev] first pass at parsing pop3 messages
bear
bear at code-bear.com
Sat Mar 6 22:07:34 PST 2004
I've taken the MailAgent.py code that stuart had and made some changes
and additions to test loading email messages from a pop3 server and
storing them in the repository. This is just more hacking on my part to
get used to using Chandler internals.
I've attached the latest version here for comments, abuse, scorn or
whatever ;)
bear
-------------- next part --------------
import os, sys, string
import poplib, email
import mx.DateTime as DateTime
import repository.parcel.LoadParcels as LoadParcels
import application.Globals as Globals
from repository.persistence.XMLRepository import XMLRepository
from repository.item.Item import Item
from repository.schema.Attribute import Attribute
from OSAF.contentmodel.mail.Mail import MailParcel, MailMessage, EmailAddress, EmailAccount
from email.Utils import parsedate, parseaddr, getaddresses
def RetrieveAddress(addressItem):
address = None
if addressItem:
address = EmailAddress()
address.emailAddress = addressItem[0] + ' <' + addressItem[1] + '>'
return address
def MakeMessage(messageText, emailAccount = None):
msgItem = email.message_from_string(messageText)
print msgItem
msg = MailMessage()
# itemref="mail:relatedMessages"
# itemref="mail:attachments"
# itemref="mail:characterEncoding"
# itemref="mail:serverStatus"
# itemref="mail:deliveryStatus"
# itemref="mail:stronglyLinkedTask"
# itemref="mail:spamScore"
# itemref="mail:forwardedIn"
# itemref="mail:repliedToIn"
# itemref="mail:inReplyTo"
msg.dateSent = DateTime.mktime(parsedate(msgItem['Date'])) # itemref="mail:dateSent"
msg.dateReceived = DateTime.mktime(parsedate(msgItem['Delivery-date'])) # itemref="mail:dateReceived"
if msgItem['Envelope-To:']:
msg.sentTo = msgItem['Envelope-To:'] # itemref="mail:sentTo"
msg.subject = msgItem['Subject'] # itemref="mail:subject"
msg.replyAddress = RetrieveAddress(msgItem['Reply-To']) # itemref="mail:replyAddress"
msg.toAddress = [] # itemref="mail:toAddress"
for address in getaddresses(msgItem.get_all('To', [])):
msg.toAddress.append(RetrieveAddress(address))
msg.ccAddress = [] # itemref="mail:ccAddress"
for address in getaddresses(msgItem.get_all('Cc', [])):
msg.ccAddress.append(RetrieveAddress(address))
msg.bccAddress = [] # itemref="mail:bccAddress"
for address in getaddresses(msgItem.get_all('Bcc', [])):
msg.bccAddress.append(RetrieveAddress(address))
header = ''
for headerItem in msgItem.items():
header = header + headerItem[0] + ': ' + headerItem[1]
msg.messageHeader = header # itemref="mail:messageHeader"
msg.messageBody = msgItem.as_string() # itemref="mail:messageBody"
# @@@ need to store address to send receipt to
if msgItem['Disposition-Notification-To']: # itemref="mail:readReceiptRequested"
msg.readReceiptRequested = True
# itemref="mail:deliveryReceiptRequested"
priorityText = msgItem['X-Priority'] # itemref="content:priority"
if priorityText:
priority = string.atoi(priorityText[:1])
if priority == 1 or priority == 2:
msg.priority = 'High'
else:
if priority == 3:
msg.priority = 'Medium'
else:
msg.priority = 'Low'
if emailAccount:
msg.downloadAccount = emailAccount # itemref="mail:downloadAccount"
msg.hasBeenRead = False # itemref="mail:hasBeenRead"
msg.status = 'Unread' # itemref="mail:status"
#for part in msgItem.walk():
# print part.get_content_type()
return msg
if __name__ == '__main__':
Globals.chandlerDirectory = os.environ['CHANDLERDIR']
Globals.repository = XMLRepository('__Pop3_Test__')
Globals.repository.create()
schemaPack = os.path.join(os.environ['CHANDLERDIR'], 'repository', 'packs', 'schema.pack')
Globals.repository.loadPack(schemaPack)
parcelDir = os.path.join(os.environ['CHANDLERDIR'], 'parcels')
sys.path.insert(1, parcelDir)
LoadParcels.LoadParcel(os.path.join(parcelDir, 'OSAF', 'contentmodel', 'mail'),
'//parcels/OSAF/contentmodel/mail', parcelDir, Globals.repository)
Globals.repository.commit()
test = EmailAccount()
popItem = poplib.POP3('192.168.42.42')
popItem.user("wftest")
popItem.pass_("wf42")
numMessages = len(popItem.list()[1])
for i in range(numMessages):
s = string.join(popItem.retr(i+1)[1], '\n')
print 'message ', i
msg = MakeMessage(s, test)
#test.downloadedMail.append(MakeMessage(s, test))
Globals.repository.close()
Globals.repository.delete()
More information about the Dev
mailing list