Modifying the email content to include an attachment
In this recipe, you will learn how to send a file as an attachment to the registered user. In our previous recipe, we created a log file of the email content, which we will use as an email attachment for this instance. However, in real-world applications, you might not intend to send log files to the end user.
Note
At the time of writing, SendGrid recommends that the size of the attachment shouldn't exceed 10 MB, though technically, your email can be as large as 20 MB.
Getting ready
This is a continuation of the Implementing email logging in Azure Blob Storage recipe. If you are reading this first, make sure to go through the previous recipes of this chapter beforehand.
How to do it...
In this section, we will need to perform the following steps before moving to the next section:
- Make the changes to the code to create a log file with the RowKey of the table. We will achieve this using the IBinder interface. The IBinder interface helps us in customizing the name of the file.
- Send this file as an attachment to the email.
Customizing the log file name using the IBinder interface
Perform the following steps:
- Navigate to the run.csx file of the SendNotifications function.
- Remove the TextWriter object and replace it with the variable binder of the IBinder type. The following is the new signature of the Run method:
#r "SendGrid"
#r "Newtonsoft.Json"
#r "Microsoft.Azure.WebJobs.Extensions.Storage"
using System;
using SendGrid.Helpers.Mail;
using Newtonsoft.Json;
using Microsoft.Azure.WebJobs.Extensions.Storage;
public static void Run(string myQueueItem,
out SendGridMessage message,
IBinder binder,
ILogger log)
- Since you have removed the TextWriter object, the outputBlob.WriteLine(emailContent); function will no longer work. Let's replace it with the following piece of code:
using (var emailLogBloboutput = binder.Bind<TextWriter>(new BlobAttribute($"userregistrationemaillogs/
{ inputJson.RowKey}.log")))
{
emailLogBloboutput.WriteLine(emailContent);
}
- In the RegisterUser function, run a test using the same request payload that we used in the previous recipes.
- You can see the email log file that is created using the RowKey of the new record stored in Azure Table storage, as shown in Figure 2.21:
Figure 2.21: Email log file stored in Azure Table storage
Adding an attachment to the email
To add an attachment to the email, perform the following steps:
- Add the following code to the Run method of the SendNotifications function, and save the changes by clicking on the Save button:
message.AddAttachment(FirstName +"_"+LastName+".log", System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(emailContent)),
"text/plain",
"attachment",
"Logs"
);
- Run a test using the same request payload that we used in the previous recipes.
- Figure 2.22 shows the email, along with the attachment:
Figure 2.22: Displaying an email along with the attachment
Note
Learn more about the SendGrid API at https://sendgrid.com/docs/API_Reference/api_v3.html.
In this recipe, you have learned how to add an attachment to the email. Let's now move on to the next recipe.