如何通过 MailChimp 3.0 api 发送电子邮件?
Posted
技术标签:
【中文标题】如何通过 MailChimp 3.0 api 发送电子邮件?【英文标题】:How to send email through MailChimp 3.0 api? 【发布时间】:2015-10-21 12:12:54 【问题描述】:我正在尝试通过 php 中的 mailchimp api 3.0 版发送电子邮件,但我没有运气。这是我的代码:
$postString = '
"message":
"html": "this is the emails html content",
"text": "this is the emails text content",
"subject": "this is the subject",
"from_email": "xxx@dyyy.sk",
"from_name": "John",
"to_email": "aaa.bbb@gmail.com",
"to_name": "Anton",
"track_opens": false,
"track_clicks": false
';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->api_endpoint);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'drewm:'.$this->api_key);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/vnd.api+json', 'Content-Type: application/vnd.api+json'));
curl_setopt($ch, CURLOPT_USERAGENT, 'DrewM/MailChimp-API/3.0 (github.com/drewm/mailchimp-api)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verify_ssl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
$result = curl_exec($ch);
echo $result;
我做错了什么?
【问题讨论】:
How to create a campaign using mailchimp v3.0的可能重复 【参考方案1】:您不能像使用 v1 那样从 API v3 发送随机电子邮件。现在您只能按照 LamaDelRay 的说明在 MailChimp 中发送之前创建的活动。
【讨论】:
【参考方案2】:我的代码在这里为 test 邮件工作,但基本上它对于非测试是一样的,只是 url 正在改变。
<?php
$apiKey = "your api key found easily in your account";
$campaignId = "your campaign id, you need to create one. Use the playground to get the id";
$memberId = md5(strtolower("membermail"));
$dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
$url = 'https://'. $dataCenter . '.api.mailchimp.com/3.0/campaigns/' . $campaignId .'/actions/test';
$jsonEmail = '"test_emails":["the mail you want to send thing sat"],"send_type":"html"';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, 'apikey:'.$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_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonEmail);
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
?>
如果一切顺利,你应该有一个字符串“0”。稍等一下,邮件就会发送出去。
祝你好运!
【讨论】:
要更改哪个网址以及在哪里? @LamadelRay $url 的行是用你的 apikey 和你的campaignid 做的 我可以将表单数据传递到此邮件 我们如何动态设置电子邮件和电子邮件正文,就像在 mailchimp 中设置活动时,您必须设置主题和正文。它似乎是静态的。 @AtaulMustafa 可以在json中指定主题。对于身体来说,这里和那里都有一些技巧;这有点难以解释,值得提问。【参考方案3】:<?php
//require_once('mailchimpint/mcapi/inc/MCAPI.class.php');
$apikey = 'Your api';
$to_emails = array('to email 1', 'toemail 2');
$to_names = array('Name 1', 'Name 2');
$message = array(
'html'=>'Yo, this is the <b>html</b> portion',
'text'=>'Yo, this is the *text* portion',
'subject'=>'This is the subject',
'from_name'=>'Me!',
'from_email'=>'',
'to_email'=>$to_emails,
'to_name'=>$to_names
);
$tags = array('WelcomeEmail');
$params = array(
'apikey'=>$apikey,
'message'=>$message,
'track_opens'=>true,
'track_clicks'=>false,
'tags'=>$tags
);
$url = "http://us5.sts.mailchimp.com/1.0/SendEmail";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url.'?'.http_build_query($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
echo $result;
curl_close ($ch);
var_dump($result);
$data = json_decode($result);
echo "Status = ".$data->status."\n";
?>
【讨论】:
此答案适用于 1.0 api 而不是 3.0 对于仍在寻找答案的任何人,版本 1 将于 2019 年 4 月停用。以上是关于如何通过 MailChimp 3.0 api 发送电子邮件?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Mailchimp API 3.0 多次向订阅者发送同一封电子邮件
Mailchimp 如何在 javascript 中调用 mailchimp 3.0 API