|
import streamlit as st |
|
import mailparser |
|
from email_reply_parser import EmailReplyParser |
|
from bs4 import BeautifulSoup |
|
|
|
|
|
def extract_latest_message(raw_email): |
|
try: |
|
|
|
mail = mailparser.parse_from_string(raw_email) |
|
|
|
|
|
st.write("Parsed Email Object:") |
|
st.json(mail.mail_json) |
|
|
|
|
|
text_parts = mail.text_plain |
|
html_parts = mail.text_html |
|
|
|
|
|
st.write("Text Parts:", text_parts) |
|
st.write("HTML Parts:", html_parts) |
|
|
|
|
|
if mail.text_plain: |
|
body = mail.text_plain[0] |
|
st.write("Extracted plain text body from email.") |
|
elif mail.text_html: |
|
|
|
body = mail.text_html[0] |
|
st.write("Extracted HTML body from email. Converting to plain text...") |
|
|
|
body = BeautifulSoup(body, "html.parser").get_text() |
|
else: |
|
body = "No body content found in email." |
|
|
|
|
|
st.write("Cleaned-up email body before parsing:") |
|
st.text_area("Parsed Body", value=body, height=200) |
|
|
|
|
|
latest_reply = EmailReplyParser.parse_reply(body) |
|
|
|
return latest_reply |
|
except Exception as e: |
|
return f"Error: {e}" |
|
|
|
|
|
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. |
|
""") |
|
|
|
|
|
raw_email = st.text_area("Paste the raw MIME email content here", height=300) |
|
|
|
|
|
if st.button("Extract Latest Message"): |
|
if raw_email.strip(): |
|
|
|
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() |