Skip to main content
Question

How to get the forwarded email section from an email message


Forum|alt.badge.img+1

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
Forum|alt.badge.img+11
  • Pathfinder Community Team
  • 59 replies
  • 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 


Marc Mueller
Pathfinder Advocate | Tier 6
Forum|alt.badge.img+14
  • Pathfinder Advocate | Tier 6
  • 167 replies
  • 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


Forum|alt.badge.img+1
  • Author
  • Cadet | Tier 2
  • 4 replies
  • March 15, 2025

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

 


Marc Mueller
Pathfinder Advocate | Tier 6
Forum|alt.badge.img+14
  • Pathfinder Advocate | Tier 6
  • 167 replies
  • March 15, 2025

@Pamodi 

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

 

Cheers

Marc


Forum|alt.badge.img+1
  • Author
  • Cadet | Tier 2
  • 4 replies
  • March 15, 2025

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


madhu subbegowda
Most Valuable Pathfinder
Forum|alt.badge.img+8

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.

Shreya.Kumar
Pathfinder Community Team
Forum|alt.badge.img+9
  • Pathfinder Community Team
  • 95 replies
  • March 17, 2025

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


Forum|alt.badge.img+1
  • Author
  • Cadet | Tier 2
  • 4 replies
  • 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
Forum|alt.badge.img+8

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 subbegowda
Most Valuable Pathfinder
Forum|alt.badge.img+8

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.

Forum|alt.badge.img+8
  • Navigator | Tier 3
  • 58 replies
  • March 17, 2025
Pamodi wrote:

Hi ​@madhu subbegowda ,

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

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

 

 

There is no such key or any other key mentioned in above posts, refer documentation. You can check available keys in debug mode.


Forum|alt.badge.img+1
  • Author
  • Cadet | Tier 2
  • 4 replies
  • March 17, 2025

@madhu subbegowda 

Got the same error for $EmailDetails{rawEmailContent}$ and $EmailDetails{emailMessageText}$

Field content must not be null

The python script does not retrieve the forwarded email


madhu subbegowda
Most Valuable Pathfinder
Forum|alt.badge.img+8

Hello Pramodi,

This seems to be a common issue since some email clients format forwarded content in ways that might not be readily accessible through standard email reading actions.
Is it possible to try these approaches to solve this:

 

Use Get property action: Instead of directly accessing the emailMessage property, try:
    $EmailDetails{emailAttachments}$
    $EmailDetails{emailHTML}$
The HTML version of the email often contains the complete content including forwarded sections.

 

Try different email reading modes:
    In the "Read email" action, try changing the "Read email mode" option to "HTML" instead of "Plain text"
    This preserves more of the original formatting and content

 

Use Regular Expressions:
    After reading the email (using HTML format), use a regex pattern action to extract content between forwarding markers
    Example pattern: (?s)(?i)------+ ?Forwarded message ?------+(.*?)(?=------+|$)

 

Check email headers:
    Sometimes the forwarded content may be stored in specific headers
    Try accessing $EmailDetails{emailHeaders}$ to see if that contains the information

 

Save email as .eml and parse:
    Save the entire email as an .eml file
    Then use String actions to parse the file content, which often preserves the forwarded sections

 

Which email service are you connecting to? Some services like Gmail might require specific approaches for accessing the complete forwarded content.


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings