12

I'm trying to send email through mailchimp api version 3.0 in php, but i have no luck. This is my code:

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

What am i doing wrong?

Anton Smatanik
  • 587
  • 1
  • 9
  • 25
  • 1
    possible duplicate of [How to create a campaign using mailchimp v3.0](http://stackoverflow.com/questions/31427926/how-to-create-a-campaign-using-mailchimp-v3-0) – TooMuchPete Jul 31 '15 at 01:53

3 Answers3

5

You cannot send a random email from API v3 like you probably did with v1. Now you can only send a previously created campaign in MailChimp as stated by LamaDelRay.

async3
  • 431
  • 6
  • 8
3

My code here worked for a test mail, but basically it's the same thing for non test, just the url is changing.

<?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);

?>

you should have a string "0" if everything went well. Wait a bit and the mail will be sent.

Good luck !

LamaDelRay
  • 199
  • 2
  • 11
  • 1
    which url to change and where?? @LamadelRay – Amanjot Kaur Jun 16 '16 at 05:04
  • 1
    The line of $url is to be made with your apikey and your campaignid – LamaDelRay Jun 16 '16 at 06:12
  • 1
    can i pass form data to this mail –  Aug 02 '17 at 14:27
  • 1
    How can we set email and body of email dynamically, as in mailchimp during setting campaign you have to set the subject and body. It seems to be static. – Ata ul Mustafa Oct 12 '17 at 13:40
  • 1
    @AtaulMustafa You can specify the subject in the json. For the body there is some tricks here and there; it's kinda hard to explain and worth a question. – LamaDelRay Oct 12 '17 at 15:00
  • 1
    @LamaDelRay, Thanks for reply. Actually I want to send an email whose subject and body will be dynamic, and also the recipients will also be dynamic. So, in my case I cannot make static list of recipients. Do you have any suggestion/solution/workaround. – Ata ul Mustafa Oct 13 '17 at 10:14
  • 1
    @AtaulMustafa You can do a bit of dynamism by using templates that you overwrite at each call of your function, but I advise using something else than mailchimp, it's kinda hard to use it for dynamic mails. – LamaDelRay Oct 13 '17 at 11:13
  • string(234) "{"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"Resource Not Found","status":404,"detail":"The requested resource could not be found.","instance":"9e30c4ca-d2ea-4e07-a723-8b386d35b44e"}" i got this type of erroe – Bhadresh Sonani Oct 23 '19 at 10:41
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";
 ?>
ramji
  • 60
  • 5