Skip to main content
Cadet | Tier 2
March 13, 2025
Question

How to get the forwarded email section from an email message

  • March 13, 2025
  • 13 replies
  • 370 views

I need to create an automation anywhere bot to read bounced emails and extract the original mail subject and recipient from the email body.

But the ------------ Forwarded email---------- section is not captured in the email body

What can I do to sort this?

 

The process I used is mentioned below but the forwarded message section was not displayed in the message

 

Connect Email action - using imap

Loop action - Read email - assigned to a dictionary variable

Message action - display message was set to $EmailDetails{emailMessage}$

 

    13 replies

    Lu.Hunnicutt
    Pathfinder Community Team
    Pathfinder Community Team
    March 13, 2025

    Hi! I’m going to tag a few folks who may be able to help!

    @Vatsy 

    @Ganesh Bhat 

    @Marc Mueller 

    @devpatel 
    @pdube 

    Let's Connect! www.linkedin.com/in/lu-hunnicutt-1290jan
    Marc Mueller
    Most Valuable Pathfinder
    Most Valuable Pathfinder
    March 14, 2025

    Hi ​@Pamodi,

     

    could you add a screenshot of the “original” mail and what is shown in your message box please?

    We use the IMAP as well and we get the whole emailBody. 🤔

     

    Cheers

    Marc

    🤝Let's connect on LinkedIn: https://www.linkedin.com/in/developer-marc-mueller >>>>>>>> 📺Check out my YouTube channel - https://youtube.com/@AutomationDevelopmentEasy
    PamodiAuthor
    Cadet | Tier 2
    March 15, 2025

    @Marc Mueller   The section highlighted in yellow is not fetched for the body

     

    Marc Mueller
    Most Valuable Pathfinder
    Most Valuable Pathfinder
    March 15, 2025

    @Pamodi 

    Have tried to switch between 'Plain Text'  and 'Html'?

     

    Cheers

    Marc

    🤝Let's connect on LinkedIn: https://www.linkedin.com/in/developer-marc-mueller >>>>>>>> 📺Check out my YouTube channel - https://youtube.com/@AutomationDevelopmentEasy
    PamodiAuthor
    Cadet | Tier 2
    March 15, 2025

    Yes I tried that still the highlighted in yellow not fetched ​@Marc Mueller 

    madhu subbegowda
    Most Valuable Pathfinder
    Most Valuable Pathfinder
    March 17, 2025

    The issue you're facing is likely because the email body is in HTML format, and Automation Anywhere's $EmailDetails{emailMessage}$ variable may only extract the plain text portion, omitting certain structured content like forwarded sections, tables, or inline formatting.

    Solutions to Capture the Forwarded Section

    1. Extract the HTML Version of the Email

    Instead of using $EmailDetails{emailMessage}$, try using:

    • $EmailDetails{emailMessageHtml}$ → This captures the full HTML version, which may include the forwarded email section.

    2. Convert HTML to Plain Text (if needed)

    If $EmailDetails{emailMessageHtml}$ works but returns raw HTML, you can convert it to plain text using Python or an HTML parsing tool.

    • Approach using Python (via Automation Anywhere Python Script action):

    Python Script:

    from bs4 import BeautifulSoup

    def extract_plain_text(html_content):
        soup = BeautifulSoup(html_content, "html.parser")
        return soup.get_text()

    email_body = extract_plain_text($EmailDetails{emailMessageHtml}$)
    print(email_body)
     

    • You can then store this output in a variable for further processing.

    3. Check if Forwarded Content is Inside an Attachment

    Some mail servers handle forwarded messages by embedding them as attachments (EML or MSG format) instead of in the body.

    To check:

    1. Loop through attachments using $EmailDetails{emailAttachments}$.
    2. If the email has .eml or .msg attachments, open and extract the required details.

    4. Use a Different IMAP Email Property

    If $EmailDetails{emailMessageHtml}$ does not work, try using:

    • $EmailDetails{rawEmailContent}$ → Extracts the entire raw email content, including hidden parts.

    5. Check if Email Server is Modifying the Content

    Some email servers strip forwarded content for security reasons. To verify:

    • Try sending the same bounced email to another email provider (e.g., Gmail, Outlook) and compare the content.
    • If the forwarded section is missing only in Automation Anywhere, the issue is likely due to how IMAP fetches emails.

    Try this approach:

    1. Try $EmailDetails{emailMessageHtml}$ instead of $EmailDetails{emailMessage}$.
    2. If HTML output is messy, parse it using Python's BeautifulSoup.
    3. If forwarded content is in an attachment, extract the .eml or .msg file.
    4. As a last resort, check if the email server is modifying the message.
    Madhu Kumar T S || https://www.linkedin.com/in/madhukumarts/
    Shreya.Kumar
    Pathfinder Community Team
    March 17, 2025

    @Pamodi if ​@madhu subbegowda ‘s answer helped you, please do mark it as best answer! Thanks!

    PamodiAuthor
    Cadet | Tier 2
    March 17, 2025

    Hi ​@madhu subbegowda ,

    Is it possible to display $EmailDetails{emailMessageHtml}$ in a message?

    Even though email body not retrieved unread emails marked as read.

     

     

    madhu subbegowda
    Most Valuable Pathfinder
    Most Valuable Pathfinder
    March 17, 2025

    Can you try this - 

    Option 1: Extract Full Email Using Raw Content

    • Try displaying $EmailDetails{rawEmailContent}$ in a message box.
    • If this works, parse the required fields manually.

    Option 2: Read Email via Python & IMAP (More Reliable for Forwarded Content)

    If the built-in Connect Email action does not retrieve forwarded content properly, try using Python with IMAP:

     

    python script:

    import imaplib import email from bs4 import BeautifulSoup mail = imaplib.IMAP4_SSL("imap.server.com") mail.login("your_email", "your_password") mail.select("inbox") result, data = mail.search(None, "UNSEEN") email_ids = data[0].split() for e_id in email_ids: result, data = mail.fetch(e_id, "(RFC822)") raw_email = data[0][1].decode("utf-8") msg = email.message_from_string(raw_email) if msg.is_multipart(): for part in msg.walk(): if part.get_content_type() == "text/html": email_body = part.get_payload(decode=True).decode("utf-8") break else: email_body = msg.get_payload(decode=True).decode("utf-8") soup = BeautifulSoup(email_body, "html.parser") plain_text = soup.get_text() print(plain_text) # Extracted email content

    • This script retrieves the full email body and extracts plain text, including forwarded content.
    • You can run this in Automation Anywhere's Python Script action to fetch emails instead of using the built-in IMAP action.

    Let me know if this doesn’t work.

    Madhu Kumar T S || https://www.linkedin.com/in/madhukumarts/
    madhu subbegowda
    Most Valuable Pathfinder
    Most Valuable Pathfinder
    March 17, 2025

    You may also try to troubleshoot:

    • Confirm Email Content is Available in IMAP Response

      • Before extracting email content, loop through the email headers and check if the email contains an HTML part.
      • Use $EmailDetails{rawEmailContent}$ to verify if the raw email data contains the forwarded section.
    • Check for Multipart Emails

      • Some bounced emails and forwarded emails have multiple MIME parts (text, HTML, or attachments).
      • Try $EmailDetails{emailMessageText}$ instead of $EmailDetails{emailMessageHtml}$.
    • Use Debugging to Identify Where Data is Lost

      • Before processing the email, add a log message to capture and print $EmailDetails{rawEmailContent}$.
      • If the forwarded section is present in the raw content but missing in the extracted message, the parsing method is stripping out certain parts.
    • Manually Extract the Forwarded Section

      • If $EmailDetails{rawEmailContent}$ contains the full email, you can extract the forwarded section using regex in Automation Anywhere.
      • Example regex pattern to extract the forwarded message:
         

        makefile

    ------------ Forwarded message ------------

    Subject: (.*?)

    From: (.*?)

    To: (.*?)

    • Use String: Extract Text or Python to extract key details from raw email content.
    Madhu Kumar T S || https://www.linkedin.com/in/madhukumarts/