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

New custom dyanmic templates #5

Merged
merged 4 commits into from
Nov 17, 2021
Merged

Conversation

jwoertink
Copy link
Member

@jwoertink jwoertink commented Nov 15, 2021

This adds in the ability for an email to specify a Dynamic Template https://docs.sendgrid.com/ui/sending-email/how-to-send-an-email-with-dynamic-transactional-templates

Dynamic templates are designed and created within the SendGrid website itself. The template builder allows you to specify handlebar type variable placeholders. In the API, you send the template_id, and the dynamic_template_data hash, and SendGrid compiles the email for you to send that.

To use this feature, you would just define two specific methods in your email.

class NotifierEmail < BaseEmail
  def initialize(@user : User)
  end

  def template_id
    "the-special-id-in-sendgrid"
  end

  def dynamic_template_data
    {
      "username" => @user.username,
      "unsub_token" => @user.unsubscribe_token
    }
  end

  to @user
  subject "Notification"
  # note `templates` isn't specified here...
end

NotifierEmail.new(current_user).deliver_now

Since this is pretty custom to SendGrid, I couldn't see a way to make it more generic to be a Carbon specific feature.

@jwoertink
Copy link
Member Author

Ok, it seems there's an issue with how SendGrid determines what to send sendgrid/sendgrid-nodejs#435 (comment) Even though the docs say content is required, my guess is sending the key (even with an empty hash) makes their API throw an error. To circumvent this, you have to add something like:

def html_body
  "0"
end

I'll need to see if there's a way to remove the content key if template_id is not nil to test that it works. Another theory might be that they support some sort of fallback? I haven't seen any docs that mention that, but I wouldn't be surprised if the template_id was invalid, it just used whatever you sent in the content array...

@jwoertink
Copy link
Member Author

Most of the API was implemented using NamedTuple which isn't dynamic. You can't dynamically add in keys, or remove them. The Sendgrid API docs don't mention this, but the theory here is that if you add template_id, you have to remove content otherwise it will try to use that first.

2d59edb refactors the interface to use Hashes so we can add/remove keys dynamically to the params. This also caused a bit of a compiler headache... I'm going to add this to my app to fully test this. If it works, we can keep it in... if not, then I'll revert and consider adding in the html_body "hack" 😝

@jwoertink
Copy link
Member Author

That worked! No need for the html_body hack as long as there's no content key sent when you have template_id defined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants