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

Substitutions may not be used with dynamic templating #299

Closed
Hamdan85 opened this issue Jul 24, 2018 · 58 comments
Closed

Substitutions may not be used with dynamic templating #299

Hamdan85 opened this issue Jul 24, 2018 · 58 comments
Labels
difficulty: unknown or n/a fix is unknown in difficulty status: help wanted requesting help from the community type: bug bug in the library

Comments

@Hamdan85
Copy link

Issue Summary

I got 400 error trying to send a transactional template:

class UserMailer < ApplicationMailer
  require 'sendgrid-ruby'
  include SendGrid

  def welcome(user)
    mail = Mail.new
    mail.from = Email.new(email: user.email)
    personalization = Personalization.new
    personalization.add_to(Email.new(email: user.email, name: user.name))
    personalization.add_substitution(Substitution.new(key: '-name-', value: user.name))
    mail.add_personalization(personalization)

    mail.template_id = '<my template id>'

    sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
    response = sg.client.mail._('send').post(request_body: mail.to_json)
    puts response.status_code
    puts response.body
    puts response.headers
  end
end

Response

=> #<SendGrid::Response:0x00007fbe6650bb40 @status_code="400", @body="{\"errors\":[{\"message\":\"Substitutions may not be used with dynamic templating\",\"field\":\"personalizations.0.substitutions\",\"help\":\"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.personalizations.substitutions\"}]}", @headers={"server"=>["nginx"], "date"=>["Tue, 24 Jul 2018 02:58:15 GMT"], "content-type"=>["application/json"], "content-length"=>["238"], "connection"=>["close"], "access-control-allow-origin"=>["https://sendgrid.api-docs.io"], "access-control-allow-methods"=>["POST"], "access-control-allow-headers"=>["Authorization, Content-Type, On-behalf-of, x-sg-elas-acl"], "access-control-max-age"=>["600"], "x-no-cors-reason"=>["https://sendgrid.com/docs/Classroom/Basics/API/cors.html"]}>

Technical details:

  • sendgrid-ruby Version: master
  • Ruby Version: 2.5.1p57
@thinkingserious
Copy link
Contributor

Hello @Hamdan85,

I think there may be a bug where you need to add the following:

mail.add_content(Content.new(type: 'text/html', value: '')) right before mail.template_id = '<my template id>'.

Could you please try that?

Thanks!

With Best Regards,

Elmer

@thinkingserious thinkingserious added status: help wanted requesting help from the community type: question question directed at the library difficulty: unknown or n/a fix is unknown in difficulty labels Jul 24, 2018
@Hamdan85
Copy link
Author

Hamdan85 commented Jul 24, 2018

hi @thinkingserious ... thanks for your answer.

I have the same result. Im amazed how confuse this api can be...

In fact, i just got it (almost) working by using this:

class UserMailer < ApplicationMailer
  require 'sendgrid-ruby'
  include SendGrid

  def welcome(user)
    data = {
      personalizations: [
        {
          to: [
            {
              email: user.email
            }
          ],
          "-name-": user.name,
          subject: "NOT"
        }
      ],
      from: {
        email: "---@---.---.--"
      },
      template_id: "my api id"
    }
    response = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']).client.mail._('send').post(request_body: data)
    p response
  end
end

Im also amazed that this isnt documented in anywhere...
how can a substitution not be allowed if this is exactly the point of the api? lol...

ps: I can send the email, but the variable is still empty

@thinkingserious
Copy link
Contributor

Can you please share what the HTML looks like? Thanks!

@Hamdan85
Copy link
Author

There's no HTML... that's the point of using the template_id which is the id of a template that has the HTML in sendgrid platform.
Am I wrong?

@thinkingserious
Copy link
Contributor

Yes, I’m referring to the HTML in the SendGrid platform. I’m trying to reproduce the error. Thanks!

@Hamdan85
Copy link
Author

Ok.

