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

标签 python python-3.x lotus-notes win32com

提取电子邮件的一些数据后,我想使用 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/

相关文章:

python - 如何使用 psycopg2.sql 将列列表或 *(所有列)传递给动态 SQL 查询

python-3.x - 使用 setuptools 在 python 包中链接 f2py 生成的 *.so 文件

lotus-notes - ftsearch 返回空文档

python - 拟合高斯分布,必须使用 python 中提供的均值

python - 获取一列中所有值均为 nan 的行

python - EOFError : marshal data too short

python - 从列表中删除元组

c - 查找 C 的 Lotus Notes 安装

lotus-notes - Lotus Notes - 自动回复代理,用于接收到特定邮件接收的电子邮件

Python:处理平均时间,第一个/第二个慢得多