Skip to main content

Notifications

How to Save Embedded Images in Email Using Power Automate Desktop

How to Save Embedded Images in Email Using Power Automate Desktop

Introduction:

Saving embedded images from emails is crucial for many business processes. However, doing this in Power Automate Desktop can be tricky, especially if the images are embedded rather than attached separately then you may need to handle the HTML content of the email.
  • Add the "Extract HTML element attribute" action to get the src attribute of the image.
  • Locate the cid (Content ID) of the embedded images within the email body.
  • Convert the cid into an image by extracting the base64 content or downloading it using external scripts or APIs if necessary. provides a seamless way to automate this task. In this blog post, we’ll walk you through the steps to extract and save embedded images from an email.
The problem here is if the src attribute of the image is not embedded or not found we can go with this process. So, in order to avoid this, I have generated a simple solution.
This blog post will guide you through the steps to extract and save embedded images from an email using Power Automate Desktop.


Prerequisites
  • Power Automate Desktop installed
  • Outlook App and access to your email account

Step-by-Step Guide


Step 1: Create a New Flow
  • Open Power Automate Desktop.
  • Click on New Flow and name it “Save Embedded Images”.
Step 2: Connect to Your Email
  • Launch Outlook.
  • Configure the action to connect to your email account and specify the folder (e.g., Inbox) where the emails are located.
  • Retrieve the email messages based on the sender’s email ID and unread status as shown in below image.

Step 3: Loop Through Retrieved Emails and Save Emails as .msg Files
  • Add a For Each loop to iterate through the emails.
  • Inside the loop, use the Save Outlook Email Messages action and configure it accordingly as shown in below image.


Step 4: Convert .msg Email Files into PDF Files
  • Get files from the folder containing .msg files.
  • Add a For Each loop to iterate through the saved .msg email files.
  • Use the PowerShell Script action to convert .msg files into PDF files as shown in below image.


PowerShell Script:
# This code has been generated by AI. Original prompt:
# Powershell script to convert .msg saved email to pdf
# 
Add-Type -AssemblyName Microsoft.Office.Interop.Outlook
Add-Type -AssemblyName System.Windows.Forms

function Convert-MsgToPdf {
    param (
        [string]$msgFilePath,
        [string]$pdfFilePath
    )

    $outlook = New-Object -ComObject Outlook.Application
    $namespace = $outlook.GetNamespace("MAPI")
    $mailItem = $namespace.OpenSharedItem($msgFilePath)

    $wordApp = New-Object -ComObject Word.Application
    $wordApp.Visible = $false

    $tempHtmlPath = [System.IO.Path]::ChangeExtension($msgFilePath, ".html")
    $mailItem.SaveAs($tempHtmlPath, [Microsoft.Office.Interop.Outlook.OlSaveAsType]::olHTML)

    $document = $wordApp.Documents.Open($tempHtmlPath)
    $document.SaveAs([ref] $pdfFilePath, [ref] 17)  # 17 is the wdFormatPDF enumeration

    $document.Close([ref] $false)
    $wordApp.Quit()

    Remove-Item $tempHtmlPath

    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($document) | Out-Null
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($wordApp) | Out-Null
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($mailItem) | Out-Null
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($namespace) | Out-Null
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($outlook) | Out-Null
}

# Example usage
$msgFilePath = "%CurrentItem.FullName%"
$pdfFilePath = "C:\Users\Desktop\Power Automate Desktop\Practice\HTML\Email Messages as .pdf\Email_%Count%.pdf"
Convert-MsgToPdf -msgFilePath $msgFilePath -pdfFilePath $pdfFilePath


Step 5: Extract Embedded Images and Save Them
  • Get files from the folder containing the converted PDF files.
  • Add a For Each loop to iterate through the converted PDF files.
  • Use the Extract Images from PDF action as shown in below image.


Flow Image

Here’s what the flow looks like:




Conclusion
By following these steps, you can easily automate the process of saving embedded images from emails using Power Automate Desktop. This can save you a lot of time and ensure that important images are always stored securely.

Feel free to customize this guide to better fit your needs!

Comments