sendgrid-csharp
sendgrid-csharp copied to clipboard
Document how to obtain htmlContent or PlainText of what was sent / will be sent when using dynamic templates
Issue Summary (Question)
I love SendGrid but I am really hitting a brick wall here. I imagine it's a common scenario in applications to want to show an audit trail / email correspondence history once an email is sent via SendGrid. I use dynamic templates to send emails and I am finding that I have one of two options:
- Use the API to obtain htmlcontent of the dynamic template and replace variables manually - this will have limitations as each template will have different variables.
- Use Inbound Parse and try and figure out where the email belongs in a multitenant distributed application and then store it from there. One issue with this is the URL for the API Post endpoint will need to be accessible on the Web so it will not be possible to test this on local host.
Both options feel very long winded, is there an alternative simpler approach? For example...
- Query an API endpoint with x-message-id to obtain copy of the email body.
- SendEmailAsync() response to contain htmlContent and PlainText
- TemplateEngine Endpoint where we can post variables and template Id and it will generate htmlContext and plaintext back?
I really welcome your thoughts and suggestions
Steps to Reproduce
- SendEmailAsync() then try and access the content of what was sent.
Exception/Log
None
I ended up using reflection and replaced the template variable in the application. Code looks something like this if anyone wants to use in the future / contribute towards a future method in the library.
msgData is an anonymous object with data that is passed to the Template. You can obtain html_content and plain_content from client.RequestAsync(method: SendGridClient.Method.GET, urlPath: "templates/" + emailTemplateId)
Type customType = msgData.GetType();
PropertyInfo[] properties = customType.GetProperties();
foreach(PropertyInfo property in properties)
{
hTMLContent = hTMLContent.Replace("{{" + property.Name + "}}", msgData.GetType().GetProperty(property.Name).GetValue(msgData, null).ToString());
plainContent = plainContent.Replace("{{" + property.Name + "}}", msgData.GetType().GetProperty(property.Name).GetValue(msgData, null).ToString());
subject = subject.Replace("{{" + property.Name + "}}", msgData.GetType().GetProperty(property.Name).GetValue(msgData, null).ToString());
}
Hello @hbermani,
Thanks for publishing your solution!
This issue has been added to our internal backlog to be prioritized. Pull requests and +1s on the issue summary will help it move up the backlog.
With best regards,
Elmer
Internal tracking for creating an API endpoint: MP-6255