Question regd downloading multipart mail using POP3

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Mar 28 18:19:23 EDT 2008


En Fri, 28 Mar 2008 16:47:51 -0300, SPJ <mail2spj at yahoo.com> escribió:

> 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.

None is a special object in Python, 'none' is a string, and none is a name  
that may refer to anything. They're not the same thing.
I would remove *all* Nones and nones and "none"s. An empty list is enough  
indication that there are no attachments, ok?

>     # regex for searching mails from example at example.com
>     soc = re.compile(".*example\@example.com.*")

This regex may be too slow. Use
soc = re.compile(r"example at example\.com")
(note the r and the \. and lack of .* on both ends; also @ isn't a special  
character so it's not necesary to escape it)

>         if soc.match(check1):
>             subject = msg1.get('Subject') # Subject for only mails

With the above reg.exp., use:
if soc.search(check1): ...

>                        if part.get_content_type() != ("text/html" or  
> "text/plain" or "multipart/alternative" or "multipart/mixed"):

That's wrong. The () evaluate simply to "text/html". You surely want this:

content_type = part.get_content_type()
if content_type not in ["text/html", "text/plain",  
"multipart/alternative", "multipart/mixed"]: ...

-- 
Gabriel Genellina




More information about the Python-list mailing list