2017-08-01 135 views
0

我已經嘗試了很多代碼來訪問和閱讀電子郵件內容,例如,關於Gmail我只能進行身份驗證,並且使用Outlook,我的代碼可以讀取電子郵件但是已加密...但現在它只能訪問電子郵件,不會輸出加密信息。所以我需要幫助來解決這個問題。由於如何閱讀Python中的電子郵件內容3

下面的代碼:

import imaplib 
import base64 

email_user = input('Email: ') 
email_pass = input('Password: ') 

M = imaplib.IMAP4_SSL('imap-mail.outlook.com', 993) 
M.login(email_user, email_pass) 
M.select() 
typ, data = M.search(None, 'ALL') 
for num in data[0].split(): 
    typ, data = M.fetch(num, '(RFC822)') 
    num1 = base64.b64decode(num1) 
    data1 = base64.b64decode(data) 
    print('Message %s\n%s\n' % (num, data[0][1])) 
M.close() 
M.logout() 
+0

你在做什麼?你在期待什麼? – Nabin

+0

我只收到電子郵件身份驗證,並且期待輸出可以顯示收件箱中的電子郵件內容 – Ricardo91

回答

0

我已經通過你的代碼進行覈對無論如何,有一對夫婦的意見。第一個問題是,當你在這裏粘貼代碼時,逗號似乎已被刪除。我試圖讓他們回來,所以檢查我的代碼是否符合你的要求。請記住,如果沒有訪問Outlook,我無法測試我的建議。

import imaplib 
import base64 
email_user = input('Email: ') 
email_pass = input('Password: ') 

M = imaplib.IMAP4_SSL('imap-mail.outlook.com', 993) 
M.login(email_user, email_pass) 
M.select() 

typ, data = M.search(None, 'ALL') 

for num in data[0].split(): 
    typ, data = M.fetch(num, '(RFC822)') # data is being redefined here, that's probably not right 
    num1 = base64.b64decode(num1)   # should this be (num) rather than (num1) ? 
    data1 = base64.b64decode(data) 
    print('Message %s\n%s\n' % (num, data[0][1])) # don't you want to print num1 and data1 here? 

M.close() 
M.logout() 

假設這是正確地重新組織,調用第10行的郵件列表中的數據,但隨後重新分配調用取(),以數據線13

第14行,你解碼的結果num1,尚未定義。我不確定這些數字是否需要解碼,但這似乎有點奇怪。

在第16行,您打印的是編碼值,而不是您已解碼的值。我想你可能想要類似

import imaplib 
import base64 
email_user = input('Email: ') 
email_pass = input('Password: ') 

M = imaplib.IMAP4_SSL('imap-mail.outlook.com', 993) 
M.login(email_user, email_pass) 
M.select() 

typ, message_numbers = M.search(None, 'ALL') # change variable name, and use new name in for loop 

for num in message_numbers[0].split(): 
    typ, data = M.fetch(num, '(RFC822)') 
    # num1 = base64.b64decode(num)   # unnecessary, I think 
    print(data) # check what you've actually got. That will help with the next line 
    data1 = base64.b64decode(data[0][1]) 
    print('Message %s\n%s\n' % (num, data1)) 

M.close() 
M.logout() 

希望有所幫助。