How To Send an Email with Attachments Using EPiServer Forms
Table of contents
In case the File upload element is specified an EPiServer form will store all uploaded files in the EPiServer blob storage by default. The form can be configured to automatically send out an email to any specified email address with all the data that is entered in the form. However, the uploaded files are not sent as attachments but as links. If we want to change that, we would have to extend the existing functionality.
Custom Actors in EPiServer Forms
We need to create a custom Actor. By definition, Actors are server-side actions that are performed after the user’s form submission. Episerver forms comes with two pre-defined Actors: CallWebhookAfterSubmissionActor and SendEmailAfterSubmissionActor.
If we want to send uploaded files as attachments, we need to extend SendEmailAfterSubmissionActor. We can do this by creating a custom actor that will inherit SendEmailAfterSubmissionActor:
After that, we need to create a model for the Uploaded file. This model will have 3 properties: Name, Type and Input stream.
Next, we need to implement a method for collecting uploaded files in our actor.
At this moment, the files are not stored in the EPiServer blob storage, because the file uploading might take a while. For that reason, we use HttpRequestContext. In this way, we will get files from the request, instead of waiting for them to be stored in the blob storage. Without that, it will send an email with incomplete attachments.
Now, we can override the Run method in our custom actor, and call the GetUploadedFiles() method from there.
Finally, we can create a method for sending an email with attachments. This method will be called from the SendMessage method. In this method, we will get all the uploaded files, and create attachments based on those files. When all the attachments are created, we need to include those attachments in an email message, and finally, send that message.
Latest blog posts
Intent classification: understanding text with the powe...
In today’s world, with the expansion of data generated from various sources, analyzing it has become a critical challenge for businesses. Read more about how intent classification of textual data works and how it can lead t...
What Is Stable Diffusion and How Does It Work?
For the past few years, revolutionary models in the field of AI image generators have appeared. Stable diffusion is a text-to-image model of Deep Learning published in 2022. Find out the reasons why Stable diffusion gained ...