skip to Main Content

I have an e-commerce web application consisting of Blazor WASM for the client side, Blazor Server for administration, and Web API for integration with the database and services. After order completion, I am sending an email to the customer.
I am using ASP.NET Core Identity IEmailSender SendEmailAsync

There is no problem sending emails but I need a more professional email body. So I decided to use a template with some changes. Here is the sample template (Email Template)

There are 2 subjects I need to accomplish. Generating a similar look with CSS and how to deal with dynamic content in this template. If there are more than 1 order I have to somehow add them to the template.

Here is how I try to generate the template with CSS (not complete just a portion for simplicity);

var bodyBuilder = new BodyBuilder
{
    HtmlBody = @"
<html>
     <head>
        <style>
             body {
                min-height: 100vh;
                background-size: cover;
                font-family: 'Lato', sans-serif;
                color: rgba(116, 116, 116, 0.667);
                background: linear-gradient(140deg , #fff , 50% , #BA68C8);
            }
...
    </style>
</head>
<body>
    <div class='container-fluid my-5  d-flex  justify-content-center'>        <div class='card card-1'>            <div class='card-header bg-white'>                <div class='media flex-sm-row flex-column-reverse justify-content-between  '>                    <div class='col my-auto'> <h4 class='mb-0'>Thanks for your Order,<span class='change-color'>@orderHeaderDTO.Name</span> !</h4> </div>                    <div class='col-auto text-center  my-auto pl-0 pt-sm-4'> <img class='img-fluid my-auto align-items-center mb-0 pt-3'  src='https://i.imgur.com/7q7gIzR.png' width='115' height='115'> <p class='mb-4 pt-0 Glasses'>Walk In Style</p>  </div>                </div>            </div>            <div class='card-body'>                <div class='row justify-content-between mb-3'>
...
// Send the email
await _emailSender.SendEmailAsync(orderHeaderDTO.Email, "Order Confirmation", bodyBuilder.HtmlBody);

After sending the mail, I checked it but it did not look like the sample template. How can I make a similar template and how can I dynamically add orders to this template?

2

Answers


  1. Chosen as BEST ANSWER

    Here is how I solved my problem, hope it helps others as well.

    Created a sample HTML page:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Email Template for Order Confirmation Email</title>
    
        <!-- Start Common CSS -->
        <style type="text/css">
        #outlook a {{
                padding: 0;
            }}
    
            body {{
                width: 100% !important;
                -webkit-text-size-adjust: 100%;
                -ms-text-size-adjust: 100%;
                margin: 0;
                padding: 0;
                font-family: Helvetica, arial, sans-serif;
            }}
    
            .ExternalClass {{
                width: 100%;
            }}
    
                .ExternalClass, .ExternalClass p, .ExternalClass span, .ExternalClass font, .ExternalClass td, .ExternalClass div {{
                    line-height: 100%;
                }}
    
            .backgroundTable {{
                margin: 0;
                padding: 0;
                width: 100% !important;
                line-height: 100% !important;
            }}
    
            .main-temp table {{
                border-collapse: collapse;
                mso-table-lspace: 0pt;
                mso-table-rspace: 0pt;
                font-family: Helvetica, arial, sans-serif;
            }}
    
                .main-temp table td {{
                    border-collapse: collapse;
                }}
        </style>
        <!-- End Common CSS -->
    </head>
    <body>
        <table width="100%" cellpadding="0" cellspacing="0" border="0" class="backgroundTable main-temp" style="background-color: #d5d5d5;">
            <tbody>
                <tr>
                    <td>
                        <table width="650" align="center" cellpadding="15" cellspacing="0" border="0" class="devicewidth" style="background-color: #ffffff;">
                            <tbody>
                                <!-- Start header Section -->
                                <tr>
                                    <td style="padding-top: 30px;">
                                        <table width="560" align="center" cellpadding="0" cellspacing="0" border="0" class="devicewidthinner" style="border-bottom: 1px solid #eeeeee; text-align: center;">
                                            <tbody>
                                                <tr>
                                                    <td style="padding-bottom: 10px;">
                                                        <a href="http://localhost:7169/"><img src="images/logo.png" alt="Walk In Style" /></a>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td style="font-size: 14px; line-height: 18px; color: #666666;">
                                                        3828 Mall Road
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td style="font-size: 14px; line-height: 18px; color: #666666;">
                                                        123 Consectetur at ligula 10660
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td style="font-size: 14px; line-height: 18px; color: #666666;">
                                                        Phone: 010-020-0340 | Email:  [email protected]
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td style="font-size: 14px; line-height: 18px; color: #666666; padding-bottom: 25px;">
                                                        <strong>Order Number:</strong> --OrderId-- | <strong>Order Date:</strong> --OrderDate--
                                                    </td>
                                                </tr>
                                            </tbody>
                                        </table>
                                    </td>
                                </tr>
                                <!-- End header Section -->
                                <!-- Start address Section -->
                                <tr>
                                    <td style="padding-top: 0;">
                                        <table width="560" align="center" cellpadding="0" cellspacing="0" border="0" class="devicewidthinner" style="border-bottom: 1px solid #bbbbbb;">
                                            <tbody>
                                                <tr>
                                                    <td style="width: 55%; font-size: 16px; font-weight: bold; color: #666666; padding-bottom: 5px;">
                                                        Delivery Adderss
                                                    </td>
                                                    <td style="width: 45%; font-size: 16px; font-weight: bold; color: #666666; padding-bottom: 5px;">
                                                        Billing Address
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td style="width: 55%; font-size: 14px; line-height: 18px; color: #666666;">
                                                        --Name--
                                                    </td>
                                                    <td style="width: 45%; font-size: 14px; line-height: 18px; color: #666666;">
                                                        --Name--
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td style="width: 55%; font-size: 14px; line-height: 18px; color: #666666;">
                                                        --Address--
                                                    </td>
                                                    <td style="width: 45%; font-size: 14px; line-height: 18px; color: #666666;">
                                                        --Address--
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td style="width: 55%; font-size: 14px; line-height: 18px; color: #666666; padding-bottom: 10px;">
                                                        --City--
                                                    </td>
                                                    <td style="width: 45%; font-size: 14px; line-height: 18px; color: #666666; padding-bottom: 10px;">
                                                        --City--
                                                    </td>
                                                </tr>
                                            </tbody>
                                        </table>
                                    </td>
                                </tr>
                                <!-- End address Section -->
                                <!-- Start product Section -->
                                <!-- Start calculation Section -->
                                <tr>
                                    <td style="padding-top: 0;">
                                        <table width="560" align="center" cellpadding="0" cellspacing="0" border="0" class="devicewidthinner" style="border-bottom: 1px solid #bbbbbb; margin-top: -5px;">
                                            <tbody>
                                                <tr>
                                                    <td rowspan="5" style="width: 55%;"></td>
                                                    <td style="font-size: 14px; line-height: 18px; color: #666666;">
                                                        Sub-Total:
                                                    </td>
                                                    <td style="font-size: 14px; line-height: 18px; color: #666666; width: 130px; text-align: right;">
                                                        --SubTotal--
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td style="font-size: 14px; line-height: 18px; color: #666666; padding-bottom: 10px; border-bottom: 1px solid #eeeeee;">
                                                        Shipping Fee:
                                                    </td>
                                                    <td style="font-size: 14px; line-height: 18px; color: #666666; padding-bottom: 10px; border-bottom: 1px solid #eeeeee; text-align: right;">
                                                        $5.00
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td style="font-size: 14px; font-weight: bold; line-height: 18px; color: #666666; padding-top: 10px;">
                                                        Order Total
                                                    </td>
                                                    <td style="font-size: 14px; font-weight: bold; line-height: 18px; color: #666666; padding-top: 10px; text-align: right;">
                                                        --Total--
                                                    </td>
                                                </tr>
                                            </tbody>
                                        </table>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </td>
                </tr>
            </tbody>
        </table>
    </body>
    </html>
    

    Here is my method to fill dynamic values

    private async Task SendOrderConfirmationEmail(OrderHeaderDTO orderHeaderDTO)
    {
        try
        {
            // Get the order for details in the template
            var orders = await _orderRepository.GetOrdersByUser(orderHeaderDTO.UserId);
            var order = orders.Where(o => o.OrderHeader.Id == orderHeaderDTO.Id).FirstOrDefault();
    
            // Construct the path to the file
            var pathToFile = Path.Combine("Templates", "EmailTemplate", "Confirmation_EmailTemplate.html");
    
            // Check if the file exists
            if (!System.IO.File.Exists(pathToFile))
            {
                throw new FileNotFoundException("The email template file was not found.", pathToFile);
            }
            var builder = new BodyBuilder();
            using (var SourceReader = System.IO.File.OpenText(pathToFile))
            {
                builder.HtmlBody = SourceReader.ReadToEnd();
            }
    
            var tableRows = new StringBuilder();
            foreach (var detail in order.OrderDetails)
            {
                tableRows.Append($@"
                <tr>
                    <td style='padding-top: 0;'>
                        <table width='560' align='center' cellpadding='0' cellspacing='0' border='0' class='devicewidthinner' style='border-bottom: 1px solid #eeeeee;'>
                            <tbody>
                                <tr>
                                    <td rowspan='4' style='padding-right: 10px; padding-bottom: 10px;'>
                                        <img style='height: 80px;' src='{detail.Product?.ImageUrl}' alt='Product Image' />
                                    </td>
                                    <td colspan='2' style='font-size: 14px; font-weight: bold; color: #666666; padding-bottom: 5px;'>
                                        {detail.ProductName}
                                    </td>
                                </tr>
                                <tr>
                                    <td style='font-size: 14px; line-height: 18px; color: #757575; width: 440px;'>
                                        Quantity: {detail.Count}
                                    </td>
                                    <td style='width: 130px;'></td>
                                </tr>
                                <tr>
                                    <td style='font-size: 14px; line-height: 18px; color: #757575;'>
                                        Color: {detail.Product?.Color}
                                    </td>
                                    <td style='font-size: 14px; line-height: 18px; color: #757575; text-align: right;'>
                                        {detail.Price.ToString("C", CultureInfo.InvariantCulture)} Per Unit
                                    </td>
                                </tr>
                                <tr>
                                    <td style='font-size: 14px; line-height: 18px; color: #757575; padding-bottom: 10px;'>
                                        Size: {detail.Size}
                                    </td>
                                    <td style='font-size: 14px; line-height: 18px; color: #757575; text-align: right; padding-bottom: 10px;'>
                                        <b style='color: #666666;'>{(detail.Price * detail.Count).ToString("C", CultureInfo.InvariantCulture)}</b> Total
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </td>
                </tr>");
            }
    
            // Replace the placeholder in the HTML template with the generated tableRows
            builder.HtmlBody = builder.HtmlBody.Replace("<!-- Start product Section -->", tableRows.ToString());
            builder.HtmlBody = builder.HtmlBody.Replace("--OrderId--", orderHeaderDTO.Id.ToString());
            builder.HtmlBody = builder.HtmlBody.Replace("--OrderDate--", orderHeaderDTO.OrderDate.ToString());
            builder.HtmlBody = builder.HtmlBody.Replace("--Name--", orderHeaderDTO.Name);
            builder.HtmlBody = builder.HtmlBody.Replace("--Address--", orderHeaderDTO.StreetAddress);
            builder.HtmlBody = builder.HtmlBody.Replace("--City--", orderHeaderDTO.City);
            builder.HtmlBody = builder.HtmlBody.Replace("--SubTotal--", orderHeaderDTO.OrderTotal.ToString("C", CultureInfo.InvariantCulture));
            builder.HtmlBody = builder.HtmlBody.Replace("--Total--", (orderHeaderDTO.OrderTotal
                + 5).ToString("C", CultureInfo.InvariantCulture));
    
            // Send the email
            await _emailSender.SendEmailAsync(orderHeaderDTO.Email, "Order Confirmation", builder.HtmlBody);
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
    }
    

    Here is the email: email screenshot

    I just need to figure out how to display the product image on the email. Hope someone can guide me.


  2. The template that you provided also needs the links to bootstrap and jquery to work. I can not see there references in your sample code.

    Nevertheless, it is not a good idea to include external stylesheets or scripts in an email because they will most likely be blocked by the receiving email client.

    I recommend choosing a plain html template with inline css styles, which has the best compatibility, see https://www.smashingmagazine.com/2017/01/introduction-building-sending-html-email-for-web-developers/#inline-css

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search