Reach

Grow

Manage

Automate

Reach

Grow

Manage

Automate

高级电子邮件模板

2016年3月25日

电子邮件

1 min read

高级电子邮件模板

2016年3月25日

电子邮件

1 min read

高级电子邮件模板

此帖针对希望最大限度发挥 SparkPost 电子邮件模板功能的开发人员。假设您能够舒适地阅读 JSON 内容并遵循基本的编程流程。随着可能您不熟悉的术语的引入,例如 RFC 5322,文本与其来源引用相链接。

Business in a box.

探索我们的解决方案。

本文是针对希望充分利用SparkPost 的电子邮件模板功能的开发人员撰写的。假设您已经熟悉阅读 JSON 内容和遵循基本的编程流程。当引入可能对您来说新的术语时,如RFC 5322,文本会链接到其来源参考。理解这些后,让我们直接开始。

SparkPost 的 模板传输功能使发送电子邮件变得简单。这些功能为文本HTML内容提供了一种抽象形式,这意味着大多数情况下无需直接编码由RFC 5322定义的原始电子邮件格式,此格式以前称为(RFC 822)。但有时您可能希望创建更复杂的消息,这些消息具有其他通过 SparkPost 的 RESTful 接口未直接公开的多用途互联网邮件扩展(MIME)部分。




简化电子邮件撰写

首先,让我们回顾一下发送电子邮件的理想场景。使用传输端点提供文本HTML内容。在幕后,SparkPost 负责组合有效的 RFC 5322 电子邮件。SparkPost 将substitution_data的替换变量插入文本和 HTML 内容中。这是为每个收件人生成自定义内容的强大方式,共用模板中实现。

以下是一个包含 HTML 和文本内容以及 substitution_data 的传输示例。

{  "options":{    "open_tracking":true,    "click_tracking":true  },  "campaign_id":"christmas_campaign",  "return_path":"bounces-christmas-campaign@domain.com",  "metadata":{    "user_type":"students"  },  "substitution_data":{    "sender":"Big Store Team"  },  "recipients":[    {      "return_path":"123@bounces.domain.com",      "address":{        "email":"wilma@domain.com",        "name":"Wilma Flintstone"      },      "tags":[        "greeting",        "prehistoric",        "fred",        "flintstone"      ],      "metadata":{        "place":"Bedrock"      },      "substitution_data":{        "customer_type":"Platinum"      }    }  ],  "content":{    "from":{      "name":"Fred Flintstone",      "email":"fred@domain.com"    },    "subject":"Big Christmas savings!"    "reply_to":"Christmas Sales <sales@domain.com>",    "headers":{      "X-Customer-Campaign-ID":"christmas_campaign"    },    "text":"Hi {{address.name}} \nSave big this Christmas in your area {{place}}! \nClick http://www.mysite.com and get huge discount\n Hurry, this offer is only to {{user_type}}\n {{sender}}",    "html":"<p>Hi {{address.name}} \nSave big this Christmas in your area {{place}}! \nClick http://www.mysite.com and get huge discount\n</p><p>Hurry, this offer is only to {{user_type}}\n</p><p>{{sender}}</p>"  }}

 

替换数据数组

许多人意识到 SparkPost 的传输和模板端点可以在电子邮件头和正文中进行简单内容替换。但是很多人忽视了提供条件内容或数据数组的能力,这些数据也可以被替换。您还可以为每个收件人提供独特内容。在此示例中,发送了一个包含每位收件人唯一链接的数组。

这是通过提供一个 JSON 数据数组实现的,该数据将被填充到电子邮件正文中。提供数据后,SparkPost 将使用模板中的逻辑来填充它。

在这个例子中,SparkPost 将寻找名为“files_html”的替换数据,并对数组中的每个元素执行“for each”。它将创建一个包含“files_html”元素中“file”值的行。请注意“loop_var.file”周围的三重大括号。这是因为数组的每个元素都包含 HTML,我们需要告诉服务器原样使用它而不是转义。文本部分将是一个简单的文本标签和文件的 URL。




<table>    {{each files_html}}        <tr><td>{{{loop_var.file}}}</td></tr>    {{end}}</table>

以下是完整的工作示例:

{    "recipients":[        {            "address":{                "email":"recipient1@domain.com"            },            "substitution_data":{                "files_html":[                    {                        "file":"<a href=\"http://domain.com/file1a.txt\">File 1a Description</a>"                    },                    {                        "file":"<a href=\"http://domain.com/file2a.txt\">File 2a Description</a>"                    }                ],                "files_plain":[                    {                        "file":"File 1a -- http://domain.com/file1a.txt"                    },                      {                        "file":"File 2a -- http://domain.com/file2a.txt"                    }                ]                }        },        {            "address":{                "email":"recipient2@domain.com"            },            "substitution_data":{                "files_html":[                    {                        "file":"<a href=\"http://domain.com/file1b.txt\">File 1b Description</a>"                    },                    {                        "file":"<a href=\"http://domain.com/file2b.txt\">File 2b Description</a>"                    }                ],                "files_plain":[                    {                        "file":"File 1b -- http://domain.com/file1b.txt"                    },                      {                        "file":"File 2b -- http://domain.com/file2b.txt"                    }                ]                }         }     ],     "return_path":"chris@test.domain.com",     "content":{         "from":{             "name":"chris@test.domain.com",             "email":"chris@test.domain.com"         },         "html":"<b>您的文件:</b><br>\n<table>\n{{each files_html}}    <tr><td> {{{loop_var.file}}} </td></tr>\n{{ end }} </table>\n\n",         "text":"您的文件:\n{{each files_plain}} {{loop_var.file}} \n{{ end }} \n\n\n\n",         "subject":"用 SparkPost 发送邮件很有趣"     } }

