Create and Send Campaign Using MooSend API

<?php

$apikey = "<MOOSEND-API-KEY-HERE>";
$listID = "<MOOSEND-LIST-ID-HERE>";
$ep = "https://api.moosend.com/v3/";

$ch = curl_init();

$url = $ep."campaigns/create.json?Format=json&apikey=".$apikey;

$bulkPostFields = array (
  'Name' => '<CAMPAIGN-NAME-HERE>',
  'Subject' => '<CAMPAIGN-SUBJECT-HERE>',
  'SenderEmail' => '<SENDER-EMAIL-ID-HERE>',
  'ReplyToEmail' => '<SENDER-EMAIL-ID-HERE>',
  'ConfirmationToEmail' => '<SENDER-EMAIL-ID-HERE>',
  'MailingLists' => [
array (
'MailingListID' => "<MOOSEND-LIST-ID-HERE>",
'SegmentID' => "<MOOSEND-SEGMENT-ID-HERE>"
),
],
'WebLocation' => "PATH-TO-HTML-EMAILER",
);

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($bulkPostFields));
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "Accept: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}else{
$res = json_decode($result);
$campaignID = $res->Context;
}
curl_close ($ch);

//campaign send here
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $ep."campaigns/".$campaignID."/send.json?apikey=".$apikey);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "Accept: application/json";
$headers[] = "Content-Length: 0";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}else{
echo $result;
}
curl_close ($ch);

?> 

0 comments :

Create and Send Campaign Using Active Campaign API


Coming Soon..

0 comments :

Create and Send Campaign Using SendGrid API

<?php

$apiKey = "<SENDGRID-API-KEY-HERE>";
     
        $html_content = file_get_contents($emailerpath);
        $html_content = str_replace("*|UNSUB|*", "[unsubscribe]", $html_content); //code to add unsubscribe link

        $titleText = 'AD Daily - ' . date("Y-m-d");
        $postFields = array('title' => '<CAMPAIGN-NAME-HERE>', 'subject' => '<CAMPAIGN-SUBJECT-HERE>', "sender_id" => "<SENDER-ID-HERE>",
            "list_ids" => array("<LIST-ID-HERE>"),
            "categories" => array("<CAMPAIGN-CATEGORY>"),
            "html_content" => $html_content,
            "plain_content" => $mail_subject . ' <a href="[unsubscribe]" style="text-decoration:none;" target="_blank"><span style="color: #000000;">Unsubscribe</span></a>',
            "suppression_group_id" => "<UNSUBSCRIBE-GROUP-ID-HERE>",
        );
        $aa = json_encode($postFields);

        //create a campaign using sendGrid
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => "https://api.sendgrid.com/v3/campaigns",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => $aa,
            CURLOPT_HTTPHEADER => array(
                "authorization: Bearer " . $apiKey . "",
                "content-type: application/json"
            ),
        ));
        $response = json_decode(curl_exec($curl));
        $err = curl_error($curl);
        curl_close($curl);
     
        if ($err) {
            echo "<br/>cURL Error #:" . $err;
        } else {
            $campaignID = $response->id;
        }

//send a campaign using sendGrid
        if (isset($campaignID) && $campaignID != "") {
            echo " campaignID ==> " . $campaignID;
            $curl = curl_init();

            curl_setopt_array($curl, array(
                CURLOPT_URL => "https://api.sendgrid.com/v3/campaigns/" . $campaignID . "/schedules/now",
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 30,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "POST",
                CURLOPT_POSTFIELDS => "null",
                CURLOPT_HTTPHEADER => array(
                    "authorization: Bearer " . $apiKey . "",
                    "content-type: application/json"
                ),
            ));

            $response = curl_exec($curl);
            $err = curl_error($curl);

            curl_close($curl);

            if ($err) {
                echo "<br/>cURL Error #:" . $err;
            } else {
                echo '<br/>Operation completed without any errors <br/>';
                echo $response;
            }
        }

?>

0 comments :

Create and Send Campaign Using Mailchimp API


<?php

//Include MailChimp API version 3 Class Library
include('mcv3/src/MailChimp.php');

use \DrewM\MailChimp\MailChimp;
$MailChimp = new MailChimp('<MAILCHIMP-API-KEY-HERE>');

//create campaign at Mailchimp
$result = $MailChimp->post('campaigns', array(
            "type" => 'regular',
            "recipients" => array(
                "list_id" => "<LIST-ID-HERE>",
                "segment_opts" => array(
                    "match" => "any",
                    "conditions" => array(
                        array("field" => "interests-<INTEREST-GROUP-ID-HERE>", "op" => "interestcontains", "value" => ["<INTEREST-ID-HERE>"])
                    ),
                )
            ),
            'settings' => array(
                'subject_line' => '<CAMPAIGN-SUBJECT-HERE>',
                'title' => '<CAMPAIGN-NAME-HERE>',
                'from_name' => '<FROM-NAME-HERE>',
                'reply_to' => '<FROM-EMAIL-ID-HERE>',
                'auto_footer' => false
            ),
            'content' => array('url' => $emailerpath),
        ));
        $campaign_id = $result['id'];

        //sleep for Sometime
        sleep(20);

//put content to campaign
        $contentx = json_encode($MailChimp->put('/campaigns/' . $campaign_id . '/content', array('url' => $emailerpath)
        ));

        //sleep for Sometime
        sleep(20);

//send email to list
        echo json_encode($MailChimp->post('/campaigns/' . $campaign_id . '/actions/send'));

?>

0 comments :