Skip to main content
Solved

How to extract .pdf file which is coming inside a .eml file?

  • May 26, 2025
  • 3 replies
  • 281 views

Forum|alt.badge.img

I am currently receiving emails that contain .eml file (an outlook item). 

Inside the said .eml is a .pdf that I want to save.

Any who has done this before or knows how to do it please let me know

Best answer by madhu subbegowda

there are many ways to achieve this. you can give it a try with simple VBA code below:

Sub SavePDFFromEML()
    Dim ns As Outlook.Namespace
    Dim inbox As Outlook.Folder
    Dim mail As Outlook.MailItem
    Dim att As Attachment
    Dim subMail As MailItem
    Dim filePath As String

    Set ns = Application.GetNamespace("MAPI")
    Set inbox = ns.GetDefaultFolder(olFolderInbox)

    For Each mail In inbox.Items
        If mail.Attachments.Count > 0 Then
            For Each att In mail.Attachments
                If Right(att.FileName, 4) = ".eml" Then
                    ' Save the .eml file temporarily
                    filePath = Environ("USERPROFILE") & "\Desktop\" & att.FileName
                    att.SaveAsFile filePath

                    ' Open the .eml as MailItem
                    Set subMail = Application.CreateItemFromTemplate(filePath)

                    ' Now check for PDF inside this email
                    If subMail.Attachments.Count > 0 Then
                        For Each innerAtt In subMail.Attachments
                            If Right(innerAtt.FileName, 4) = ".pdf" Then
                                innerAtt.SaveAsFile Environ("USERPROFILE") & "\Desktop\" & innerAtt.FileName
                            End If
                        Next
                    End If
                End If
            Next
        End If
    Next
End Sub

 

(Or) use powershell script using using Mimekit or similar can parse .eml files and extract embedded attachments.

(Or) If you want to achieve using AA:

1. Read the .eml file
Use the File Read or Open File command to access the .eml file contents.

2. Parse the .eml file
Automation Anywhere doesn't natively parse .eml files, so you’ll need to:
Use Python Script inside your bot to extract attachments.
Or use third-party command-line tools like ripmime or 7-Zip to extract attachments from .eml.

import email
import os
from email import policy
from email.parser import BytesParser

# Read the .eml file
with open("path_to_file.eml", 'rb') as f:
msg = BytesParser(policy=policy.default).parse(f)

# Save attachments
for attachment in msg.iter_attachments():
filename = attachment.get_filename()
if filename.endswith('.pdf'):
with open(os.path.join("output_folder", filename), 'wb') as pdf_file:
pdf_file.write(attachment.get_payload(decode=True))

3. Call the Python script from Automation Anywhere
Use Run Script or Execute Python Script command.
Pass the .eml file path and output folder as arguments.

4. Continue with AA actions
Once the .pdf is saved, you can continue processing it (read content, extract data, move to SharePoint, etc.).

 

Hope this helps you!

3 replies

madhu subbegowda
Most Valuable Pathfinder
Forum|alt.badge.img+12
  • Most Valuable Pathfinder
  • Answer
  • May 26, 2025

there are many ways to achieve this. you can give it a try with simple VBA code below:

Sub SavePDFFromEML()
    Dim ns As Outlook.Namespace
    Dim inbox As Outlook.Folder
    Dim mail As Outlook.MailItem
    Dim att As Attachment
    Dim subMail As MailItem
    Dim filePath As String

    Set ns = Application.GetNamespace("MAPI")
    Set inbox = ns.GetDefaultFolder(olFolderInbox)

    For Each mail In inbox.Items
        If mail.Attachments.Count > 0 Then
            For Each att In mail.Attachments
                If Right(att.FileName, 4) = ".eml" Then
                    ' Save the .eml file temporarily
                    filePath = Environ("USERPROFILE") & "\Desktop\" & att.FileName
                    att.SaveAsFile filePath

                    ' Open the .eml as MailItem
                    Set subMail = Application.CreateItemFromTemplate(filePath)

                    ' Now check for PDF inside this email
                    If subMail.Attachments.Count > 0 Then
                        For Each innerAtt In subMail.Attachments
                            If Right(innerAtt.FileName, 4) = ".pdf" Then
                                innerAtt.SaveAsFile Environ("USERPROFILE") & "\Desktop\" & innerAtt.FileName
                            End If
                        Next
                    End If
                End If
            Next
        End If
    Next
End Sub

 

(Or) use powershell script using using Mimekit or similar can parse .eml files and extract embedded attachments.

(Or) If you want to achieve using AA:

1. Read the .eml file
Use the File Read or Open File command to access the .eml file contents.

2. Parse the .eml file
Automation Anywhere doesn't natively parse .eml files, so you’ll need to:
Use Python Script inside your bot to extract attachments.
Or use third-party command-line tools like ripmime or 7-Zip to extract attachments from .eml.

import email
import os
from email import policy
from email.parser import BytesParser

# Read the .eml file
with open("path_to_file.eml", 'rb') as f:
msg = BytesParser(policy=policy.default).parse(f)

# Save attachments
for attachment in msg.iter_attachments():
filename = attachment.get_filename()
if filename.endswith('.pdf'):
with open(os.path.join("output_folder", filename), 'wb') as pdf_file:
pdf_file.write(attachment.get_payload(decode=True))

3. Call the Python script from Automation Anywhere
Use Run Script or Execute Python Script command.
Pass the .eml file path and output folder as arguments.

4. Continue with AA actions
Once the .pdf is saved, you can continue processing it (read content, extract data, move to SharePoint, etc.).

 

Hope this helps you!


Forum|alt.badge.img

Opening the EML file in an email client such Thunderbird or Windows Live Mail is the most straightforward way to extract a PDF file from an email. After then it's simple to view and save attachments as PDFs. If you don't have an email client it becomes difficult because you would need to manually decode the.eml content which takes time. A more reliable option is to use a specialist tool like DataVare EML to PDF Converter which quickly extracts PDF attachments from EML files while maintaining the integrity of the data.


Forum|alt.badge.img
  • Cadet | Tier 2
  • January 15, 2026

If a PDF file is coming inside an .EML file, it means the email itself is attached as an Outlook item, and the PDF is inside that email. You can extract it in a few simple ways.

Method 1: Using an Email Client (Manual Way)
Open the main email first, then double-click the attached .eml file. This will open the email in your default email client (like Outlook or Windows Mail). Once the .eml file opens, you should see the PDF attachment inside it. Right-click the PDF and choose Save As to store it on your system. This works well if you have only a few files.

Method 2: When You Have Many EML Files
If you are receiving many such emails and manual extraction is slow, you can use a tool like Advik EML to PDF Converter. It allows you to load EML files and extract attachments (including PDFs) directly without opening each email one by one. This is useful when handling bulk emails or archived EML files.

In short, for occasional emails, opening the EML file manually is enough. But if this is a repeated task or involves many files, using a dedicated EML handling tool can save time and effort.