'How to send an email in Sendgrid to a single recipient using dynamic template and obtain the sent HTML for retention purposes

I am currently working on integrating the sending of mail in sendgrid with our internal CRM system. The use case I am working with is to send a customized email to a single customer when a specific button is pressed within our CRM. I have created a dynamic template within sendgrid containing handlebars, which I would like to substitute with values from our CRM system (eg customers name). My intention is to make use of the sendgrid api endpoints when sending my email. Once sent, I need to obtain the HTML that was sent to the customer so that it can be retained within our database.

"Mail send" covers the majority of those requirements, but it is my understanding that mail send offers no response body (just a status code), so I am unable to obtain a copy of the HTML, which I would like to contain the values that were inserted in place of the handlebars.

I have noted you can query the email activity feed api and filter by subject/email to locate the sent message (which I tried out), but the response does not contain the HTML.

I have also looked into single send as an alternative which does offer a response body, but this only allows you to send to a segment or a list of contacts and seems to be more fit for purpose for an email going to a selection of people, as opposed to just one.

Can anyone suggest a way of me being able to achieve everything from the "mail send" option whilst also retaining the HTML of the email that was sent INCLUDING its handlebar substitutions?

I have provisionally been doing test api calls in postman so at this stage postman implementation would be fine. I will ultimately be using a c# ASP.Net web forms solution, querying sendgrid api endpoints. I also appreciate some configuration will likely be required via the sendgrid back office.

Successful postman request to send "mail send":

POST /v3/mail/send
Base url: https://api.sendgrid.com
{
  "personalizations": [
    {
      "to": [
        {
          "email": "[email protected]",
          "name": "API Testing"
        }
      ],
      "subject": "API testing successful",
      "dynamic_template_data": {
      "sellerName": "Mr Seller",
      "sellerEmail": "Test @ Test",
      "telNumber": "0123 456 789"
        }
    }
  ],
  "from": {
    "email": "[email protected]",
    "name": "API Testing"
  },
  "template_id" : "templateidfordesigntemplategoeshere"
}

Dummy example of snippet of html contained within dynamic template held in sendgrid back office:

...
<p>Best Regards,</p><br>
<p>{{sellerName}}</p>
<p>My Dummy Title</p>
<p>{{sellerEmail}}</p>
<p>01234 567 890</p>
<p>My Dummy Company</p>

Example of snippet of html I would like to retain in internal CRM system:

...
<p>Best Regards,</p><br>
<p>Mr Seller</p>
<p>My Dummy Title</p>
<p>Test @ Test</p>
<p>01234 567 890</p>
<p>My Dummy Company</p>


Solution 1:[1]

I found a solution to this and thought I would share in case another person faces the same issue. I discovered a nuget package for using Handlebars.js templates in a .NET application. [https://github.com/Handlebars-Net/Handlebars.Net][1] I first queried the designs sendgrid api endpoint to retrieve my template containing the handlebars as a response. I then used the Handlebars.net to pipe in the dynamic values for my crm, which produced the html I needed to retain. I finally performed a sendgrid mailsend via the api, using the html I had just populated and if the mail send result in a success error I retained the html within the database.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 brittdevvs