接入教程
1. 注册Mailchimp账号并获取API密钥
2. 创建邮件列表并设置营销活动模板
3. 调用发送接口测试邮件功能
4. 集成交易邮件发送到应用流程
5. 监控邮件发送数据并优化效果
创建邮件营销活动
python
import requests
url = "https://api.mailchimp.com/3.0/campaigns"
headers = {
"Authorization": "apikey YOUR_API_KEY"
}
payload = {
"type": "regular",
"recipients": {
"list_id": "YOUR_LIST_ID"
},
"settings": {
"subject_line": "Welcome Offer",
"from_name": "Your Brand",
"reply_to": "contact@yourbrand.com"
}
}
response = requests.post(url, json=payload, headers=headers)
print(response.status_code)
print(response.json())
添加联系人到邮件列表
php
<?php
$api_key = 'YOUR_API_KEY';
$server_prefix = substr($api_key, strpos($api_key, '-') + 1);
$url = "https://$server_prefix.api.mailchimp.com/3.0/lists/YOUR_LIST_ID/members";
$data = [
'email_address' => 'user@example.com',
'status' => 'subscribed',
'merge_fields' => [
'FNAME' => 'John',
'LNAME' => 'Doe'
]
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "apikey:$api_key");
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>
发送交易邮件
javascript
const axios = require('axios');
const sendTransactionalEmail = async () => {
try {
const response = await axios.post(
'https://mandrillapp.com/api/1.0/messages/send',
{
key: 'YOUR_API_KEY',
message: {
from_email: 'noreply@yourdomain.com',
to: [{ email: 'customer@example.com' }],
subject: 'Order Confirmation',
html: '<p>Your order has been confirmed.</p>',
text: 'Your order has been confirmed.'
}
}
);
console.log(response.data);
} catch (error) {
console.error(error);
}
};
sendTransactionalEmail();
常见问题
如何获取Mailchimp API密钥?
登录Mailchimp账户后,进入账户设置中的"API密钥"部分即可生成新的API密钥。请注意保管好密钥,避免泄露。
API调用速率限制是多少?
Mailchimp API有速率限制,通常为每小时最多10个请求。具体限制可能因账户类型而异,建议查阅官方文档获取最新信息。
发送交易邮件和营销邮件使用同一个API吗?
不完全相同。营销邮件主要通过Mailchimp核心API(api.mailchimp.com)管理,而交易邮件通常通过Mandrill API(mandrillapp.com)发送,两者可能需要不同的API密钥和端点。
Aitishiku.com