File size: 1,899 Bytes
d4087f2
 
 
1c75fc2
d4087f2
 
 
 
 
 
 
1c75fc2
 
 
 
 
 
 
 
d4087f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c75fc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import streamlit as st
import mailparser
from email_reply_parser import EmailReplyParser
from bs4 import BeautifulSoup

# Function to extract the latest email message from raw email content
def extract_latest_message(raw_email):
    try:
        # Parse the email using mail-parser
        mail = mailparser.parse_from_string(raw_email)

        # Extract the plain text body if available
        if mail.text_plain:
            body = mail.text_plain[0]
        else:
            # If plain text is not available, fallback to HTML body and convert to plain text
            body = mail.body
            # Use BeautifulSoup to strip HTML tags and get plain text
            body = BeautifulSoup(body, "html.parser").get_text()

        # Use email-reply-parser to extract only the latest reply (remove quoted thread)
        latest_reply = EmailReplyParser.parse_reply(body)

        return latest_reply
    except Exception as e:
        return f"Error: {e}"

# Streamlit app
def main():
    st.title("Email Latest Message Extractor")

    st.write("""
    This tool extracts the latest message from a raw MIME email and removes any quoted thread or previous messages.
    Paste the raw email in MIME format in the text area below, and the tool will display the latest message.
    """)

    # Input field for the raw email content
    raw_email = st.text_area("Paste the raw MIME email content here", height=300)

    # Button to process the input
    if st.button("Extract Latest Message"):
        if raw_email.strip():
            # Call the function to extract the latest message
            latest_message = extract_latest_message(raw_email)
            st.subheader("Extracted Latest Message:")
            st.text_area("Latest Message", value=latest_message, height=200)
        else:
            st.warning("Please paste the raw MIME email content.")

if __name__ == "__main__":
    main()