I have chosed the variable name in several formats: {{name}}, -name-, %name%
nothing works
Here's the code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html data-editor-version="2" class="sg-campaigns" xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1" /><!--[if !mso]><!-->
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" /><!--<![endif]-->
    <!--[if (gte mso 9)|(IE)]>
    <xml>
    <o:OfficeDocumentSettings>
    <o:AllowPNG/>
    <o:PixelsPerInch>96</o:PixelsPerInch>
    </o:OfficeDocumentSettings>
    </xml>
    <![endif]-->
    <!--[if (gte mso 9)|(IE)]>
    <style type="text/css">
      body {width: 600px;margin: 0 auto;}
      table {border-collapse: collapse;}
      table, td {mso-table-lspace: 0pt;mso-table-rspace: 0pt;}
      img {-ms-interpolation-mode: bicubic;}
    </style>
    <![endif]-->

    <style type="text/css">
      body, p, div {
        font-family: arial;
        font-size: 14px;
      }
      body {
        color: #000000;
      }
      body a {
        color: #1188E6;
        text-decoration: none;
      }
      p { margin: 0; padding: 0; }
      table.wrapper {
        width:100% !important;
        table-layout: fixed;
        -webkit-font-smoothing: antialiased;
        -webkit-text-size-adjust: 100%;
        -moz-text-size-adjust: 100%;
        -ms-text-size-adjust: 100%;
      }
      img.max-width {
        max-width: 100% !important;
      }
      .column.of-2 {
        width: 50%;
      }
      .column.of-3 {
        width: 33.333%;
      }
      .column.of-4 {
        width: 25%;
      }
      @media screen and (max-width:480px) {
        .preheader .rightColumnContent,
        .footer .rightColumnContent {
            text-align: left !important;
        }
        .preheader .rightColumnContent div,
        .preheader .rightColumnContent span,
        .footer .rightColumnContent div,
        .footer .rightColumnContent span {
          text-align: left !important;
        }
        .preheader .rightColumnContent,
        .preheader .leftColumnContent {
          font-size: 80% !important;
          padding: 5px 0;
        }
        table.wrapper-mobile {
          width: 100% !important;
          table-layout: fixed;
        }
        img.max-width {
          height: auto !important;
          max-width: 480px !important;
        }
        a.bulletproof-button {
          display: block !important;
          width: auto !important;
          font-size: 80%;
          padding-left: 0 !important;
          padding-right: 0 !important;
        }
        .columns {
          width: 100% !important;
        }
        .column {
          display: block !important;
          width: 100% !important;
          padding-left: 0 !important;
          padding-right: 0 !important;
          margin-left: 0 !important;
          margin-right: 0 !important;
        }
      }
    </style>
    <!--user entered Head Start-->
    
     <!--End Head user entered-->
  </head>
  <body>
    <center class="wrapper" data-link-color="#1188E6" data-body-style="font-size: 14px; font-family: arial; color: #000000; background-color: #ffffff;">
      <div class="webkit">
        <table cellpadding="0" cellspacing="0" border="0" width="100%" class="wrapper" bgcolor="#ffffff">
          <tr>
            <td valign="top" bgcolor="#ffffff" width="100%">
              <table width="100%" role="content-container" class="outer" align="center" cellpadding="0" cellspacing="0" border="0">
                <tr>
                  <td width="100%">
                    <table width="100%" cellpadding="0" cellspacing="0" border="0">
                      <tr>
                        <td>
                          <!--[if mso]>
                          <center>
                          <table><tr><td width="600">
                          <![endif]-->
                          <table width="100%" cellpadding="0" cellspacing="0" border="0" style="width: 100%; max-width:600px;" align="center">
                            <tr>
                              <td role="modules-container" style="padding: 0px 0px 0px 0px; color: #000000; text-align: left;" bgcolor="#ffffff" width="100%" align="left">
                                
    <table class="module preheader preheader-hide" role="module" data-type="preheader" border="0" cellpadding="0" cellspacing="0" width="100%"
           style="display: none !important; mso-hide: all; visibility: hidden; opacity: 0; color: transparent; height: 0; width: 0;">
      <tr>
        <td role="module-content">
          <p>Agora falta pouco... jajá te ligamos.</p>
        </td>
      </tr>
    </table>
  
    <table class="wrapper" role="module" data-type="image" border="0" cellpadding="0" cellspacing="0" width="100%" style="table-layout: fixed;">
      <tr>
        <td style="font-size:6px;line-height:10px;padding:0px 0px 0px 0px;" valign="top" align="center">
          <a href="https://cancelarammeuvoo.com.br"><img class="max-width" border="0" style="display:block;color:#000000;text-decoration:none;font-family:Helvetica, arial, sans-serif;font-size:16px;max-width:100% !important;width:100%;height:auto !important;" src="https://marketing-image-production.s3.amazonaws.com/uploads/5db65a3554ff036853826cc0c53a8c67d981a2ed964ee2f2615f9644566b7a5e34d40a3e436152030e227178c7b4d63afcc4a3539651d30344dd2e03b278a036.png" alt="logo" width="600"></a>
        </td>
      </tr>
    </table>
  
    <table class="module" role="module" data-type="text" border="0" cellpadding="0" cellspacing="0" width="100%" style="table-layout: fixed;">
      <tr>
        <td style="background-color:#ffffff;padding:18px 0px 18px 0px;line-height:22px;text-align:inherit;"
            height="100%"
            valign="top"
            bgcolor="#ffffff">
            <h1 style="text-align: center;">Muito obrigado por sua confiança.&nbsp;</h1>

<div>Olá -name-,</div>

<div>Muito obrigado por sua confiança. Em breve ligaremos para requisitar alguns documentos, uma procuração e mais dados sobre o ocorrido. Isso garante que nosso time jurídico faça tudo certinho pra gente resolver isso o mais rápido possível ok?</div>

<div>&nbsp;</div>

<div>Um grande abraço e até daqui a pouquinho.</div>
        </td>
      </tr>
    </table>
  <div data-role="module-unsubscribe" class="module unsubscribe-css__unsubscribe___2CDlR" role="module" data-type="unsubscribe" style="color:#444444;font-size:12px;line-height:20px;padding:16px 16px 16px 16px;text-align:center"><div class="Unsubscribe--addressLine"><p class="Unsubscribe--senderName" style="font-family:Arial,Helvetica, sans-serif;font-size:12px;line-height:20px">[Sender_Name]</p><p style="font-family:Arial,Helvetica, sans-serif;font-size:12px;line-height:20px"><span class="Unsubscribe--senderAddress">[Sender_Address]</span>, <span class="Unsubscribe--senderCity">[Sender_City]</span>, <span class="Unsubscribe--senderState">[Sender_State]</span> <span class="Unsubscribe--senderZip">[Sender_Zip]</span> </p></div><p style="font-family:Arial,Helvetica, sans-serif;font-size:12px;line-height:20px"><a class="Unsubscribe--unsubscribeLink" href="<%asm_group_unsubscribe_raw_url%>">Unsubscribe</a> - <a class="Unsubscribe--unsubscribePreferences" href="<%asm_preferences_raw_url%>">Unsubscribe Preferences</a></p></div>
                              </td>
                            </tr>
                          </table>
                          <!--[if mso]>
                          </td></tr></table>
                          </center>
                          <![endif]-->
                        </td>
                      </tr>
                    </table>
                  </td>
                </tr>
              </table>
            </td>
          </tr>
        </table>
      </div>
    </center>
  </body>
</html>

@Hamdan85
Copy link
Author

ps, my footer also doesnt work even if I have edited my account info. I dont have write power over footer params.

@thinkingserious
Copy link
Contributor

@Hamdan85,

Thanks for providing the HTML.

Does the -name- placeholder stay as -name- or is it blank in the final email?

Also, have you tried running this example?

With Best Regards,

Elmer

@Hamdan85
Copy link
Author

When I use emailer helper I dont even get to send the email...
I get this response:

=> #<SendGrid::Response:0x00007fbe6650bb40 @status_code="400", @body="{\"errors\":[{\"message\":\"Substitutions may not be used with dynamic templating\",\"field\":\"personalizations.0.substitutions\",\"help\":\"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.personalizations.substitutions\"}]}", @headers={"server"=>["nginx"], "date"=>["Tue, 24 Jul 2018 02:58:15 GMT"], "content-type"=>["application/json"], "content-length"=>["238"], "connection"=>["close"], "access-control-allow-origin"=>["https://sendgrid.api-docs.io"], "access-control-allow-methods"=>["POST"], "access-control-allow-headers"=>["Authorization, Content-Type, On-behalf-of, x-sg-elas-acl"], "access-control-max-age"=>["600"], "x-no-cors-reason"=>["https://sendgrid.com/docs/Classroom/Basics/API/cors.html"]}>

Trying to send with the payload described above gives me the email with the value empty.

@thinkingserious
Copy link
Contributor

Ah, my apologies, the issue is that you are using the new version of our templates, which uses a different mechanism for substitutions. I will be updating this SDK soon to reflect these changes. In the mean-time, using the example you provided here, please use this syntax within the Personalization object. Thanks!

@Hamdan85
Copy link
Author

@thinkingserious but this example doesn't work anyway... the email arrives without the dynamic content... how can I use it?!

@thinkingserious
Copy link
Contributor

@Hamdan85,

Please try

class UserMailer < ApplicationMailer
  require 'sendgrid-ruby'
  include SendGrid

  def welcome(user)
    data = {
      personalizations: [
        {
          to: [
            {
              email: user.email
            }
          ],
          dynamic_template_data: {
               name: user.name
          },
          subject: "NOT"
        }
      ],
      from: {
        email: "---@---.---.--"
      },
      template_id: "my api id"
    }
    response = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']).client.mail._('send').post(request_body: data)
    p response
  end
end

And in the HTML use {{name}}

@PhucQuoine
Copy link

@thinkingserious I got the same problem.
There are some reasons so I'm using the old version sendgrid-ruby-4.0.8.
My template

<div><div><p>
  Dear Support,
</p>
<p>
  A new user has just registered.  Details below:
  </p><ul>
    <li>Email: {{email}}</li>
    <li>Location: {{country_name}} ({{country_code}})</li>
    <li>ID: {{user_id}}</li>
  </ul>
<p></p>
<p>
  Regards
</p>
<strong>This one sent by sendgrid template.</strong></div></div>

My ruby code:

mail = Mail.new
    @user = User.find(user_id)
    mail.subject = 'New registration'

    personalization = Personalization.new
    personalization.to = Email.new(email: 'support@mailinator.com')
    personalization.substitutions = Substitution.new(key: 'email', value: @user.email)
    personalization.substitutions = Substitution.new(key: 'country_name', value: @user.country_name)
    personalization.substitutions = Substitution.new(key: 'country_code', value: @user.country)
    personalization.substitutions = Substitution.new(key: 'user_id', value: @user.id.to_s)
    mail.personalizations = personalization

    mail.template_id = template_id_for('notify_new_user')
    mail.categories = Category.new(name: 'notify_new_user')
    mail.from = Email.new(email: Setting.support_email, name: 'Support')
    sg = SendGrid::API.new(api_key: SENDGRID_API_KEY)
    response = sg.client.mail._('send').post(request_body: mail.to_json)

Is there any way to make it work?

@Hamdan85
Copy link
Author

is there a rest doc? this lib seems way not ready for production...
no demands here lol... it's just a observation.

@PhucQuoine
Copy link

@Hamdan85
There is a pull request waiting for review #300

@thinkingserious
Copy link
Contributor

Hello everyone!

Please see this issue and PR #300 for progress and current work around.

Thanks for your assistance and patience!

With Best Regards,

Elmer

@thinkingserious
Copy link
Contributor

@Hamdan85,

Code similar to the following should work for v4.0.8:

data = JSON.parse('{
  "categories": [
    "notify_new_user"
  ],
  "from": {
    "email": "'+Setting.support_email+'",
    "name": "Support"
  },
  "personalizations": [
    {
      "dynamic_template_data": {
        "email": "'+@user.email+'",
        "country_name": "'+@user.country_name+'",
        "country_code": "'+@user.country+'",
        "user_id": "'+@user.id.to_s+'"
      },
      "to": [
        {
          "email": "support@mailinator.com"
        }
      ]
    }
  ],
  "subject": "New registration",
  "template_id": "'+template_id_for('notify_new_user')+'"
}')
response = sg.client.mail._("send").post(request_body: data)
puts response.status_code
puts response.body
puts response.headers

@Hamdan85
Copy link
Author

Hamdan85 commented Jul 27, 2018 via email

@thinkingserious
Copy link
Contributor

It's string interpolation.

@Hamdan85
Copy link
Author

Hamdan85 commented Jul 27, 2018 via email

@PhucQuoine
Copy link

so much thank to @thinkingserious
It's work for me.

@PhucQuoine
Copy link

@thinkingserious
The email successfully sent. But there is another problem, the email subject doesn't show up while correct email content.

@Hamdan85
Copy link
Author

Hamdan85 commented Jul 28, 2018

when i put {{name}} in template, it just says to me that the template is invalid on sendgrid UI... that's new.

UPDATE: it works now... from nowhere... lol

@Hamdan85
Copy link
Author

how about the sender_name data for transactional templates? is it applies to this hash too?
bacause nothing in UI seems to be configuring these particular fields

@PhucQuoine
Copy link

PhucQuoine commented Jul 28, 2018

@Hamdan85 Everything is ok with me except the email subject. Does it shown up in your email?

@krzkrzkrz
Copy link

krzkrzkrz commented Jul 28, 2018

Same here. Subject doesnt seem to work here! As in. Its not appearing

Unfortunately https://sendgrid.com/docs/User_Guide/Transactional_Templates/how_to_send_an_email_with_transactional_templates.html doesnt say anything about subject too. Not sure if bad documentation or just lacking in feature

