我无法找出为什么我的表单没有将信息发送到 Mailchimp API



我现在很头疼。并且找不到我的代码不工作的原因。我需要把表格连接到Mailchimp。我输入了正确的API密钥/列表ID。我错过什么了吗#帮助

我的JS知识一般。我用我找到的一些模板创建了这个代码。

非常感谢:D

<?php
use PHPMailerPHPMailerPHPMailer;
// Mailchimp
if(!empty($_POST['newsletter']) && isset($_POST['submit'])){
$fname = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
if(!empty($email) && !filter_var($email, FILTER_VALIDATE_EMAIL) === false){
// MailChimp API credentials
$apiKey = 'xxxxxxxxxxxxxxxxxxxxxx-us15';
$listID = 'b8c5013790';
// 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'        => 'pending',
'merge_fields'  => [
'FNAME'     => $fname,
'PHONE'     => $phone
]
]);
// 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);
// store the status message based on response code
if ($httpCode == 200) {
$_SESSION['msg'] = '<p style="color: #34A853">You have successfully subscribed to CodexWorld.</p>';
} else {
switch ($httpCode) {
case 214:
$msg = 'You are already subscribed.';
break;
default:
$msg = 'Some problem occurred, please try again.';
break;
}
$_SESSION['msg'] = '<p style="color: #EA4335">'.$msg.'</p>';
}
}else{
$_SESSION['msg'] = '<p style="color: #EA4335">Please enter valid email address.</p>';
}
}
// end mailchimp
if  ( isset ($_POST['name']) and isset ($_POST['email']) and isset ($_POST['message']) ){
$url = $_POST['url'];
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$rappel = $_POST['period'];
$message = $_POST['message'];
$address = $_POST['address'];
$ville = $_POST['city'];
$net = $_POST['net'];
$tv = $_POST['tv'];
$tel = $_POST['tel'];
$file_attach  = $_FILES['file']['tmp_name'];
$file_attach_name = $_FILES['image']['name'];
$msg = '';
if (array_key_exists('email', $_POST)) {
date_default_timezone_set('Etc/UTC');
require 'vendor/autoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "xxx@xxx.com";
$mail->Password = "";
$mail->setFrom('xxx@xxx.com');
$mail->addReplyTo('$email', '$name');
$cnt=count($_FILES['file']['name']);
if($cnt > 0){
for($i=0;$i<$cnt;$i++){
$mail->AddAttachment($_FILES['file']['tmp_name'][$i], $_FILES['file']['name'][$i],'base64',$_FILES['file']['type'][$i]);
}
}
if ($mail->addReplyTo($_POST['email'], $_POST['name'])) {
//Set the subject line
$mail->Subject = 'Message de votre site ' . $client . ' - ' . $name;
//Keep it simple - don't use HTML
$mail->isHTML(true);
$mail->CharSet = 'UTF-8';
//Build a simple message body
$mail->Body = file_get_contents('rd-mailform.tpl');
$mail->Body = str_replace(
["<!-- #{StartName} -->", "<!-- #{Name} -->"],
["Nom:", $_POST['name']],
$mail->Body);
$mail->Body = str_replace(
["<!-- #{StartPhone} -->", "<!-- #{Phone} -->"],
["T&#233;l&#233;phone:", $_POST['phone']],
$mail->Body);
$mail->Body = str_replace(
["<!-- #{StartEmail} -->", "<!-- #{Email} -->"],
["Email:", $_POST['email']],
$mail->Body);
$mail->Body = str_replace(
["<!-- #{StartRappel} -->", "<!-- #{Rappel} -->"],
["P&#233;riode de rappel:", $_POST['period']],
$mail->Body);
$mail->Body = str_replace(
["<!-- #{MessageStart} -->", "<!-- #{Message} -->"],
["Message:", $_POST['message']],
$mail->Body);
$mail->Body = $mail->Body."Adresse IP de l'expéditeur: $ssIPAddress / Localisation <a href='http://ipinfodb.com/ip_locator.php?ip=$ssIPAddress'>ici</a>, <a href='http://www.ip-tracker.org/ip-to-location.php?ip=$ssIPAddress'>ici</a> et <a href='http://whatismyipaddress.com/ip/$ssIPAddress'>ici</a></p>";
//Send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
}
print "<meta http-equiv="refresh" content="0;URL=success.php">";
}
}
?>

我认为我的API密钥或API中心有问题(我在文档中看到我可能需要在url前面添加us15。我应该吗?

合并字段很好。表单和Mailchimp之间的链接以前是有效的,所以我不知道发生了什么。谢谢你的帮助。

我找到了它。这是因为我必须更改"挂起";状态为";认购";。

最新更新