從事電子郵件營銷,准入opt-in郵箱列表是必不可少的。你可能已經有了准入列表,同時還使用電子郵件客戶端軟件。若是你能從電子郵件客戶端中導出准入列表,那這份列表想必是極好的。html
我使用一些代碼來將 outlook 配置中的全部郵件寫入一個臨時文件中,如今讓我來嘗試解釋一下這些代碼。python
首先你須要導入 win32com.client,爲此你須要安裝 pywin32:linux
pip install pywin32
咱們須要經過 MAPI 協議鏈接 Outlok:git
outlook=win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
而後從 outlook 配置中獲取全部的帳戶:github
accounts= win32com.client.Dispatch("Outlook.Application").Session.Accounts;
在而後須要從名爲 emaileri_al 的收件箱中獲取郵件:spa
def emailleri_al(folder): messages = folder.Items a=len(messages) if a>0: for message2 in messages: try: sender = message2.SenderEmailAddress if sender != "": print(sender, file=f) except: print("Ben hatayım") print(account.DeliveryStore.DisplayName) pass try: message2.Save message2.Close(0) except: pass
你須要進入全部帳戶的全部收件箱中獲取電子郵件:code
for account in accounts: global inbox inbox = outlook.Folders(account.DeliveryStore.DisplayName) print("****Account Name**********************************",file=f) print(account.DisplayName,file=f) print(account.DisplayName) print("***************************************************",file=f) folders = inbox.Folders for folder in folders: print("****Folder Name**********************************", file=f) print(folder, file=f) print("*************************************************", file=f) emailleri_al(folder) a = len(folder.folders) if a>0 : global z z = outlook.Folders(account.DeliveryStore.DisplayName).Folders(folder.name) x = z.Folders for y in x: emailleri_al(y) print("****Folder Name**********************************", file=f) print("..."+y.name,file=f) print("*************************************************", file=
下面是完整的代碼:htm
import win32com.client import win32com import os import sys f = open("testfile.txt","w+") outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") accounts= win32com.client.Dispatch("Outlook.Application").Session.Accounts; def emailleri_al(folder): messages = folder.Items a=len(messages) if a>0: for message2 in messages: try: sender = message2.SenderEmailAddress if sender != "": print(sender, file=f) except: print("Error") print(account.DeliveryStore.DisplayName) pass try: message2.Save message2.Close(0) except: pass for account in accounts: global inbox inbox = outlook.Folders(account.DeliveryStore.DisplayName) print("****Account Name**********************************",file=f) print(account.DisplayName,file=f) print(account.DisplayName) print("***************************************************",file=f) folders = inbox.Folders for folder in folders: print("****Folder Name**********************************", file=f) print(folder, file=f) print("*************************************************", file=f) emailleri_al(folder) a = len(folder.folders) if a>0 : global z z = outlook.Folders(account.DeliveryStore.DisplayName).Folders(folder.name) x = z.Folders for y in x: emailleri_al(y) print("****Folder Name**********************************", file=f) print("..."+y.name,file=f) print("*************************************************", file=f) print("Finished Succesfully")
via: https://www.codementor.io/aliacetrefli/how-to-read-outlook-emails-by-python-jkp2ksk95ip
做者:A.A. Cetrefli 選題:lujun9972 譯者:lujun9972 校對:wxyget