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 :