简体   繁体   English

SendGrid:模板和替代标签

[英]SendGrid: templates and substitution tags

I've created a sendgrid template in order to be able to build my emails according to users' information. 我创建了一个sendgrid模板,以便能够根据用户信息构建电子邮件。 By now, it's really straightsorward: 到现在为止,这很简单:

<html>
<body>
  <div>&lt;%body%&gt;</div>  
  <div>Hi there&nbsp;:username!</div>  
  <div>Please, click on here to complete Accoung Activation: :activation</div>  
  <div>Please, bear with us.</div>
</body>
</html>

As far I've been to figure out I'm able to replace tokens ( :username and :activation ). 到目前为止,我一直在寻找可以替换令牌( :username:activation )的方法。

Nevertheless, I don't quite understand how to build it on java. 不过,我不太了解如何在Java上构建它。 Up to now, I've been able to write this code in order to send an email with a template: 到目前为止,我已经能够编写以下代码以便发送带有模板的电子邮件:

String activationUri = "http://activation uri.sample.com/activation";
String address = "sample@sample.com";

Email from = new Email("no-reply@facetz.zone");
String subject = "Account activation mail request";
Email to = new Email(address);
Content content = new Content("text/plain", activationUri);
Mail mail = new Mail(from, subject, to, content);
mail.setTemplateId("7928c2b2-c5a9-4918-a035-db5b7aae532b");

SendGrid sg = new SendGrid("api_key");
Request request = new Request();
try {
  request.method = Method.POST;
  request.endpoint = "mail/send";
  request.body = mail.build();

  Response response = sg.api(request);
} catch (IOException ex) {
    throw MailGenerationException.create(address, ex);
}

As you can see I've set the templateId , nevertheless, I'm not able to get how to: 如您所见,尽管如此,我已经设置了templateId ,但我无法获得如何:

  1. Set template version. 设置模板版本。
  2. Add token substitutions. 添加令牌替换。

By other hand: 另一方面:

  1. Which's the difference between section tags and substitution tags and <%subject%> and <%body%> tags? section tagssubstitution tags以及<%subject%><%body%>标签之间有什么区别?

Please, I've really took a look on documentation. 拜托,我真的看过文档。 Up to now, I've not been able to understand everything I've posed. 到目前为止,我还无法理解我提出的所有内容。

I was looking to do something similar, but I couln't find a way to do it with one request. 我一直想做类似的事情,但是我找不到一种方法可以处理一个请求。

The template that will be used is always the "active" one, so, to choose a different version, you must first call the templates/versions endpoint and "activate" it. 将使用的模板始终是“活动的”模板,因此,要选择其他版本,必须首先调用模板/版本端点并“激活”它。

Assuming you're using the API version 3, then you would do somethins like this (before actually sending the email): 假设您使用的是API版本3,那么您将执行以下操作(在实际发送电子邮件之前):

Request request = new Request();
try {
  request.method = Method.PATCH;
  request.endpoint = "templates/" + templateId + "/versions/" + versionId;
  request.body = "{\"active\": \"1\"}";

  Response response = sg.api(request);
  if (response.status == 200)
    // success
  else
    // failure
} catch (IOException ex) {
    throw MailGenerationException.create(address, ex);
}

To retrieve the list of template versions, you need to call the templates endpoint ... then the use of a version becomes a little tedious xD. 要检索模板版本列表,您需要调用模板端点 ...然后使用版本会变得有点乏味。

For the substitutions, you must build a Personalization object: 对于替换,您必须构建一个Personalization对象:

Personalization obj = new Pesrsonalization();
obj.addSubstitution("tag", "value");
// Etc.

The Personalization class is very useful, as it can hold the recipients (CC, BCC and TO) and other data. 个性化类非常有用,因为它可以容纳收件人(CC,BCC和TO)和其他数据。

The <%body%> tag gets replaced by whatever you send in the mail.body, and the <%subject%> gets replaced by the subject set in the Personalization object (or the mail.subject). <%body%>标记将替换为您在mail.body中发送的内容,而<%subject%>则替换为在Personalization对象(或mail.subject)中设置的主题。 The only difference with any other tag is that these don't need to be set through the Personalization object. 与任何其他标签的唯一区别是不需要通过Personalization对象设置这些标签。

By the way, the subject can contain other tags tha will be replaced too. 顺便说一下,主题可以包含其他标签,这些标签也将被替换。

Hope this helps you. 希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM