Emailing files from an Add Picture control directly from Power Apps
Have you ever wanted to upload files in Power Apps using the Add Picture control and attach these to an email without using a Flow? It is actually not all that complex once the structure of the data Outlook expects is understood.
Emailing uploaded file (single)
Firstly, starting with emailing the item uploaded - put an Add Picture control on the screen - I will call the Image Control in it imgAttachEmail and the Add Media Button ambFileAttach here for reference.
Now down to business - the code on your send button/icon simply needs to grab the file content and name of the file uploaded and give them specific field names.
With(
{
wFile:
Table(
{
Name: ambFileAttach.FileName,
ContentBytes: imgAttachEmail.Image
}
)
},
Office365Outlook.SendEmailV2(
"targetname@domain.com",
"YourFiles",
"Files Attached",
{
Attachments:
AddColumns(
wFile,
"@odata.type",
""
)
}
)
)
and that is the extent of it. Your uploaded file will be emailed as an attachment.
Emailing uploaded files
You now want to upload a number of files and send them all on an email - no problems, you just need to store them in a Collection to review before attaching to the email. Using the same principal as above, put this on the OnChange of the button ambFileAttach
Collect(
colAttach,
{
ContentBytes:imAttachEmail.Image,
Name:Self.FileName
}
)
and the resulting email
Office365Outlook.SendEmailV2(
"target@domain.com",
"YourFiles",
"Files Attached",
{
Attachments:
AddColumns(
colAttach,
"@odata.type",
""
)
}
)
In this solution, you also would want to look at these files as they are uploaded - put in a Gallery with the Items
colAttach
and inside an Image Control with the Image
ThisItem.ContentBytes
and a Label with the Text
ThisItem.Name
and you will see the files as they are uploaded.
Including selected attachments
Taking this a step further, what if you also wanted to email List attachments (or selected attachments) in the same email ? Firstly, please refer to this previous post for the process of emailing selected attachments as some of the code is better explained there.
This process requires a bit more manipulating of the data - the first bit is in my previous post, but now we need to add the content of the uploaded file. For this, we need a Collection to join the two elements into a common format so we do this (for one uploaded file below)
ClearCollect(
colImages,
AddColumns(
RenameColumns(
Filter(
galAttach.AllItems,
ckChoose.Value
),
"Value",
"ContentBytes"
),
"@odata.type",
""
),
{
Name: ambFileAttach.FileName,
ContentBytes: imgAttachEmail.Image
}
);
Office365Outlook.SendEmailV2(
"target@domain.com",
"YourFiles",
"Files Attached",
{Attachments: colImages}
)
This makes a Collection including all the content and file names from both the selected Attachments and the uploaded file and then attaches this to the email.
For all uploaded files - replace
{
Name: ambFileAttach.FileName,
ContentBytes: imgAttachEmail.Image
}
with
colAttach
Including all attachments
If you simply want all attachments and also include the upoaded file, you would do this
ClearCollect(
colImages,
AddColumns(
RenameColumns(
AttachControlName.Attachments,
"Value",
"ContentBytes"
),
"@odata.type",
""
),
{
Name: ambFileAttach.FileName,
ContentBytes: imgAttachEmail.Image
}
);
Office365Outlook.SendEmailV2(
"target@domain.com",
"YourFiles",
"Files Attached",
{Attachments: colImages}
)
and the same for all uploads as above.
*This post is locked for comments