Skip to main content

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

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!


Reply