簡體   English   中英

如何在PHP中使用mailchimp API發送電子郵件

[英]How to send emails using mailchimp API in php

這是我的MailChimp中的列表。 清單名稱:客戶

在此處輸入圖片說明

我正在嘗試使用MailChimp API將電子郵件發送至電子郵件:“ rajbiswas176@gmail.com ”。

我的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('rajbiswas176@gmail.com');
    $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'=>'raj.biswas936@gmail.com',
        '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將電子郵件發送到列表中的特定電子郵件

先感謝您。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM