我假设动态模板的版本是在发送电子邮件时指定模板的 id 和版本。调用v3 api时如何指定动态模板的版本?我正在使用 C# 客户端库。
我只是想为 SendGrid 设置一个试用电子邮件,这是我第一次使用它,所以我确定这很简单,但我似乎无法替换占位符数据。
我正在使用这样的 NodeJS 库:
sgMail.setApiKey(mailConfig.apiKey);
const msgConfig = {
to: email,
from: mailConfig.defaults.from,
templateId: mailConfig.templates.registrationConfirmation,
substitutions: {
'--displayName--': original.displayName,
'--companyName--': 'Hello world'
}
};
console.log('Sending: ', msgConfig);
// now send the registration confirmation email.
return sgMail.send(msgConfig).then(() => {
console.log('done.');
})
.catch((err) => {
console.error(JSON.stringify(err));
});
Run Code Online (Sandbox Code Playgroud)
在模板中有一个我使用可视化编辑器添加的文本块:
Hello --displayName--
We would love to take this opportunity to welcome you to the store.
from
--companyName--
Run Code Online (Sandbox Code Playgroud)
但是,当我运行测试以发送电子邮件时,它可以正常发送邮件,但不会替换占位符。
我在这里缺少什么?
我一直在使用 sendgrid-ruby gem 发送电子邮件。电子邮件的主题没有正确解码特殊字符。例如。为电子邮件发送此主题会How's it going在实际电子邮件中转换为此主题How's it going
我曾尝试将字符串编码为不同的格式,例如 ASCII、ISO_8859_1,但这些都不起作用。
@body_json['personalizations'][0]['dynamic_template_data'] = {
'email_title': @email_title,
'content': @description,
'subject': "How's it going"
}
SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']).client.mail._('send').post(request_body: @body_json)
Run Code Online (Sandbox Code Playgroud)
电子邮件主题应正确显示特殊字符,例如 ' & :
我<%datetime%>在 SendGrid 模板中定义了一个变量。我根据这个命名约定决定遵循已经放置<%subject%>的主题行。我在示例中看到了不同的变量命名约定:https : //github.com/sendgrid/sendgrid-csharp/blob/master/SendGrid/Example/Example.cs#L41使用-name-and -city-,而https://github.com/sendgrid /sendgrid-csharp/blob/master/SendGrid/Example/Example.cs#L157使用%name%和%city%。
我只是假设变量替换基于简单的模式匹配,因此这些示例的对应模板包含完全相同的字符串。到目前为止,无论出于何种原因,这对我都不起作用。
string sendGridApiKey = ConfigurationManager.AppSettings["SendGridApiKey"].ToString();
var sendGrid = new SendGridAPIClient(sendGridApiKey);
string emailFrom = ConfigurationManager.AppSettings["EmailFrom"].ToString();
Email from = new Email(emailFrom);
string subject = "Supposed to be replaced. Can I get rid of this somehow then?";
string emaiTo = ConfigurationManager.AppSettings["EmailTo"].ToString();
Email to = new Email(emaiTo);
Content content = new Content("text/html", "Supposed to be replaced by the template. Can I get …Run Code Online (Sandbox Code Playgroud) 在SendGrids上使用USE案例之后,github确实设法向我发送了带有正确模板的电子邮件,但是替换显然不起作用,并且在生成的电子邮件中留为空白。服务器端:
const sgmailer = require("@sendgrid/mail");
sgmailer.setApiKey(process.env.SENDGRID_API_KEY);
sgmailer.setSubstitutionWrappers('{{', '}}');
const msg = {
to: '...',
from: 'sender@example.org',
subject: 'Hello world',
text: 'Hello plain world!',
html: '<p>Hello HTML world!</p>',
templateId: '...',
substitutions: {
name: 'Some One',
city: 'Denver',
},
};
sgmailer.send(msg)
Run Code Online (Sandbox Code Playgroud)
模板中的HTML:
const sgmailer = require("@sendgrid/mail");
sgmailer.setApiKey(process.env.SENDGRID_API_KEY);
sgmailer.setSubstitutionWrappers('{{', '}}');
const msg = {
to: '...',
from: 'sender@example.org',
subject: 'Hello world',
text: 'Hello plain world!',
html: '<p>Hello HTML world!</p>',
templateId: '...',
substitutions: {
name: 'Some One',
city: 'Denver',
},
};
sgmailer.send(msg) …Run Code Online (Sandbox Code Playgroud)我正在尝试将我的API代码从Sendgrid v2更新到实际的Sendgrid v3,所以我的代码以前看起来像这样:
public void sendCreatedUserEmail(User user) {
Email from = new Email(FROM);
from.setName(EMAIL_NAME);
String subject = "Hello" + user.getName();
Email to = new Email(user.getEmail());
Content content = new Content("text/html", "Something");
Mail mail = new Mail(from, subject, to, content);
mail.personalization.get(0).addSubstitution("{name1}", user.getName());
mail.personalization.get(0).addSubstitution("{name2}", user.getName());
mail.setTemplateId(USER_TEMPLATE_ID);
SendGrid sg = new SendGrid(SENDGRID_API_KEY);
Request request = new Request();
try {
request.setMethod(Method.POST);
request.setEndpoint("mail/send");
request.setBody(mail.build());
Response response = sg.api(request);
} catch (IOException ex) {
logger.error(ex);
}
}
Run Code Online (Sandbox Code Playgroud)
经过几个小时的研究,我将v3更改为:(为了确保画面更清晰,我将所有内容分开)
public void sendCreatedUserEmail(User user) {
Mail mail = …Run Code Online (Sandbox Code Playgroud) 有一个预定义的营销模板,模板的相关部分如下所示:
嗨 [%first_name | 亲爱的粉丝%],xxxxxx
sendgrid 模板变量替换有很多不同的语法,这非常令人困惑。例如,我见过 [%variable_name%]、{{variable_name}}、-variable_name- 和 <%variable_name%>
如何将替换/个性化/模板数据(只是列出我见过的似乎指代同一事物的不同名称)从 C# 客户端传递到模板?