我不明白为什么我的表单不向 Mailchimp API 发送信息
我现在很头疼。并且无法找出为什么我的代码不起作用。 我需要将表单连接到 Mailchimp。 我输入了正确的 API 密钥/列表 ID。 我错过了什么吗? #help
我的 JS 知识很一般。我根据找到的一些模板创建了此代码。
非常感谢 :D
<?php
use PHPMailer\PHPMailer\PHPMailer;
// 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 = "[email protected]";
$mail->Password = "";
$mail->setFrom('[email protected]');
$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éléphone:", $_POST['phone']],
$mail->Body);
$mail->Body = str_replace(
["<!-- #{StartEmail} -->", "<!-- #{Email} -->"],
["Email:", $_POST['email']],
$mail->Body);
$mail->Body = str_replace(
["<!-- #{StartRappel} -->", "<!-- #{Rappel} -->"],
["Pé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 之间的链接之前工作过,所以不知道发生了什么。 感谢您的帮助。
I'm breaking my head right now. And can't find out why my code isn't working.
I need to connect a form to Mailchimp.
I put the right API key/list ID.
Did I miss something? #help
My JS knowledge is average. I created this code from some templates I found.
Thanks a lot :D
<?php
use PHPMailer\PHPMailer\PHPMailer;
// 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 = "[email protected]";
$mail->Password = "";
$mail->setFrom('[email protected]');
$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éléphone:", $_POST['phone']],
$mail->Body);
$mail->Body = str_replace(
["<!-- #{StartEmail} -->", "<!-- #{Email} -->"],
["Email:", $_POST['email']],
$mail->Body);
$mail->Body = str_replace(
["<!-- #{StartRappel} -->", "<!-- #{Rappel} -->"],
["Pé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\">";
}
}
?>
I thought there were a problem with my API key or the API center (i saw on documentation that I may have to add us15 in front of url. Should I?
Merge fields are good. Link between the form and Mailchimp worked before, so I don't know what happened.
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了。这是因为我必须将“待处理”状态更改为“已订阅”。
I found it. It's because I had to change "pending" status to "subscribed".