所以,我有一个 http 端点,用于从 SparkPost 接收不同类型的事件(例如交付、退回、投诉、打开跟踪等)。一切正常,但我没有收到有关 Click 事件的任何帖子。到目前为止,这是我尝试过的:
private void sendEmail(String from, String[] recipients) throws SparkPostException {
TransmissionWithRecipientArray transmission = new TransmissionWithRecipientArray();
ArrayList<String> tags = new ArrayList<String>();
tags.add("tag #1");
tags.add("tag #2");
// Populate Recipients
List<RecipientAttributes> recipientArray = new ArrayList<RecipientAttributes>();
for (String recipient : recipients) {
RecipientAttributes recipientAttribs = new RecipientAttributes();
recipientAttribs.setAddress(new AddressAttributes(recipient));
recipientAttribs.setTags(tags);
recipientArray.add(recipientAttribs);
}
transmission.setRecipientArray(recipientArray);
// Populate Substitution Data
Map<String, Object> substitutionData = new HashMap<String, Object>();
substitutionData.put("link", "http://www.google.com?utm_campaign=test_campaign");
OptionsAttributes optionsAttributes = new OptionsAttributes();
optionsAttributes.setClickTracking(true); // THIS DOESN'T SEEM TO MAKE A DIFFERENCE
optionsAttributes.setOpenTracking(true);
transmission.setSubstitutionData(substitutionData);
transmission.setOptions(optionsAttributes);
transmission.setCampaignId("test_campaign");
Map<String, String> metadata = new HashMap<String, String>();
metadata.put("user_type", "test");
transmission.setMetadata(metadata);
transmission.setReturnPath("example@some-mail.com");
// Populate Email Body
TemplateContentAttributes contentAttributes = new TemplateContentAttributes();
contentAttributes.setFrom(new AddressAttributes(from));
contentAttributes.setSubject("Your subject content here.");
contentAttributes.setText("Your Text content here.");
contentAttributes.setHtml("<p>Your <b>HTML</b> content here. {{ link }}</p>");
transmission.setContentAttributes(contentAttributes);
transmission.setContentAttributes(contentAttributes);
// Send the Email
RestConnection connection = new RestConnection(this.client, getEndPoint());
Response response = ResourceTransmissions.create(connection, 0, transmission);
System.out.println("Transmission Response: " + response);
}
最佳答案
为了让SparkPost模板引擎只个性化http(s)?
网址,而不是包装像 mailto:a@b.com
这样的东西, 方案( http://
或 https://
)需要在模板中,而不是在替换数据中。这是一个例子:
模板:
This is a link to <a href="http://{{{myurl}}}">somewhere awesome</a>!
替换数据:
substitutionData.put("myurl", "www.google.com?utm_campaign=test_campaign");
这里实际上有三个变化 - 第二个是使用三重 curl {{{
而不是 {{
双 curl ,以避免 html 转义替换变量的内容。第三个是将 url 放在 anchor 标记内,因为 SparkPost 不会包装裸链接。
关于java - SparkPost - 没有收到点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37027054/