@Hamdan85
Copy link
Author

Hamdan85 commented Jul 28, 2018 via email

@krzkrzkrz
Copy link

@NavinGelot You need to include subject in dynamic_template_data:

"dynamic_template_data": {
  "subject": "Lorem ipsum",
  "name": "navin",
  "city": "AMD"
},

@NavinGelot
Copy link

NavinGelot commented Jul 30, 2018

solved, yes that i did but i forgot to replace subject in ui Thanks @krzkrzkrz :) & @Hamdan85

email

@thinkingserious
Copy link
Contributor

Hello Everyone,

Have you tried including the subject key in the Personalization object?

For example:

def notify_new_user_v2(user_id)
    @user = User.find(user_id)
    data = JSON.parse('{
      "categories": [
        "notify_new_user"
      ],
      "from": {
        "email": "'+Setting.support_email+'",
        "name": "Support"
      },
      "personalizations": [
        {
          "dynamic_template_data": {
          },
          "to": [
            {
              "email": "'+@user.email+'"
            }
          ]
        },
        "subject": "New registration",
      ],
      "template_id": "'+template_id_for('notify_new_user')+'"
    }')

    sg = SendGrid::API.new(api_key: SENDGRID_API_KEY)
    response = sg.client.mail._("send").post(request_body: data)
    p '***************'
    p response
  end

That said, including a subject outside of the Personaliztion object should act as a global override. I'll leave this issue open and on our backlog for further investigation.

With Best Regards,

Elmer

@thinkingserious thinkingserious added type: bug bug in the library and removed type: question question directed at the library labels Jul 30, 2018
@pdkproitf
Copy link

@thinkingserious thing
I did. But it didn't work with the dynamic template.

@thinkingserious
Copy link
Contributor

Thanks for the confirmation @pdkproitf.

@achempion
Copy link

I think that is some sort of trolling. We have the docs with explicit arguments such as template_id and substitutions but that is not working as expected.

https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/index.html

To make things work we should spend hours to finally get the answer — we need to use {{ val }} inside the template and

"personalizations": {
    "dynamic_template_data": {
        "val": "some text"
    }
}

as the argument for an api. Which is not mentioning anywhere.

The documentation quality is complete joke.

@Hamdan85
Copy link
Author

Hamdan85 commented Aug 9, 2018 via email

@thinkingserious
Copy link
Contributor

Hello @achempion,

Thanks for your feedback and taking the time to post a solution.

An update is coming shortly.

Please check out this issue for details. Specifically, this PR is just about ready to go live.

With Best Regards,

Elmer

@24601
Copy link

24601 commented Aug 20, 2018

Setting dynamic_template_data instead of substitutions NEEDS to be documented VERY VERY clearly. It is NOT documented ANYWHERE but here, and it just silently FAILS.

This cost us an entire afternoon of frustration.

The docs are contradictory and don't match reality! In fact, there are many examples, documentation items, and reference pieces that actively guide the reader/user in the totally wrong direction!

@thinkingserious
Copy link
Contributor

You are correct @24601.

We have released support for PHP and Python and are now working on C#. Next is Java, then Node.js, then Go and then this SDK.

My apologies for the delay.

Everyone,

Please follow this issue for progress.

@Hamdan85
Copy link
Author

Hamdan85 commented Aug 21, 2018 via email

@thinkingserious
Copy link
Contributor

I'm closing out this issue to help consolidate things. Please check out this issue for details.

@kwent
Copy link

kwent commented Aug 24, 2018

Posting my solution since i spent as well wayyyyy too much time to make this work. Documentation needs to be revisited for sure.

Sendgrid Template GUI side:

screen shot 2018-08-24 at 5 04 19 pm

Then update your subject code block with:

<div style="display:none">{{ subject }}</div>

Note: {{{ body }}} vs {{ body }} is to handle html

Sendgrid Ruby SDK API side:

data = {
  from: {
    email: "no-reply@acme.com",
    name: "Acme"
  },
  personalizations: [
    dynamic_template_data: {
      subject: "Hello world",
      body: "<strong>Body is strong</strong>"
    },
    to: [{
      email: "user@example.com",
      name: "John Doe"
    }]
  ],
  template_id: "my_template_id"
}

begin
  sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
  response = sg.client.mail._("send").post(request_body: data)
rescue Exception => e
  Rollbar.error(e)
end

Enjoy

@victor-enogwe
Copy link

Wondering if the PR is merged.
The helper methods helps keep things clean and easy.
I currently just avoid using the add_substitution method.

