1

因此,我有一个 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);
}
4

1 回答 1

4

为了让 SparkPost 模板引擎只个性化http(s)?url,而不是包装类似的东西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 不会包装裸链接。

于 2016-05-06T15:20:22.280 回答