Converting Emails and Attachments to PDF in Power Automate

April 8th 2020
Back to all blogs

The Encodian ‘Convert to PDF‘ action supports 70+ standard file formats, including ‘MSG’, an email message. This post outlines how to convert an email message received into an Office 365 Outlook mailbox into PDF format and how to convert an email message and contained attachments into a single PDF document.

UPDATE AUG-20: Whilst this approach is still valid, our new ‘Convert Mail Message‘ action provides a much simpler solution for converting email messages to PDF. Check out this post for further details: V2: Convert Emails and Attachments to PDF in Power Automate

Convert Email (MSG) to PDF

1. Launch Power Automate

2. Create a new ‘Automated flow.

3. Provide the following information

3.a. Flow name: Enter a relevant name/description for the Flow

3.b. Select the Office 365 Outlook ‘When a new email arrives‘ trigger action

3. c Click ‘Create.

4. Configure the Office 365 Outlook ‘When a new email arrives‘ trigger action to ensure your Flow is only triggered when required.

5. Add the ‘Export email‘ Office 365 Outlook action

5.a. Message Id: Select the ‘Message Id‘ property from the Office 365 Outlook ‘When a new email arrives‘ trigger action

6. Add the Encodian ‘Convert to PDF‘ action

6a. Filename: Enter a nominal value ensuring the msg file extension is added

6. b. File Content: Select the ‘Body’ property from the ‘Export email‘ Office 365 Outlook action

6.c. PDF Filename: Enter the name for the PDF file to be created

At this point, your email message has been converted to PDF. You simply need to add actions to your flow to do something with the PDF document; for this example, we’ll add the PDF document to a SharePoint document library.

7. Add the SharePoint ‘Create File‘ action

7.a. Site Address: Enter the location of the target SharePoint site

7. b. Folder Path: Select the target SharePoint library/folder

7.c. File Name: Select the ‘Filename‘ property from the ‘Convert to PDF‘ action

7.d. File Content: Select the ‘File Content‘ property from the ‘Convert to PDF‘ action

You can now test your Flow, noting you’ll need to ensure you send an email about the configuration you applied to the trigger action. Your email should now be converted to PDF format:

Convert Email (MSG) and Attachments to PDF

Next, we will convert the email message and any attachments to a single PDF Document.

1. Repeat ‘Convert Email (MSG) to PDF‘ steps 1 to 6

2. Add an ‘Initialize Variable‘ action

2.a. Name: Set to ‘FilesForMerge

2.b. Type: Set to ‘Array

The array variable will be used to hold the attachments (Base 64 format), which we’ll later convert and merge into a single PDF document.

3. Add an ‘Append to array variable’ action

3.a. Name: Select ‘FilesForMerge

3.b. Value: Copy the following data into the action

{
    "fileName": ,
    "fileContent": {
        "$content-type": "application/octet-stream",
        "$content":
    }
}

3.c. Place your cursor as per the arrow and select ‘Filename‘ from the Encodian ‘Convert to PDF‘ action

3.d. Place your cursor as per the arrow and select ‘File Content‘ from the Encodian ‘Convert to PDF‘ action

4. Add an ‘Apply to each‘ control action

4. a Select an output from previous steps: Insert the ‘Attachments‘ parameter from the ‘When a new email arrives’ trigger action

The following steps will append each attachment to the FilesForMerge variable. Images are commonplace to be embedded within emails (email signatures, for example). These embedded images are also included as attachments; therefore, the following action can be used to filter these embedded images from being treated as actual attachments.

5. Add a ‘Condition‘ action

5.a. Choose a Value: Select the ‘Attachments Name‘ property from the Office 365 Outlook ‘When a new email arrives‘ trigger action

5. b. Operator: Select ‘Starts with

5. c. Choose a Value: Enter “image0”

All embedded pictures are exposed in the attachments with an automated filename beginning ‘image0’. For example, the first image could be ‘image001.png’.

The following actions will all be added within the ‘If no’ branch:

6. Add an ‘Append to array variable‘ action

6.a. Name: Select ‘FilesForMerge

6. b. Value: Copy the following data into the action

{
    "fileName": ,
    "fileContent": {
        "$content-type": "application/octet-stream",
        "$content":
    }
}

6. c Place your cursor as per the arrow and select ‘Attachments Name‘ from the ‘When a new email arrives’ trigger action

6.d. Place your cursor as per the arrow and select ‘Attachments Content‘ from the ‘When a new email arrives’ trigger action

7. Underneath the ‘Apply to each‘ control action, add a ‘Merge document array to PDF‘ action

7.a. PDF Filename: Enter the name for the PDF file to be created

7. b. Click the ‘Switch to input entire array‘ icon

Convert Attachment to PDF

7.c Documents: Select the ‘FilesForMerge‘ variable

Convert Attachment to PDF

At this point, your email message and its attachments will have been converted to PDF and merged into a single PDF document. You must add actions to your flow to do something with the PDF document (As before). We’ll add the PDF document to a SharePoint document library for this example.

8. Add the SharePoint ‘Create File‘ action

8.a. Site Address: Enter the location of the target SharePoint site

8.b. Folder Path: Select the target SharePoint library/folder

8. c. File Name: Select the ‘Filename‘ property from the the ‘Merge document array to PDF‘ action

8.d. File Content: Select the ‘File Content‘ property from the ‘Merge document array to PDF‘ action

Convert Attachment to PDF

You can now test your Flow, noting you’ll need to ensure you send an email about the configuration you applied to the trigger action. Your email should now be converted to PDF format:

I sent the following test email:

Convert Attachment to PDF

And the following 9-page document was created:

Convert Attachment to PDF

Troubleshooting:

Unfortunately, there seems to be a lot of variance between the ‘When a new email arrives trigger‘ actions versions relating to how the attachments are accessed, so please follow these steps should you have any issues:

Ensure that the ‘Include attachments‘ property on the ‘When a new email arrives‘ trigger action is set to ‘Yes‘, and make sure to send a new email when testing rather than using previous test data (which would still not contain the attachments if the setting has been updated).

Convert Attachment to PDF

Please also obtain the following information:

1. The version of the ‘When a new email arrives‘ trigger action, this post is using ‘V3.

2. The value of the input sent to the Encodian ‘Merge document array to PDF‘ action from the run history

Convert Attachment to PDF

Then please raise a support ticket via our support portal, and our team will advise which modifications you need to apply to your Flow.

Finally

We hope you’ve found this guide useful, and as ever, please share any feedback or comments – all are welcome!

You can find further documentation and guidance on the Encodian support portal: Merge document array to PDF.

Author
Jay Goodison

Managing Director

Back to all blogs

You might also be interested in...