如何通过MailChimp 3.0 api发送邮件?

12

我正在尝试使用Mailchimp API 3.0在PHP中发送电子邮件,但是我没有成功。这是我的代码:

$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;

我做错了什么?


1
可能是重复的问题:如何使用Mailchimp v3.0创建广告活动 - TooMuchPete
3个回答

5

您不能像使用v1 API一样从API v3发送随机电子邮件。现在,您只能按照LamaDelRay的说法发送MailChimp中先前创建的活动。


3
我的代码适用于一封“测试邮件”,但基本上非测试邮件也是相同的,只是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"。稍等片刻即可发送邮件。

祝您好运!


1
我可以将表单数据传递到这个邮件吗? - user4689071
1
我们如何动态设置电子邮件和电子邮件正文,就像在Mailchimp设置活动期间必须设置主题和正文一样。它似乎是静态的。 - Ata ul Mustafa
1
@AtaulMustafa 你可以在json中指定主题。对于正文,有一些技巧需要注意;这有点难以解释,值得提出问题。 - LamaDelRay
1
@LamaDelRay,感谢您的回复。实际上,我想发送一封邮件,其主题和正文将是动态的,收件人也将是动态的。因此,在我的情况下,我无法制作静态收件人列表。您有任何建议/解决方案/变通方法吗? - Ata ul Mustafa
1
@AtaulMustafa 你可以通过使用模板来实现一些动态效果,每次调用函数时都会覆盖它们,但我建议使用除mailchimp之外的其他东西,因为它对于动态邮件来说有点难以使用。 - LamaDelRay
显示剩余3条评论

1
<?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";
 ?>

2
这个答案适用于1.0 API而不是3.0。 - Lucas Farah
4
如果有人仍在寻找答案,版本1已于2019年4月被停用。 - Siva Kandaraj

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接