SparkPost - 不接收Click事件

时间:2016-05-04 11:53:23

标签: java sparkpost

所以,我有一个http端点,用于接收来自SparkPost的不同类型的事件(例如Delivery,Bounce,Complaint,Open Track,...)。一切正常,但我没有收到有关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);
}

1 个答案:

答案 0 :(得分:3)

为了使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");

这里实际上有三个变化 - 第二个是使用三重曲线{{{而不是{{双曲线,以避免html转义替换变量的内容。第三个是将url放在一个锚标签中,因为SparkPost不会包裹裸链接。