2014-10-29 101 views
0

我想用Python中的附件重新發送電子郵件。我有這個代碼發送電子郵件,但我怎樣才能引用附件在另一封電子郵件?轉發電子郵件附件

發送

def show_emails(): 
    M.select() 
    typ, data = M.search(None, 'All') 
    for num in data[0].split(): 
     typ, data = M.fetch(num, '(RFC822)') 
     parser = Parser() 
     email = parser.parsestr(data[0][1]) 
     print "MESSAGE NUMBER %s" % (num) 
     print 'Raw Date:' 
     print email.get('Date') 
     print "From:" 
     print email.get('From') 
     print "Subject: " 
     print email.get('Subject') 

而且這個代碼是用於發送

msg = MIMEMultipart() 

mfrom = '[email protected]' 
mto = '[email protected]' 

msg['Subject'] = 'test' 
msg['From'] = mfrom 
msg['To'] = mto 
msg['Date'] = formatdate() 

# Open the file to scan in binary mode 
fp = open('/path/to/file', 'rb') 
attachment = MIMEBase('application', 'octet-stream') 
attachment.set_payload(fp.read()) 
encoders.encode_base64(attachment) 
attachment.add_header('Content-Disposition', 'attachment; filename="filename"') 
fp.close() 
msg.attach(attachment) 

我知道我需要檢查,如果有任何附件。我如何參考附件並轉發它?

if msg.is_multipart(): 
     for part in msg.walk(): 
     fileName = part.get_filename() 
     if bool(fileName): 
      print "Attachment: %s " % (decode_header(fileName)[0][0]) 
     else: 
      print "No attachments" 

回答

0

你不能只是引用它:那是什麼的RFC 4467-9是對的,但那些不是由衆多服務器和在這一點上我覺得他們死了實現。您必須下載附件併發送它,就像您發送本地文件一樣。