2010-06-06 83 views
1

我完全不熟悉編程,我試圖構建一個autorespoder來發送msg到特定的電子郵件地址。如何使用python遍歷收件箱中的每封電子郵件?

使用if語句,我可以檢查收件箱中是否存在來自某個地址的電子郵件,並且我可以發送電子郵件,但是如果該地址有多個電子郵件,我如何使用for循環發送來自該特定地址的每封電子郵件的電子郵件。

我試圖做此作爲一個循環:

for M.search(None, 'From', address) in M.select(): 

,但我得到的錯誤:你聲稱自己是新的「不能分配給函數調用」在該行

回答

4

編程,我最好的建議是:總是閱讀文檔。

也許你應該先閱讀tutorial


documentation提供了一個例子:

import getpass, imaplib 

M = imaplib.IMAP4() 
M.login(getpass.getuser(), getpass.getpass()) 
M.select() 
typ, data = M.search(None, 'ALL') 
for num in data[0].split(): 
    typ, data = M.fetch(num, '(RFC822)') 
    print 'Message %s\n%s\n' % (num, data[0][3]) 
M.close() 
M.logout() 

您是否嘗試過?


關於你的代碼:

當你定義一個for loop,它應該是這樣的:

for x in some_data_set: 

x是一個變量,同時持有一個項目的值(和只能在for循環體中訪問(有一個例外,但這裏不重要))。

你在做什麼與imaplib模塊無關,但只是錯誤的語法。

Btw。 .select()選擇一個郵箱,並且只返回郵箱中的郵件數量。即只是標量值,沒有序列,你可以遍歷:

IMAP4.select([mailbox[, readonly]])
Select a mailbox. Returned data is the count of messages in mailbox (EXISTS response). The default mailbox is 'INBOX'. If the readonly flag is set, modifications to the mailbox are not allowed.

(這確實涉及到imaplib模塊;))

相關問題