如果你想在 SparkPost 服务器上创建一个电子邮件列表,你可以使用这个 API:https ://developers.sparkpost.com/api/recipient-lists.html#recipient-lists-create-post这是一个 PHP 示例:
<?php
$request = new HttpRequest();
$request->setUrl('https://api.sparkpost.com/api/v1/recipient-lists');
$request->setMethod(HTTP_METH_POST);
$request->setQueryData(array(
'num_rcpt_errors' => '3'
));
$request->setHeaders(array(
'Cache-Control' => 'no-cache',
'Authorization' => $YOUR_API_KEY_HERE // put your API key here
));
// You will need to put PHP code that reads from your DB and puts Your recipients in that array below. You only need the address->name and address-email bit if you don't have additional metadata
$request->setBody('{
"id": "unique_id_4_graduate_students_list",
"name": "graduate_students",
"description": "An email list of graduate students at UMBC",
"attributes": {
"internal_id": 112,
"list_group_id": 12321
},
"recipients": [
{
"address": {
"email": "wilmaflin@example.com",
"name": "Wilma"
},
"tags": [
"greeting",
"prehistoric",
"fred",
"flintstone"
],
"metadata": {
"age": "24",
"place": "Bedrock"
},
"substitution_data": {
"favorite_color": "SparkPost Orange",
"job": "Software Engineer"
}
},
{
"address": {
"email": "abc@example.com",
"name": "ABC"
},
"tags": [
"driver",
"flintstone"
],
"metadata": {
"age": "52",
"place": "MD"
},
"substitution_data": {
"favorite_color": "Sky Blue",
"job": "Driver"
}
},
{
"address": {
"email": "fred.jones@example.com",
"name": "Grad Student Office",
"header_to": "grad-student-office@example.com"
},
"tags": [
"driver",
"fred",
"flintstone"
],
"metadata": {
"age": "33",
"place": "NY"
},
"substitution_data": {
"favorite_color": "Bright Green",
"job": "Firefighter"
}
}
]
}');
try {
$response = $request->send();
echo $response->getBody();
} catch (HttpException $ex) {
echo $ex;
}
假设您想向存储在 SparkPost 中的收件人列表发送电子邮件,这是在 PHP 中执行此操作的一种方法。
为此,您需要在此处https://app.sparkpost.com/lists/recipients在 SparkPost 中创建一个“收件人列表”,并在下面的请求中提供该列表的 ID。您还需要一个允许 REST 注入的 API 密钥,可以在此处创建https://app.sparkpost.com/account/credentials您还需要一个有效的发送域,我假设您已经在 SparkPost 中设置了它。
<?php
$request = new HttpRequest();
$request->setUrl('https://api.sparkpost.com/api/v1/transmissions');
$request->setMethod(HTTP_METH_POST);
$request->setHeaders(array(
'Cache-Control' => 'no-cache',
'Authorization' => $YOUR_SPARKPOST_API_KEY . // Put your "real" API key here or better set a variable and use that
));
$request->setBody('{
"options": {
"open_tracking": true,
"click_tracking": true
},
"campaign_id": "test_campaign",
"recipients": {
"list_id": "unique_id_4_graduate_students_list" // Put your List ID here from here https://app.sparkpost.com/lists/recipients
},
"content": {
"from": {
"email": "test@test.example.com", // Change this to your proper from address
"name": "John Doe"
},
"subject": "simple subject",
"text": "simple text content",
"html": "<b>Your HTML Content</b>"
}
}
');
try {
$response = $request->send();
echo $response->getBody();
} catch (HttpException $ex) {
echo $ex;
}
这里还有一些提示,可以让您更快地进行操作。这里有一个 SparkPost PHP 库:https ://github.com/SparkPost/php-sparkpost
此外,对于更多“实时”问题和答案,您可以在此处加入社区 Slack:http ://slack.sparkpost.com/ 加入后,请查看#php 频道。
如果您想熟悉 API,可以尝试 PostMan 资源。这是一篇很好的博客文章,可以帮助您前进:https : //www.sparkpost.com/blog/how-to-run-sparkpost-postman/ 一旦您完成了,PostMan 可以生成最常用语言的代码来帮助您获得走得很快。