I got mine working by adding the key dynamic_template_data to the personalizations Array as stated above.

If you have multiple personalizations, you can reference them by index or do a loop on the mail.personalizations Array. Where mail is an instance of Mail.

eg. mail.personalizations[0]['dynamic_template_data'] = data
where data is a Hash which contains your template substitutions.
eg. { title: 'lorem ipsum', description: 'dolor sit amet' }

The Interpolation should look like this {{title}} in your templates.

@thinkingserious
Copy link
Contributor

Hello @andela-venogwe,

The PR is merged, but the gem has not been updated yet. If you subscribe to our release notifications you will receive an email once it's live.

Thanks!

With Best Regards,

Elmer

@Hamdan85
Copy link
Author

Hamdan85 commented Sep 6, 2018 via email

@HokhyTann
Copy link

How about the URL of a button? How can we dynamically set it?

@kylearoberts
Copy link
Contributor

Hey @HokhyTann

You should be able to set a URL dynamically using something like this:

<a href='{{{url}}}'> Link </a>

You likely want to use the triple bracket so that handlebars does not encode any of the link. Just make sure the link you are substituting is correctly URL encoded and you should be good.

@HokhyTann
Copy link

@kylearoberts thank you somuch, got it working now

@Ume1931
Copy link

Ume1931 commented Oct 12, 2018

Still waiting for the updation of gem regarding this issue.
How long it would take?

@joncursi
Copy link

joncursi commented Oct 12, 2018

Okay... so this wasn't well-documented, but I wanted to share my finding.

You can supply dynamic_template_data both outside and inside of personalizations. If you have a bunch of dynamic template data that is shared among all recipients, but then want to personalize something like a firstName field for each user, you can do this:

const dynamicTemplateData = { ... };

sendgrid.send(
  {
    dynamic_template_data: dynamicTemplateData,
    from: 'xx@xx.xx',
    personalizations: [
      {
        dynamic_template_data: {
          firstName: 'Jon',
        },
        to: 'xx@xx.xx',
      },
      {
        dynamic_template_data: {
          firstName: 'Jane',
        },
        to: 'xx@xx.xx',
      },
    ],
    templateId: 'xxx',
  });

It will appropriately merge each user's firstName into the overall set of dynamic_template_data

@kylearoberts
Copy link
Contributor

@joncursi

Thanks for the great testing. I tried to replicate what you did but did not have any luck. Could you take a look and let me know what I am doing differently:

curl -X POST \
  https://api.sendgrid.com/v3/mail/send \
  -H 'Authorization: Bearer API Key' \
  -H 'Content-Type: application/json' \
  -H 'cache-control: no-cache' \
  -d '{
	"dynamic_template_data": {
		"subject": "Hello World!"
	},
	"personalizations": [{
		"to": [{
			"email": "xx@xx.xx"
		}],
		"dynamic_template_data": {
			"first_name": "Kyle"
		}
	}],
	"from": {
		"email": "xx@xx.xx"
	},
	"reply_to": {
		"email": "xx@xx.xx"
	},
	"template_id": "xxx"
}'

The dynamic data in the personalization works, but not the dynamic data outside the personalization. It looks like I am doing it the same way as you, but since it is not working I must be missing something.

@joncursi
Copy link

@kylearoberts that looks good to me. Not sure why it's not working. I defer to the SendGrid team on this one.

FWIW, I am firing my requests using the @sendgrid/mail library.

@kylearoberts
Copy link
Contributor

kylearoberts commented Oct 12, 2018

@Ume1931

Looks like the code just got updated. Looks like the only thing left is for the creation of some examples on how to use it.

@joncursi

I'll see what I can do to replicate the issue and get it to our engineers.

@fsa-development
Copy link

ya'll really need to stop changing templates to "legacy"
you've introduced backwards-incompatible "upgrades" to templating multiple times in the last couple years
this is terrible product design

you should either make forward-compatible types or make all backwards-incompatible changes in the API and SDK, and keep previous versions of the API running. So if I'm using v4 of the API and v4 of the SDK it will never break and never lose the ability to make compatible templates in the dashboard

ugh why do we have to tell you this

@thinkingserious
Copy link
Contributor

Hello @fsa-development,

I would love to help resolve your issue. Is it your current goal to use our "legacy" templating with the most up to date version of this SDK? If so, this example should work.

With Best Regards,

Elmer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty: unknown or n/a fix is unknown in difficulty status: help wanted requesting help from the community type: bug bug in the library
Projects
None yet
Development

No branches or pull requests