专业提示:在代码中建议将视图标记与数据分开,但此示例的目的是尽可能简单易懂,所以我们创建了两个数组。一个数组用于 HTML 部分,另一个用于文本部分。在生产环境中,通常会有一组数据并在模板代码中编写逻辑。




传输功能中的附件

传输端点还提供了发送附件的抽象。您将看到附件被指定在content.attachments数组中,其中数组中的每个对象描述一个单独的附件项。与之前一样,SparkPost 将负责编码文本HTML替换及迭代通过附件数组来进行编码,以生成格式正确的电子邮件。

最佳实践表明,除非为服务明确要求,否则最好避免发送附件。

name类型MIME 类型附件的文件名数据Base64编码的文件数据

传输内容语法中的附件示例如下:

"content":{     "attachments":[         {             "type":"audio/mp3",             "name":"voicemail.mp3",             "data":"TVAzIERhdGEK"         }     ] }

您也可以在传输中发送“内联图像”。这些与附件非常相似,并在content.inline_images数组中指定,其中每个inline_image对象类似于上面的附件对象。

模板中的附件

现在我们有了使用传输端点发送附件的适当背景,让我们看看如何通过模板做到这一点。在撰写本文时,没有如内联传输中找到的附件抽象。有人可能会得出结论认为不能创建包含附件的模板。您部分地是正确的,但有一种解决方法,尽管您将无法隔离 RFC 5322 格式。

您可以通过自己编码 RFC 5322 内容来实现模板的附件,其中包含附件。好消息是您仍然可以在电子邮件头部HTML文本部分中使用替换数据。请注意,此类型的模板将替换限制为头部和第一个HTML文本部分。

以下是执行过程的一个示例。




RFC822 电子邮件

使用您想要的替换数据创建 RFC 5322 电子邮件。我在邮件客户端中创建了此邮件并发送给自己。一旦收到,我复制源并替换我要动态替换的字段。

MIME-Version: 1.0 Reply-To: {{replyto}} Subject: {{subject}} From: {{from}} To: {{address.email}} Content-Type: multipart/mixed; boundary=001a113c48b0b89d92052d3051da   --001a113c48b0b89d92052d3051da Content-Type: multipart/alternative; boundary=001a113c48b0b89d89052d3051d8   --001a113c48b0b89d89052d3051d8 Content-Type: text/plain; charset=UTF-8   带*文本附件*的邮件。   {{body2}}   --001a113c48b0b89d89052d3051d8 Content-Type: text/html; charset=UTF-8   <div dir="ltr"><div>带 <i>文本附件</i>的邮件。</div>   {{body1}} </div>   --001a113c48b0b89d89052d3051d8-- --001a113c48b0b89d92052d3051da Content-Type: text/plain; charset=US-ASCII; name="myfile.txt" Content-Disposition: attachment; filename="myfile.txt" Content-Transfer-Encoding: base64 X-Attachment-Id: f_ild455ce0   VGhpcyBpcyBteSBzaW1wbGUgdGV4dCBmaWxlLgo= --001a113c48b0b89d92052d3051da--

在此消息的最后一个 MIME 部分,您将看到Content-Disposition: attachment; filename=myfile.txt”。这就是定义文件名称的地方。您的附件内容肯定会复杂得多,但此示例努力保持简单。




存储模板

一旦您有一个有效的 RFC 5322 电子邮件,可以使用email_rfc822形式的模板端点存储它,而不是使用文本HTML字段。以下是该消息的content的示例:

{    "content":{        "email_rfc822":"MIME-Version: 1.0\nReply-To: {{replyto}}\nSubject: {{subject}}\nFrom: {{from}}\nTo: {{address.email}}\nContent-Type: multipart/mixed; boundary=001a113c48b0b89d92052d3051da\n\n--001a113c48b0b89d92052d3051da\nContent-Type: multipart/alternative; boundary=001a113c48b0b89d89052d3051d8\n\n--001a113c48b0b89d89052d3051d8\nContent-Type: text/plain; charset=UTF-8\n\n带*文本附件*的邮件。\n\n{{body2}}\n\n--001a113c48b0b89d89052d3051d8\nContent-Type: text/html; charset=UTF-8\n\n<div dir=\"ltr\"><div>带<i>文本附件</i>的邮件。</div>\n\n{{body1}}\n</div>\n\n--001a113c48b0b89d89052d3051d8--\n--001a113c48b0b89d92052d3051da\nContent-Type: text/plain; charset=US-ASCII; name=\"myfile.txt\"\nContent-Disposition: attachment; filename=\"myfile.txt\"\nContent-Transfer-Encoding: base64\nX-Attachment-Id: f_ild455ce0\n\nVGhpcyBpcyBteSBzaW1wbGUgdGV4dCBmaWxlLgo=\n--001a113c48b0b89d92052d3051da--"    },    "name":"_TMP_TEMPLATE_TEST"}

当请求完成时,SparkPost 将返回您的新模板的唯一标识符。例如xxxxxxx




发送模板

好消息是,创建 RFC 5322 内容是最难的部分。从这里开始,使用 SparkPost 发送该模板与发送任何其他模板完全一样。

以下是如何发送该模板并填充替换数据:

{    "campaign_id":"MyCampaign",    "return_path":"myReturnPath@yourdomain.com",    "substitution_data":{        "replyto":"myReplyToh@yourdomain.com",        "from":"MyFrom@yourdomain.com",        "subject":"my subject",       &nip;"body1":"HTML 部分的额外内容",        "body2":"文本部分的额外内容"    },  &nip;"recipients":[      &nip;{ &nip;&nip;&nip;"substitution_data":{}, &nip;&nip;&nip;"address":{&nbnip;&nip;&nip;&nip;&nip;"email":"test1@domain.com",&nbnip;&nip;&nip;&nip;&nip;"name":"test1" &nip;&nip;&nip;&nip;}&nbnip;&nip;&nip;&nip;&nip;} &nbnip;&nip;&nip;&nip;] &nbsnip;&nbnip; "content":{  &nip;&nip;&nip;"template_id":"xxxxxxx",  &nip;&nip;&nip;"use_draft_template":true }




来自邮件客户端 API 的模板

如果您使用的编程语言中有用于撰写电子邮件的库,您可以使用它来编程创建模板或甚至在线发送消息。此处是一个使用 JavaMail 通过 SparkPost 传输端点执行此操作的示例。此方法应能轻松翻译为 PHP 或您选择的编程语言。

/_* * 使用 SparkPost RESTful 接口的 JavaMail MIME 消息示例 _/ public class App extends SparkPostBaseApp {     public static void main(String[] args) throws Exception {         Logger.getRootLogger().setLevel(Level.DEBUG);         ;;;        App app = new App();         app.runApp();     } //    private void runApp() throws Exception {         Message message = createMultipartMessage();         // 将JavaMail消息转换为传输字符串        String rfc822Content = getMessageAsString(message);          // 在内容中添加TO和From字段,这将从SparkPost替换数据进行的数据        rfc822Content = "To: {{address.email}}\r\nFrom: {{from}}\r\n" + rfc822Content; &bnational;&limitsSystem.out.println(connection.getResponseEntity(contextEntity));}}}

==<== 提示二大小

把Johnny's乐意做自己的唯一眼和暗的眼的孩子的唯一眼和在使焗, Johnny只在对自己唯一的孩子和使焗中, Johnny只是独眼者随时也会在孩子心有欲无悔随时随地随时的事时无法拒绝为他选择换做惟一当一颗星为自己选择会到而成为天空中的星星在她引导进入, 一片会为he-d说 He-d astronauts为我们选择出he-d的最亲的, Johnny_d.jslighters in boot, everydast in steady on cors, Johnny_d.htmle and more Customs still passionates, he-d。

总结

现在您已了解如何使用 SparkPost 发送几乎所有复杂度的电子邮件,您可以查看“SparkPost 支持 Apple Watch 发送邮件”,或查看替换语法,它可以在您的模板或传输内容中以“if then else”、“表达式条件”、或者“数组迭代”的方式使用。

让我们为您联系Bird专家。
在30分钟内见证Bird的全部威力。

通过提交,您同意 Bird 可能会就我们的产品和服务与您联系。

您可以随时取消订阅。查看Bird的隐私声明以获取有关数据处理的详细信息。

Newsletter

通过每周更新到您的收件箱,随时了解 Bird 的最新动态。

让我们为您联系Bird专家。
在30分钟内见证Bird的全部威力。

通过提交,您同意 Bird 可能会就我们的产品和服务与您联系。

您可以随时取消订阅。查看Bird的隐私声明以获取有关数据处理的详细信息。

Newsletter

通过每周更新到您的收件箱,随时了解 Bird 的最新动态。

让我们为您联系Bird专家。
在30分钟内见证Bird的全部威力。

通过提交,您同意 Bird 可能会就我们的产品和服务与您联系。

您可以随时取消订阅。查看Bird的隐私声明以获取有关数据处理的详细信息。

R

Reach

G

Grow

M

Manage

A

Automate

Newsletter

通过每周更新到您的收件箱,随时了解 Bird 的最新动态。