如何使用phpchimp API在php中发送电子邮件

问题描述 投票:0回答:1

这是我在MailChimp中的列表。列表名称:客户端

enter image description here

我正在尝试使用MailChimp API向电子邮件发送电子邮件:“[email protected]”。

我的PHP代码在这里:

<?php
/*
$apikey = 'my_api-key-goes_here';
$list_id = 'list_id_of_Clients_goes_here';
*/

    $apikey = 'my_api-key-goes_here';



    $to_emails = array('[email protected]');
    $to_names = array('Raj');

    $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'=>'[email protected]',
        '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://us1.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);

    $data = json_decode($result);
    echo "Status = ".$data->status."\n";
 ?>

但是当我运行这个脚本时,我得到的输出是“Status =”。我试图发送的邮件也没有发送。我哪里错了?如何使用MailChimp API从列表中向特定电子邮件发送电子邮件

先感谢您。

php mailchimp mailchimp-api-v3.0 php-curl
1个回答
0
投票
$email = 'EMAIL_ADDRESS';
$list_id = 'LIST_ID';
$api_key = 'API_KEY';

$data_center = substr($api_key,strpos($api_key,'-')+1);

$url = 'https://'. $data_center .'.api.mailchimp.com/3.0/lists/'. $list_id .'/members';

$json = json_encode([
    'email_address' => $email,
    'status'        => 'subscribed', //pass 'subscribed' or 'pending'
]);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $api_key);
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_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
$result = curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo $status_code;
© www.soinside.com 2019 - 2024. All rights reserved.