Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document how to obtain htmlContent or PlainText of what was sent / will be sent when using dynamic templates #1025

Open
hbermani opened this issue Jul 14, 2020 · 3 comments
Labels
status: help wanted requesting help from the community type: community enhancement feature request not on Twilio's roadmap type: docs update documentation change not affecting the code

Comments

@hbermani
Copy link

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

  1. SendEmailAsync() then try and access the content of what was sent.

Exception/Log

None
@hbermani
Copy link
Author

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());  
            }

@thinkingserious
Copy link
Contributor

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

@thinkingserious thinkingserious added status: help wanted requesting help from the community type: community enhancement feature request not on Twilio's roadmap labels Jul 14, 2020
@thinkingserious thinkingserious changed the title Question: How to obtain htmlContent or PlainText of what was sent / will be sent when using dynamic templates? Document how to obtain htmlContent or PlainText of what was sent / will be sent when using dynamic templates Jul 14, 2020
@thinkingserious thinkingserious added the type: docs update documentation change not affecting the code label Jul 15, 2020
@thinkingserious
Copy link
Contributor

thinkingserious commented Jul 16, 2020

Internal tracking for creating an API endpoint: MP-6255

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: help wanted requesting help from the community type: community enhancement feature request not on Twilio's roadmap type: docs update documentation change not affecting the code
Projects
None yet
Development

No branches or pull requests

2 participants