-
Notifications
You must be signed in to change notification settings - Fork 322
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
Comments
Hello @Hamdan85, I think there may be a bug where you need to add the following:
Could you please try that? Thanks! With Best Regards, Elmer |
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... ps: I can send the email, but the variable is still empty |
Can you please share what the HTML looks like? Thanks! |
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. |
Yes, I’m referring to the HTML in the SendGrid platform. I’m trying to reproduce the error. Thanks! |
Ok. I have chosed the variable name in several formats: {{name}}, -name-, %name% <!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. </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> </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> |
ps, my footer also doesnt work even if I have edited my account info. I dont have write power over footer params. |
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 |
When I use emailer helper I dont even get to send the email... => #<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. |
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! |
@thinkingserious but this example doesn't work anyway... the email arrives without the dynamic content... how can I use it?! |
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 |
@thinkingserious I got the same problem.
My ruby code:
Is there any way to make it work? |
is there a rest doc? this lib seems way not ready for production... |
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 |
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 |
What the "'+ +'" mean?
Em qui, 26 de jul de 2018 17:46, Elmer Thomas <notifications@github.com>
escreveu:
… @Hamdan85 <https://github.com/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": ***@***.***+'", "country_name": ***@***.***_name+'", "country_code": ***@***.***+'", "user_id": ***@***.***_s+'" }, "to": [ { "email": ***@***.***" } ] } ], "subject": "New registration", "template_id": "'+template_id_for('notify_new_user')+'"}')
response = sg.client.mail._("send").post(request_body: data)puts response.status_codeputs response.bodyputs response.headers
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#299 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABzHSoXZyT2Sgs5GECVm7CVggJTRyLDFks5uKiqmgaJpZM4VcAIV>
.
|
It's string interpolation. |
Lol...
Missed the ' in the beginning
Em qui, 26 de jul de 2018 21:43, Elmer Thomas <notifications@github.com>
escreveu:
… It's string interpolation
<https://www.digitalocean.com/community/tutorials/how-to-work-with-strings-in-ruby#using-string-interpolation>
.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#299 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABzHStqZNeGDSeFuYG-BZpzk4mlNhW-vks5uKmIWgaJpZM4VcAIV>
.
|
so much thank to @thinkingserious |
@thinkingserious |
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 |
how about the sender_name data for transactional templates? is it applies to this hash too? |
@Hamdan85 Everything is ok with me except the email subject. Does it shown up in your email? |
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 |
I do can change de subject but by setting it with handlebar variables in
template... Not by using the subject key.
Try to put it in template like {{subject}} and set it together with your
other variables.
Lol... This doc/sdk seems very messy isn't it?
Em sáb, 28 de jul de 2018 12:35, Christian Fazzini <notifications@github.com>
escreveu:
… Same here. Subject doesnt seem to work here!
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#299 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABzHSiFFKN_aA-0-Ws1b3z023W7-jFrSks5uLIS_gaJpZM4VcAIV>
.
|
@NavinGelot You need to include
|
solved, yes that i did but i forgot to replace subject in ui Thanks @krzkrzkrz :) & @Hamdan85 |
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 thing |
Thanks for the confirmation @pdkproitf. |
I think that is some sort of trolling. We have the docs with explicit arguments such as 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 "personalizations": {
"dynamic_template_data": {
"val": "some text"
}
} as the argument for an api. Which is not mentioning anywhere. The documentation quality is complete joke. |
I wouldn't say that but is quite annoying.
|
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 |
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! |
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. |
That's odd right? Since this sdk is largely used and found the problem
earlier...but ok
Em seg, 20 de ago de 2018 22:12, Elmer Thomas <notifications@github.com>
escreveu:
… You are correct @24601 <https://github.com/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
<#301> for progress.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#299 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABzHStSh5gLUdyvLtP1v02v4oZvocL9dks5uS15ggaJpZM4VcAIV>
.
|
I'm closing out this issue to help consolidate things. Please check out this issue for details. |
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:Then update your <div style="display:none">{{ subject }}</div> Note: 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 |
Wondering if the PR is merged. I got mine working by adding the key If you have multiple personalizations, you can reference them by index or do a loop on the eg. The Interpolation should look like this |
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 |
That's starting to become dramatic lol
Em qua, 5 de set de 2018 20:51, Elmer Thomas <notifications@github.com>
escreveu:
… Hello @andela-venogwe <https://github.com/andela-venogwe>,
The PR is merged, but the gem has not been updated yet. If you subscribe
to our release notifications <https://dx.sendgrid.com/newsletter/ruby>
you will receive an email once it's live.
Thanks!
With Best Regards,
Elmer
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#299 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABzHSpr26iVNc0GKc2L6CcjaI2E6N-Lnks5uYGObgaJpZM4VcAIV>
.
|
How about the URL of a button? How can we dynamically set it? |
Hey @HokhyTann You should be able to set a URL dynamically using something like this:
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. |
@kylearoberts thank you somuch, got it working now |
Still waiting for the updation of gem regarding this issue. |
Okay... so this wasn't well-documented, but I wanted to share my finding. You can supply 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 |
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:
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. |
@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. |
ya'll really need to stop changing templates to "legacy" 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 |
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 |
Issue Summary
I got 400 error trying to send a transactional template:
Response
Technical details:
The text was updated successfully, but these errors were encountered: