2017-03-02 33 views
1

我使用email.message.Message對象來操作電子郵件消息,只需要提取html附件,而消息可以有html身體,所以看起來像get_content_type()在這裏沒有任何意義。如何區別電子郵件html正文和html附件在python中?

python有沒有簡單的方法來確定它是否是身體部位或附件?

UPD:

簡體前者的功能是這樣的:

def get_attachments(mail):    
    for part in mail.walk(): 
     if part.get_content_type() in ('application/pdf', 'image/png', 'image/jpeg'): 
      yield part 
+0

你能告訴我們你做了什麼這麼遠嗎? – Mingebag

回答

1

一切比我想象的要容易得多:

def get_attachments(mail):    
    for part in mail.walk(): 
     disposition = part.get('Content-Disposition') 
     if disposition and 'attachment' in disposition: 
      yield part 
+1

今天看着同樣的問題。你的解決方案是正確的,但可以寫得更短。考慮:如果part.get('Content-Disposition','').startswith('attachment') – jrial

相關問題