将 Lotus Notes 电子邮件移动到不同的文件夹 Python

Move Lotus Notes Email to a Different Folder Python

在提取了一些电子邮件的数据后,我想将电子邮件移动到 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")

当然,注意转义反斜杠或使用原始字符串文字以正确传递视图名称。

如果文件夹不存在,

doc.PutInFolder 会创建该文件夹。在这种情况下,用户需要具有创建 public 文件夹的权限,否则创建的文件夹将是私有的。 (如果文件夹已经存在,当然这不是问题。)