【发布时间】:2018-08-21 17:11:57
【问题描述】:
我的问题是关于使用 Mailchimp 3.0 API 和 PHP 将订阅者直接添加到我的邮件列表。
我使用的代码(见下文)按预期运行并添加了订阅者。但是,订阅者会收到一封选择加入的电子邮件。
代码来自这里:http://www.johnkieken.com/mailchimp-form-using-api-v3-0-and-jquery-ajax/
期望的行为是将订阅者直接添加到列表中,而不使用选择加入的电子邮件,并在他们已成功订阅的网站上提供消息。
我的服务器正在运行 PHP 5.3.16。
出于测试目的,我有 HTML 文件、Mailchimp API 包装器 (mailchimp.php) 和 subscribe.php 都驻留在同一目录中。
我不精通编码,所以希望有人能提供帮助。
HTML
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
</head>
<body>
<form id="signup" action="index.html" method="get">
First Name: <input type="text" name="fname" id="fname" />
Last Name: <input type="text" name="lname" id="lname" />
email Address (required): <input type="email" name="email" id="email" />
<input type="submit" id="SendButton" name="submit" value="Submit" />
</form>
<div id="message"></div>
<script src="jquery.min.js"></script>
<script>
$(document).ready(function() {
$('#signup').submit(function() {
$("#message").html("Adding your email address...");
$.ajax({
url: 'subscribe.php', // proper url to your "store-address.php" file
type: 'POST', // <- IMPORTANT
data: $('#signup').serialize() + '&ajax=true',
success: function(msg) {
var message = $.parseJSON(msg),
result = '';
if (message.status === 'pending') { // success
result = 'Success! Please click the confirmation link that will be emailed to you shortly.';
} else { // error
result = 'Error: ' + message.detail;
}
$('#message').html(result); // display the message
}
});
return false;
});
});
</script>
</body>
</html>
subscribe.php
<?php // for MailChimp API v3.0
include('MailChimp.php'); // path to API wrapper downloaded from GitHub
use \DrewM\MailChimp\MailChimp;
function storeAddress() {
$key = "mymailchimpAPIkey-us17";
$list_id = "mymailchimplistid";
$merge_vars = array(
'FNAME' => $_POST['fname'],
'LNAME' => $_POST['lname']
);
$mc = new MailChimp($key);
// add the email to your list
$result = $mc->post('/lists/'.$list_id.'/members', array(
'email_address' => $_POST['email'],
'merge_fields' => $merge_vars,
'status' => 'pending' // double opt-in
// 'status' => 'subscribed' // single opt-in
)
);
return json_encode($result);
}
// If being called via ajax, run the function, else fail
if ($_POST['ajax']) {
echo storeAddress(); // send the response back through Ajax
} else {
echo 'Method not allowed - please ensure JavaScript is enabled in this browser';
}
如果我将 subscribe.php 编辑为:
// add the email to your list
$result = $mc->post('/lists/'.$list_id.'/members', array(
'email_address' => $_POST['email'],
'merge_fields' => $merge_vars,
//'status' => 'pending' // double opt-in
'status' => 'subscribed' // single opt-in
)
);
我收到以下消息:
“错误:email@domain.com 已经是列表成员。使用 PUT 插入或更新列表成员。”
【问题讨论】: