Sendgrid-nodejs: 替代无效

创建于 2018-04-10  ·  43评论  ·  资料来源: sendgrid/sendgrid-nodejs

发行摘要

遵循在交易电子邮件中使用替代而不进行任何更改的

image

重现步骤

  1. 克隆https://github.com/panayi/test-sg/
  2. 复制示例中
  3. 编辑test.js以插入您的API密钥,您的电子邮件地址和您在上面创建的templateId
  4. 运行node test.js

技术细节:

unknown or a help wanted question

最有用的评论

嗨,在阅读@himanshupnt的评论后,我使用旧版模板进行了测试,并且可以正常工作。 谢谢@himanshupnt

后来,我找到了Sendgrid文档“带有动态事务模板的邮件发送” ,我已经开始使用新模板(不是旧模板)了。 我使用了相同的HTML模板,并且我的代码已更新为:

  const sgMail = require("@sendgrid/mail");
  sgMail.setApiKey(process.env.SENDGRID_API_KEY);
  sgMail.setSubstitutionWrappers("{{", "}}"); // Configure the substitution tag wrappers globally

  const msg = {
    from: "[email protected]",
    templateId: "d-27e7d954368c46519eddc806e5cf8156",
    personalizations: [
      {
        to: [
          {
            email: "[email protected]"
          }
        ],
        dynamic_template_data: {
          name: "Someone",
          city: "Somewhere",
          subject: "Hello new world"
        }
      }
    ]
  };

  sgMail.send(msg).catch(err => console.error(err.message));

请注意,我将“ substitutions”替换为“ dynamic_template_data”,并根据文档的要求将“ to”放置在“个性化设置”中。 而且我也将主题作为“ dynamic_template_data”之一,并且可以正常工作。 (将{{subject}}放在模板主题字段中)

谢谢。

所有43条评论

你好@panayi

一些要检查的东西:

  1. 验证模板ID是否正确
  2. 验证模板在用户界面中是否标记为活动

您还能分享您的HTML外观吗?

谢谢!

最诚挚的问候,

埃尔默

您好, @thinkingserious ,我能够重现与@panayi提到的相同的问题。
我的HTML代码看起来像这样

您好first_name(员工:employee_id)
一些HTML代码

sgMail.setApiKey('apikey');
sgMail.setSubstitutionWrappers('{{', '}}'); // Triied sgMail.setSubstitutionWrappers('-', '-'); as well`

const msg = {
  to: '[email protected]',
  from: '[email protected]',
  subject: 'Hello world',
  text: 'Hello plain world!',
  html: 'Hello HTML world!',
  templateId: 'template-id',
  substitutions: {
    first_name: 'Tejas',
    employee_id: 'emp123',
  },
};
sgMail.send(msg);

注意:我什至尝试使用substitutionWrappers: ['{{', '}}']
在味精内,但没有运气。

技术细节:

我在此问题上投入了大量时间,因此不胜感激

你好@tjstlekr

在您的HTML代码中,请将first_name更改{{first_name}} ,将employee_id更改{{employee_id}}

谢谢!

最诚挚的问候,

埃尔默

你好@thinkingserious
通过HTML代码,您是指模板代码吗?
为什么在这里在请求正文中指定了替换标记后,替换标记可以正常工作,但是在使用页面上的预定义示例时却不能正常工作?
谢谢!

@tjstlekr ,我的意思是本示例中描述的模板代码。

没有具体的例子,我无法回答你的第二个问题。 (例如,请求正文和模板代码)。

最诚挚的问候,

埃尔默

真可悲! 我无权访问模板代码。 我会坚持使用另一个版本。
无论如何还是谢谢

你有没有尝试过:

substitutions: {
    'first_name': 'Tejas',
    'employee_id': 'emp123',
  },

是的, @ thinkingserious尝试过。 我也尝试了以下方法


substitutions: {
    '-first_name-': 'Tejas',
    '-employee_id-': 'emp123',
  },

substitutions: {
    '%first_name%': 'Tejas',
    '%employee_id%': 'emp123',
  },

substitutions: {
    '=first_name=': 'Tejas',
    '=employee_id'=: 'emp123',
  },

@tjstlekr

我认为您的解决方案是让任何人都可以更改HTML模板代码,以用替换字符包装变量。

最诚挚的问候,

埃尔默

@thinkingserious是正确的。 模板变量必须用双括号{{name}}括起来。 否则,SendGrid将不知道将已发送的替换放在哪里。

变量真的需要双括号吗? 我会继续尝试,但是同一个人的不同帖子显示该替换使用破折号而不是双括号:
screenshot 2018-06-26 14 08 12

你好@tetreault

默认情况下,我们使用双括号。 setSubstitutionWrappers允许您按照演示进行修改。

谢谢!

最诚挚的问候,

埃尔默

我遇到了与@panayi相同的问题。

我遵循了事务模板示例。 我已经设置了替换包装器,并在模板中将替换包装在双括号中。 但是,我收到的电子邮件已删除所有替代项,而没有替换为我提供的替代项。

我尝试使用代码编辑器和设计编辑器作为模板。

技术细节:

谢谢!

@nrator

如果替换用空白字符串替换,我将再次检查要迭代的变量是否不为空。 如果您确实确认它们不为空,那么能否请您与相关HTML一起共享代码段,以便我可以尝试重现?

谢谢!

最诚挚的问候,

埃尔默

@thinkingserious

我不是使用变量而是字符串,因此它们不是空的。 以下是代码段,HTML和我得到的结果:

const sgMail = require("@sendgrid/mail");
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
sgMail.setSubstitutionWrappers("{{", "}}"); // Configure the substitution tag wrappers globally

const msg = {
  from: "[email protected]", // my personal email
  to: "[email protected]", // my another personal email
  subject: "Hello world",
  text: "Hello plain world!",
  html: "<p>Hello HTML world!</p>",
  templateId: "d-27e7d954368c46519eddc806e5cf8156",
  substitutions: {
    name: "Some One",
    city: "Denver"
  }
};

  sgMail.send(msg).catch(err => console.error(err.message));

HTML模板

<html>
<head>
    <title></title>
</head>
<body>
Hello {{name}},
<br /><br/>
I'm glad you are trying out the template feature!
<br /><br/>
<%body%>
<br /><br/>
I hope you are having a great day in {{city}} :)
<br /><br/>
</body>
</html>

我收到的电子邮件

Hello , 

I'm glad you are trying out the template feature! 

<%body%> 

I hope you are having a great day in :) 

技术细节
sendgrid-nodejs版本:@ sendgrid / [email protected]
Node.js版本:8.11.2

最好的祝福,
杰森

嗨,我在事务模板上遇到了同样的问题。 变量不会拾取。 使用测试数据预览SG仪表板中的模板,一切看起来都是正确的。
电子邮件会发送给正确的人,并带有正确的模板,但缺少变量值。
两天前,我创建了一个旧模板,效果很好,今天我改用较新的模板
交易模板,它不起作用。 我只是复制了旧的旧模板,
工作正常。
“ @ sendgrid / mail”:“ 6.3.1” | Node.js 8.10
这是我的代码:

async function sendEmail({ email, firstName, password }) {
  const msg = {
    to: [{ email, firstName }],
    from: {
      email: "[email protected]",
      name: "name"
    },
    asm: {
      group_id: 1234
    },
    templateId: "d-64b15a042b2d41d7a4b5b6a4eea649c5",
    substitutions: {
      userName: firstName,
      email,
      password
    }
  }
  try {
    await sgMail.send(msg);
  } catch(e){
    e.message
  }

  };

这是html模板:

<h3>Hi {{userName}},</h3>

<div><span style="font-size:16px;">Your account has been setup. To login please use</span></div>

<div><span style="font-size:16px;">the credentials below.</span></div>

<div>&nbsp;</div>

<div><span style="font-size:16px;"><strong>Email:</strong>&nbsp;{{email}}</span></div>

<div><strong><span style="font-size:16px;">Password:&nbsp;</span></strong><span style="color: rgb(0, 0, 0); font-family: arial; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400;">{{password}}</span></div>

<div><span style="font-weight: 600; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-family: arial; font-size: 14px; color: rgb(0, 0, 0);"><span style="font-size: 16px;">SignIn Url: <a href="https://a.abc.com">https://a.abc.com/</a></span></span></div>

<div>&nbsp;</div>

<div>&nbsp;</div>

<div><span style="font-size:16px;">Please let us know if you need any help, email us at <a href="mailto:[email protected]">[email protected]</a></span></div>

干杯

我只是使用相同的html代码切换到旧版模板,因此可以正常工作。 可能是由于V3 API中的新变化吗?

嗨,在阅读@himanshupnt的评论后,我使用旧版模板进行了测试,并且可以正常工作。 谢谢@himanshupnt

后来,我找到了Sendgrid文档“带有动态事务模板的邮件发送” ,我已经开始使用新模板(不是旧模板)了。 我使用了相同的HTML模板,并且我的代码已更新为:

  const sgMail = require("@sendgrid/mail");
  sgMail.setApiKey(process.env.SENDGRID_API_KEY);
  sgMail.setSubstitutionWrappers("{{", "}}"); // Configure the substitution tag wrappers globally

  const msg = {
    from: "[email protected]",
    templateId: "d-27e7d954368c46519eddc806e5cf8156",
    personalizations: [
      {
        to: [
          {
            email: "[email protected]"
          }
        ],
        dynamic_template_data: {
          name: "Someone",
          city: "Somewhere",
          subject: "Hello new world"
        }
      }
    ]
  };

  sgMail.send(msg).catch(err => console.error(err.message));

请注意,我将“ substitutions”替换为“ dynamic_template_data”,并根据文档的要求将“ to”放置在“个性化设置”中。 而且我也将主题作为“ dynamic_template_data”之一,并且可以正常工作。 (将{{subject}}放在模板主题字段中)

谢谢。

@nrator对我也是如此。 感谢您发布!

我遇到了同样的问题,我也提出了一个问题(尝试了开头提到的解决方案后)。 但是我能够将namecity@nrator所提到的模板中。 但是无法替换<%subject%><%body%>因为它们已包含在我收到的电子邮件中。 如何替换<%body%><%subject%>@himanshupnt您如何解决这个问题?

@ pankaja92 ,我也无法正常运行<%subject%>。 现在,我处理主题的方式是将其视为“ dynamic_template_data”之一,这意味着我将其包装在{{}}中,而不是<%%>并在“ dynamic_template_data”中处理。

我发现此sendgrid文档可能会有所帮助。 主题参数似乎不起作用。

@nrator谢谢。 有效。 因此,完全不需要<%body%>(因为它也不能像给出的示例一样工作)。

顺便说一句,不是特定于问题的-但是,您是否有任何经验或示例可以通过此库发送详细信息列表? 我正在寻找可以帮助我构建应用程序的东西:)

@ pankaja92我不确定发送详细信息列表是什么意思。 您可以阅读此sendgrid博客文章,了解动态模板的新功能,基本上,现在您可以遍历项目列表或使用基本条件生成动态内容。

对于实际代码,您可以参考此repo

希望能帮助到你!

大家好,在这个问题上也花了几个小时。

而不是使用substitutions ,而是在主msg对象中使用dynamicTemplateData

const sgMail = require('@sendgrid/mail')

sgMail.setApiKey(process.env.SENDGRID_API_KEY)
sgMail.setSubstitutionWrappers('{{', '}}')

const msg = {
  to: '[email protected]',
  from: '[email protected]',
  templateId: 'd-27e7d954368c46519eddc806e5cf8156',
  dynamicTemplateData: {
    name: 'Some One',
    city: 'Denver',
  },
}

sgMail.send(msg)

参考: https :

根据该PR,当templateId是动态模板(带有d-前缀)时, substitutions根本不会发送,因此dynamicTemplateData应该使用对象。

他们在这里创建了一个问题

请关注此问题:#703

将“替换”更改为“ dynamic_template_data”

@jseyfert应该不是dynamic_template_data吗?

@aroach你是正确的。 我已经更新了我的评论。

起初,它对我也不起作用。
我注意到我的版本有点过时了(我使用的是@ sendgrid / mail @ 6.2.1)
我已经更新了程序包,更改了代码,现在可以使用了:

使用@ sendgrid / [email protected]

const result = await this.sgMail.send({
                personalizations: [{
                    to: this._to
                }],
                dynamicTemplateData: {
                    test: 'test' <== working (code in template: {{ test }})
                },
                templateId: this._templateId,
            });

我应该注意下面的代码不起作用:

const result = await this.sgMail.send({
                personalizations: [{
                    to: this._to,
                dynamicTemplateData: {
                    test: 'test' <== NOT working (code in template: {{ test }})
                }
                }],
                templateId: this._templateId,
            });

是的,您需要使用v6.3.1来支持dynamic_template_data。

在2018年12月11日星期二凌晨4点15分,Roy Milder [email protected]写道:

起初,它对我也不起作用。
我注意到我的版本有点过时了(我在使用
@ sendgrid / mail @ 6.2.1)
我已经更新了程序包,更改了代码,现在可以使用了:

使用@ sendgrid / [email protected]

const result =等待this.sgMail.send({
个性化设置:[{
到:this._to
}],
dynamicTemplateData:{
测试:“测试” <==工作正常(模板中的代码:{{测试}})
},
templateId:this._templateId,
});


您收到此邮件是因为有人提到您。
直接回复此电子邮件,在GitHub上查看
https://github.com/sendgrid/sendgrid-nodejs/issues/676#issuecomment-446166152
或使线程静音
https://github.com/notifications/unsubscribe-auth/ACL-sjFRFP8txUETNWFVLs0chW5yyL1Nks5u35OzgaJpZM4TOB5l

嗨,别忘了
不创建事务性模板,创建遗留模板并制作一个副本。
然后您要做的每个步骤都会起作用。

dynamicTemplateData

为我工作。 我拥有最新版本的SendGrid库,并且正在通过Firebase(函数)使用它。 现在的主要问题是缺乏TypeScript支持和文档。

@roymilder ,非常感谢。 您的回答让我感到震惊-动态模板的替代品还不清楚。
荣誉!

你好@OleksiiM

这个例子有帮助吗?

@ darren-dev,

感谢您的帮助! 关于缺乏TypeScript支持和文档的问题,您是指Firebase方面吗?

@uups

感谢您的帮助!

最诚挚的问候,

埃尔默

@thinkingserious ,是的。 谢谢。

@OleksiiM

你先去哪里看我试图弄清楚如何确保需要使用动态模板的人员转到我首先发送给您的链接。

@thinkingserious
这里开始。

它说:
“发送事务模板
您可以使用以下三种方式之一发送事务模板:
使用SMTP中继
在Web API v3邮件发送端点的templates参数中包括模板ID
在Web API v2邮件发送端点中使用x-smtpapi参数”

因此,我跳到“在Web API v3邮件发送端点的templates参数中包括模板ID”。

为什么不清楚:
最后一页显示了可能的“请求主体参数”列表,该列表显示了template_id,但未对“ dynamic_template_data”字段进行任何说明。 我尝试使用“个性化” +“替代” +“ template_id”,但它不起作用。
另外,由于我是Sendgrid的新用户,所以我不知道“旧”模板和“新”(动态)模板以及它们之间的区别(上面的sendgrid文档页面没有对此发表任何评论)。

感谢您询问并改进文档。 我希望其他人可以避免这种麻烦。

@thinkingserious,如果我可以添加:
我目前正在使用这些文档: https :

发送电子邮件的说明(https://sendgrid.api-docs.io/v3.0/mail-send)在个性化设置中根本没有提到dynamic_template_data

因此也不清楚。

希望能帮助到你!

@Whatthefoxsays ^^

感谢@OleksiiM和@roymilder! 感谢您对Twilio SendGrid社区的贡献:)

大家好,在这个问题上也花了几个小时。

而不是使用substitutions ,而是在主msg对象中使用dynamicTemplateData

const sgMail = require('@sendgrid/mail')

sgMail.setApiKey(process.env.SENDGRID_API_KEY)
sgMail.setSubstitutionWrappers('{{', '}}')

const msg = {
  to: '[email protected]',
  from: '[email protected]',
  templateId: 'd-27e7d954368c46519eddc806e5cf8156',
  dynamicTemplateData: {
    name: 'Some One',
    city: 'Denver',
  },
}

sgMail.send(msg)

参考:#691

根据该PR,当templateId是动态模板(带有d-前缀)时, substitutions根本不会发送,因此dynamicTemplateData应该使用对象。

他们在这里创建了一个问题

从两天的忙碌开始,我看到了这个,并开始使用它。

您好@Travotics

在找到上述内容之前,您从什么文档开始? 我正在努力改善这种体验。 谢谢!

最诚挚的问候,

埃尔默

@thinkingserious https://github.com/sendgrid/sendgrid-nodejs/blob/master/use-cases/kitchen-sink.md

没有dynamicTemplateData但确实有substitutionWrapperssubstitutions

除了更新文档外,更新类型接口MailData (https://github.com/sendgrid/sendgrid-nodejs/blob/master/packages/helpers/classes/ mail.d.ts#L117)。 对于不支持substitutions @ sendgrid / mail版本,他们应该期望使用省略了substititonssubstitutionWrappers字段的V2MailData参数。

嗨,在阅读@himanshupnt的评论后,我使用旧版模板进行了测试,并且可以正常工作。 谢谢@himanshupnt

后来,我找到了Sendgrid文档“带有动态事务模板的邮件发送” ,我已经开始使用新模板(不是旧模板)了。 我使用了相同的HTML模板,并且我的代码已更新为:

  const sgMail = require("@sendgrid/mail");
  sgMail.setApiKey(process.env.SENDGRID_API_KEY);
  sgMail.setSubstitutionWrappers("{{", "}}"); // Configure the substitution tag wrappers globally

  const msg = {
    from: "[email protected]",
    templateId: "d-27e7d954368c46519eddc806e5cf8156",
    personalizations: [
      {
        to: [
          {
            email: "[email protected]"
          }
        ],
        dynamic_template_data: {
          name: "Someone",
          city: "Somewhere",
          subject: "Hello new world"
        }
      }
    ]
  };

  sgMail.send(msg).catch(err => console.error(err.message));

请注意,我将“ substitutions”替换为“ dynamic_template_data”,并根据文档的要求将“ to”放置在“个性化设置”中。 而且我也将主题作为“ dynamic_template_data”之一,并且可以正常工作。 (将{{subject}}放在模板主题字段中)

谢谢。

非常感谢你做的这些! 我为此花费了大量时间进行调试。 :/

此页面是否有帮助?
0 / 5 - 0 等级