提取电子邮件的一些数据后,我想使用 python 将电子邮件移动到指定的文件夹。我已经搜索过,但似乎没有找到我需要的东西。
以前有人这样做过吗?
根据评论,我添加了我当前的逻辑,希望它能澄清我的问题。我循环浏览我的文件夹,提取详细信息。完成此操作后,我想将电子邮件移动到另一个文件夹。
import win32com.client
import getpass
import re
'''
Loops through Lotus Notes folder to view messages
'''
def docGenerator(folderName):
# Get credentials
mailServer = 'server'
mailPath = 'PubDir\inbox.nsf'
# Password
pw = getpass.getpass('Enter password: ')
# Connect
session = win32com.client.Dispatch('Lotus.NotesSession')
# Initializing the session and database
session.Initialize(pw)
db = session.GetDatabase(mailServer, mailPath)
# Get folder
folder = db.GetView(folderName)
if not folder:
raise Exception('Folder "%s" not found' % folderName)
# Get the first document
doc = folder.GetFirstDocument()
# If the document exists,
while doc:
# Yield it
yield doc
# Get the next document
doc = folder.GetNextDocument(doc)
# Loop through emails
for doc in docGenerator('Folder\Here'):
# setting variables
subject = doc.GetItemValue('Subject')[0].strip()
invoice = re.findall(r'\d+',subject)[0]
body = doc.GetItemValue('Body')[0].strip()
# Move email after extracting above data
# ???
最佳答案
由于您将在获取下一个文档之前移动文档,因此我建议将循环替换为
doc = folder.GetFirstDocument()
while doc:
docN = folder.GetNextDocument(doc)
yield doc
doc = docN
然后将邮件移动到您需要的正确文件夹
doc.PutInFolder(r"Destination\Folder")
doc.RemoveFromFolder(r"Origin\Folder")
当然,请注意转义反斜杠或使用原始字符串文字来正确传递 View 名称。
doc.PutInFolder 将创建该文件夹(如果该文件夹不存在)。在这种情况下,用户需要具有创建公用文件夹的权限,否则创建的文件夹将是私有(private)的。 (当然,如果该文件夹已经存在,这不是问题。)
关于python - 将 Lotus Notes 电子邮件移动到不同的文件夹 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49162879/