使用Python将原始电子邮件与回复分开

2024-05-28 19:43:32 发布

您现在位置:Python中文网/ 问答频道 /正文

你们中有人知道我如何将原始电子邮件与回复分开吗?我想从对话中获得电子邮件的每一部分,但不包括发件人的姓名或其他详细信息。我尝试了这段代码,它工作得很好,但它只返回最近的电子邮件的正文。我无法得到答复的主体

以下是一个例子:

--Original email-- 
Dear Chadha,

I hope that you are doing well.

Best Regards, 
Date:..... 
Email:..... 
--reply-- 
From:.... 
to:..... 
Dear Yesmine, 
Thank you for asking, 
I am doing well. 
Kindest Regards, 
Date:...

我的问题是,我想把第一封原始邮件从回复中分离出来,并得到两封邮件的正文。我使用的代码只给出了第一封邮件的正文,没有回复。我正在寻找解决办法。我想正则表达式在这种情况下会对我有所帮助,但我不知道该怎么做

def extractBody(emailString):
    salutes = ["dear"] # A list containing greetings key words, like 'dear', 'hi', etc
    goodbyes = ["best regards"] # A list containing email footers like 'best regards' 'bye', etc

    # Split your email by line breaks and make everything lowercase
    emailLines = emailString.lower().split("\n")
    normalLines = emailString.split("\n") 

    # Start and end points to extract the text
    start = -1
    end = len(emailLines) - 1

    for i in range(len(emailLines)):
        line  = emailLines[i]

        # Check if any salute words in this line
        if len([s for s in salutes if s in line]) and start == -1:
            start = i + 1
            continue

        # Check if any goodbyes in this line
        if len([s for s in goodbyes if s in line]) and end == len(emailLines) - 1:
            end = i
            break

    if start == -1:
        return "\n".join(normalLines[:end])
    else:
        return "\n".join(normalLines[start:end])

extract =  extractBody(body).strip()
print(f"{extract}")

Tags: andinforlenif电子邮件emailline

热门问题