我试图在我的网站中使用Mailchimp API
构建自定义表单。我设法编写了一个PHP
脚本,它将我的用户从Mailchimp仪表板中添加到邮件列表中。不过,我有问题发送给他们一个自动回复电子邮件。Mailchimp API表单未发送最终欢迎电子邮件
这是我HTML
形式:
<form method='post' action='<?= get_template_directory_uri() ?>/mailchimp.php'>
<input type='text' name='email'/>
<input type='submit' name='submit' value='Subscribe'/>
</form>
这是我的代码来处理请求Mailchimp和将用户添加到列表:
session_start();
if(isset($_POST['submit'])){
$email = $_POST['email'];
if(!empty($email) && !filter_var($email, FILTER_VALIDATE_EMAIL) === false){
// MailChimp API credentials
$apiKey = 'xxx';
$listID = 'xxx';
// MailChimp API URL
$memberID = md5(strtolower($email));
$dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
$url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listID . '/members/' . $memberID;
// member information
$json = json_encode([
'email_address' => $email,
'status' => 'subscribed'
]);
// send a HTTP POST request with curl
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo "<h1>$httpCode</h1>";
return $httpCode;
}
}
我读了你不能发送最后欢迎发送电子邮件给任何人,除非他们被标记为subscribed
到您的清单。我明白Mailchimp强制执行双重选择启用策略,除非重写使用API通过设置用户状态订阅,因为我已经做了如下:
// member information
$json = json_encode([
'email_address' => $email,
'status' => 'subscribed'
]);
,但由于某种原因,我还没有在订阅列表中接收电子邮件。有人会对我做错什么有任何建议吗?
我从Mailchimp的后端确保最终欢迎电子邮件已设置。