Email - Sending email attachments with the Office 365 Outlook connector

Sending email messages from Power Apps is a common requirement. However, the syntax to compose emails that includes file attachments can be difficult to understand. This post covers serveral examples of how to carry out this task, including:

  • How to attach files from a SharePoint record
  • How to attach files from a file attachment control
  • How to attach an image from the add picture control
  • how to attach annotations/signatures from the pen input control
  • how to attach audio recordings
  • how to attach multiple attachments to an email

This post focuses on how to send these attachment types using the Office 365 Outlook connector. My earlier post bneath provides an introduction to this connector.

Attaching files from a SharePoint record

Let's begin by examining how to attach files from a SharePoint record. We call the SendEmailV2 method to send an email. We can include attachments by passing a table of attachments. The structure of this table must include the column names "ContentBytes" and "Name". The ContentBytes column contains the binary content of the attachment, and the Name column specifies the file name of the attachment.

Let's suppose we take a SharePoint record with file attachments. If we add a card to a detail form, the syntax to add the file attachments to an email message looks like this:

Office365Outlook.SendEmailV2(
"[email protected]",
"Email Subject",
"Email Body",
{
Attachments: RenameColumns(
ThisItem.Attachments,
"Value",
"ContentBytes",
"DisplayName",
"Name"
),
Importance: "Normal"
}
)
With a SharePoint record, the Attachments field returns a table of attachments. This SharePoint table returns the binary content in a field called "Value", and the attachment name in a field called "DisplayName".

In the formula above, we call the RenameColumns function to rename these columns to the "ContentBytes" and "Name" titles that the SendEmailV2 method expects.

Attaching files that a user selects using a 'file picker'/attachments control

Let's suppose we want to build an email attachment feature that is independent or disconnected from data. A typical requirement is to build a screen that allows a user to choose a file from their local computer using an attachments control, and to attach the selected file(s) to an email.

The first challenge is that there's no way to add an attachments control to a screen. The insert menu in Power Apps does not include this control. Therefore, the workaround is to copy an attachments control from a form that we've pre-built, based on a SharePoint or Dataverse record.

When we paste this control onto a new screen, we'll find various errors that relate to references that are no longer valid. We can use the app checker to quickly correct these errors. For each error that exists, we can delete the offending formula to clear the error.


Once we clear all the errors, we can add a button and use the formula beneath to send an email that includes the files that the user selects through the attachments control. For this example, we've named the attachments control attFiles.

Office365Outlook.SendEmailV2(
"[email protected]",
"Email Subject",
"Email Body",
{
Attachments: RenameColumns(
attFiles.Attachments,
"Value",
"ContentBytes"
),
Importance: "Normal"
}
)

Attaching an image that a user selects using the 'add picture' control

A variation of the previous example is to build a screen that allows a user to choose an image from their local computer using an 'add picture' control, and to attach the selected image to an email.

To carry out this task, we add an 'add picture' control to a screen. We can then use the formula beneath to send an email that includes the image that the user selects.

Office365Outlook.SendEmailV2(
"[email protected]",
"Email Subject",
"Email Body",
{
Attachments: Table({
Name:"myImageName.jpg",
ContentBytes: UploadedImage1.Image
}
),
Importance: "Normal"
}
)

The add picture control is a composite control, meaning that it includes a child image control that displays the image that the user uploads. In this case, the name of this control is UploadedImage1. This is the control that we use to retrieve the uploaded image.


Attaching the contents of the 'pen input control' in an email

The pen input control allows users to make annotations or to draw images. We can send these annotations as an email image attachment, using a process that's very similar to the 'add picutre' control method.

Once we add a pen input control to a screen, we can use the formula beneath to send an email that includes the annotation.

Office365Outlook.SendEmailV2(
"[email protected]",
"Email Subject",
"Email Body",
{
Attachments: Table({
Name:"myImageName.jpg",
ContentBytes: PenInput1.Image
}
),
Importance: "Normal"
}
)
In this example, the name of the pen input control is PenInput1, and we can retrieve the annotation through the PenInput1.Image property.


Attaching an audio recording in an email

The microphone control allows users to make audio recordings. My previous post here describes this control, including how to save audio files to a data source.

It's possible to attach an audio recording as an email attachment. To do this, we add a microphone control to the screen, and use the formula beneath.

Office365Outlook.SendEmailV2(
"[email protected]",
"Email Subject",
"Email Body",
{
Attachments: Table({
Name:"myRecording.wav",
ContentBytes: Microphone1.Audio
}
),
Importance: "Normal"
}
)
In this example, the name of the microphone control is Microphone1, and we can retrieve the recording through the Microphone1.Audio property.



Attaching multiple files in an email

Finally, a frequent requirement is to add multiple attachments from different sources to an email. An easy way to do this is to add the multiple attachments to a collection, and to pass this collection to the SendEmailV2 method.

The formula beneath illustrates how to compose an email with files from an attachment control, and the annotation from a pen input control.

ClearCollect(colAttachments, 
RenameColumns(
attFiles.Attachments,
"Value",
"ContentBytes"
),
{
Name:"myImageName.jpg",
ContentBytes:penSignature.Image
}
);
Office365Outlook.SendEmailV2(
"[email protected]",
"Email Subject",
"Email Body",
{
Attachments: colAttachments,
Importance: "Normal"
}
)



The screenshot beneath shows how this type of setup looks like.


Conclusion

The syntax to compose emails with file attachments can be complex. This post described how to carry out this task, including how to attach files, images, annotations, audio recordings, and combinations of files to an email message.

Related posts

FormuIas - Is it possible to call a user-defined function recursively in Power Apps?
January 31, 2024
Formulas - A beginners guide on how to create and call user-defined functions (UDFs)
January 28, 2024
Formula - How to add a button that converts degrees Centigrade to Fahrenheit and vice versa
May 08, 2023
Formula - How to convert a single delimited string to rows and columns
April 05, 2023
Data - How to group data in a gallery and calculate sums
January 20, 2023
Formula - How to calculate compound interest
November 24, 2022
Utilities - The best way to peform OCR on images of Power Apps Formulas
October 11, 2022
Example - How to use a drop down control to convert currencies
September 27, 2022
Formula - How to parse JSON in Power Apps- 4 examples
September 15, 2022
Data - How to get a row by ordinal number
April 28, 2022
Formula - What to do when the If statement doesn't work?
December 17, 2021
Formula - Boolean And / Or operators - What is the order of precedence?
December 16, 2021
Controls - How to set the data source of a Combo Box to a comma separated string
November 16, 2021
Numbers - 10 examples of how to round numbers
August 18, 2021
Formula - Difference between round, square, and curly brackets
July 20, 2021
Top 3 highlights of upcoming enhancements to the Power Apps language (Power FX)
May 26, 2021
Formula - What to try when numbers don't format correctly
March 24, 2021
Controls - How to convert HTML to Text
March 23, 2021
Formulas - how to return all days between two dates
March 15, 2021
Formula - How to create comma separated (CSV) list of items
February 25, 2021
Formula - How to use the IF and Switch functions - 3 common examples
February 12, 2021
Location - Finding the closest location and and sorting records by distance, based on the current location of the user
January 24, 2021
Formulas - How to cope with weekends and public holidays in date calculations
January 21, 2021