I am facing a strange problem when I am trying to retrieve multipart mail from POP server with attachments. The task is to write a script which will retrieve mail with specific 'from' field. Get the subject, body and attachments if any and reuse this info to send mail with different format to ticketing system.
The global variable values I am expecting is: msgList = [[msg1_subject,msg1_body,0],[msg2_subject,msg2_body,1]] and so on attachmentList=[['none'],['attachname.txt','filename.pl']] where none is no attachments. But the result I am getting is: msgList = [[msg1_subject,msg1_body,0],[msg2_subject,msg2_body,1]] and so on attachmentList=[[none],['none', 'attachname.txt', 'filename.pl']] An extra 'none' in the attachment list except for plain/text mails without attachments. I have tried a lot to figure out how to eliminate this but with no luck do far :( I know that some part is getting excluded from the condition, but I am unable to figure out what is causing this. Following is the function I wrote: I am not an expert programmer hence my code is not clean, so pl. bear with me. Thanks, SPJ ---------- import poplib, email, re, sys, os msgList = [] # Global list that contains messages PATH = Path to save file on disk # path were attachements will be stored attachmentName = [] # Name of attachements def popconnect(): # connect the pop server and send username and password pconnect = poplib.POP3(server) pconnect.user(user) pconnect.pass_(passwd) # Give connection status, msg list and total msg size status, msg_list, octets = pconnect.list() if len(msg_list) == 0: print "No messages in Mailbox" pass # regex for searching mails from [EMAIL PROTECTED] soc = re.compile("[EMAIL PROTECTED]") # iterate over the number of new messages from counterpane and qppend the required fields # to a list. for msg_number in [msg.split(' ')[0] for msg in msg_list]: status, lines, octets = pconnect.retr(msg_number) msg1 = email.message_from_string('\n'.join(lines)) tmpList = [] attachList = [] attachnumber = 0 check1 = msg1.get('From') check2 = msg1.get('Subject') if soc.match(check1): subject = msg1.get('Subject') # Subject for only mails only after condition is true. if msg1.is_multipart(): for part in msg1.walk(): # multipart/* are just containers mptype = part.get_content_maintype() body = part.get_payload(decode=True) if mptype == "multipart": continue filename = part.get_filename() if filename: # Attached object with filename print 'in file ' +part.get_content_type() attachnumber += 1 tmpList[2] = attachnumber # number of attachments in message attachList.append(PATH+filename) # path to attachment # Copy the attachment to disk f = open(PATH + filename,"wb") f.write(part.get_payload(decode = True)) f.close() else: print part.get_content_type() if part.get_content_type() != ("text/html" or "text/plain" or "multipart/alternative" or "multipart/mixed"): tmpList.append(subject) # Subject from message tmpList.append(body) # Body from message tmpList.append(0)# If no attachments attachList.append('none') else: # Not multipart, only body portion exists print "Not Multipart\n" body=msg1.get_payload(decode=True) tmpList.append(subject) # Subject from message tmpList.append(body) tmpList.append(0) attachList.append('none') if len(tmpList) > 0: msgList.append(tmpList) if len(attachList) > 0: attachmentName.append(attachList) print msgList, attachmentName pconnect.quit() def main(): popconnect() if __name__== '__main__': main() ____________________________________________________________________________________ Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ -- http://mail.python.org/mailman/listinfo/python-list