web-dev-qa-db-ja.com

MailChimp 3.0 APIを介してメールを送信する方法

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": "[email protected]",
            "from_name": "John",
            "to_email": "[email protected]",
            "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;

何が悪いのですか?

11
Anton Smatanik

おそらくv1で行ったように、API v3からランダムなメールを送信することはできません。これで、LamaDelRayで述べられているように、以前に作成したキャンペーンをMailChimpでのみ送信できます。

4
async3

ここの私のコードは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」が必要です。少し待つとメールが送信されます。

幸運を !

3
LamaDelRay
<?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
ramji