2011-04-06 111 views
1

我無法轉發帶有附件的郵件。任何人都可以將我指向正確的方向嗎?我猜這是在Fetch語句中,但我不確定。Python - 轉發帶附件的IMAP電子郵件(imaplib,smtplib)

import sys 
import uuid 
import re 
import smtplib 
import email 

address = '' 
username = '' 
password = '' 

def checkMail(): 
    M = imaplib.IMAP4_SSL(address) 
    M.login(username, password) 
    M.select("INBOX", readonly=True) 
    typ, data = M.search(None, 'Unseen') 

    messageCount = len(data[0].split())  
    print('messageCount: %', messageCount) 
    if messageCount > 0:  
     for num in data[0].split(): 
      typ, data = M.fetch(num, '(BODY[TEXT])') 
      foundAt = data[0][1].find('Content-Type: application') 
      if(foundAt > 0): 
       print('attachmentfound') 
       sendMail(data[0][1]) 


    M.close() 
    M.logout()  


def sendMail(raw_message): 

    toAddress = '' 
    fromAddress = '' 
    LOGIN = '' 
    PASSWORD = '' 


    server = smtplib.SMTP('', 587) 
    server.set_debuglevel(1) 
    server.ehlo() 
    server.starttls() 
    server.login(LOGIN, PASSWORD) 
    server.sendmail(fromAddress, toAddress, raw_message) 
    server.quit() 

def main(): 
    checkMail() 

main() 
+0

您可能想要查看[IMAPClient](http://imapclient.freshfoo.com/)(緩解使用imaplib的許多挫折) – Acorn 2011-04-06 20:38:07

回答

0

這是取...添加取爲(BODY [])的伎倆。

+0

將其作爲評論發佈,或編輯您的答案以包含此內容 – bguiz 2011-04-07 02:02:09