5
我有一個python腳本檢查Gmail的IMAP服務器上的電子郵件,它顯示服務器上的最新電子郵件。然而,電子郵件地址正在接收來自多個不同帳戶的電子郵件我該如何處理這個腳本,並讓它查看特定發件人的郵件?例如,只有發送到「[email protected]」的電子郵件纔會出現。Python - imaplib - 查看郵件到特定發件人
import imaplib
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('[email protected]', 'password123')
mail.list()
# Out: list of "folders" aka labels in gmail.
mail.select("inbox") # connect to inbox.
result, data = mail.search(None, "ALL")
ids = data[0] # data is a list.
id_list = ids.split() # ids is a space separated string
latest_email_id = id_list[-1] # get the latest
result, data = mail.fetch(latest_email_id, "(RFC822)") # fetch the email body (RFC822) for the given ID
raw_email = data[0][1] # here's the body, which is raw text of the whole email
# including headers and alternate payloads
在此先感謝! :)
什麼,當你搜索,而不是 「全部」,在mail.search()爲您需要的郵件,會發生什麼? – Dave