Question regd downloading multipart mail using POP3

SPJ mail2spj at
Fri Mar 28 20:47:51 CET 2008

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
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', '']]

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. 


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)
   # 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"
    # regex for searching mails from example at
    soc = re.compile(".*example\*")

   # 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))
                        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
            else: # Not multipart, only body portion exists
                print "Not Multipart\n"
                tmpList.append(subject) # Subject from message
            if len(tmpList) > 0:

            if len(attachList) > 0:

    print msgList, attachmentName


def main():

if __name__== '__main__':


Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ

More information about the Python-list mailing list