Sample code for 30+ languages & platforms
PHP Requires Chilkat v11.0.0+

SendGrid: Create a newsletter draft, then schedule the reviewed draft

See more SendGrid Examples

Prepare a Marketing Campaigns design with a subject, content, and an unsubscribe module. Supply the design and list UUIDs, a verified marketing sender ID, and the unsubscribe group ID. The first run creates only a draft. After reviewing the audience and content, run the scheduling branch with that same ID. Single Sends use an ISO 8601 UTC string, unlike Mail Send’s numeric Unix timestamp.

Create a Single Send · Schedule a Single Send

Chilkat PHP Downloads

PHP
<?php

use Chilkat2\DateTime;
use Chilkat2\Http;
use Chilkat2\HttpResponse;
use Chilkat2\JsonObject;

$success = false;

// Assumes Chilkat is unlocked. See Global Unlock Sample.
$success = false;
$http = new Http();
$http->RequireSslCertVerify = true;
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
$http->AuthToken = 'SENDGRID_API_KEY';
$resp = new HttpResponse();

// Leave this empty to create a draft. To schedule, supply the ID of an already reviewed draft.
$reviewedDraftId = '';
if ($reviewedDraftId === '') {
    $draft = new JsonObject();
    $draft->UpdateString('name', 'November newsletter');
    $draft->UpdateString('send_to.list_ids[0]', 'YOUR_LIST_UUID');
    $draft->UpdateInt('email_config.sender_id', 12345);
    $draft->UpdateInt('email_config.suppression_group_id', 67890);
    $draft->UpdateString('email_config.design_id', 'YOUR_NEWSLETTER_DESIGN_UUID');
    try {
        $http->HttpJson('POST', 'https://api.sendgrid.com/v3/marketing/singlesends', $draft, 'application/json', $resp);
    } catch (\Chilkat2\Exception $e) {
        if ($resp->StatusCode == 0) {
            echo $e->getMessage(), "\n";
        } else {
            echo 'HTTP ', $resp->StatusCode, ': ', $resp->BodyStr, "\n";
        }

        exit;
    }

    if ($resp->StatusCode != 201) {
        echo 'HTTP ', $resp->StatusCode, ': ', $resp->BodyStr, "\n";
        exit;
    }

    $created = new JsonObject();
    try {
        $created->Load($resp->BodyStr);
    } catch (\Chilkat2\Exception $e) {
        echo "Unexpected response: expected JSON.\n";
        exit;
    }

    $draftId = $created->StringOf('id');
    if (!$created->HasMember('id')) {
        echo "Missing id in response.\n";
        exit;
    }

    echo 'Draft created: ', $draftId, ". Review it in Marketing Campaigns before scheduling.\n";
    exit;
}

// The example selects tomorrow at the current UTC time. Use your approved send time.
$releaseTime = new DateTime();
$releaseTime->SetFromCurrentSystemTime();
$releaseTime->AddDays(1);
$schedule = new JsonObject();
$schedule->UpdateString('send_at', $releaseTime->GetAsTimestamp(false));
$http->SetUrlVar('draft', $reviewedDraftId);

try {
    $http->HttpJson('PUT', 'https://api.sendgrid.com/v3/marketing/singlesends/{$draft}/schedule', $schedule, 'application/json', $resp);
} catch (\Chilkat2\Exception $e) {
    if ($resp->StatusCode == 0) {
        echo $e->getMessage(), "\n";
    } else {
        echo 'HTTP ', $resp->StatusCode, ': ', $resp->BodyStr, "\n";
    }

    exit;
}

if ($resp->StatusCode != 201) {
    echo 'HTTP ', $resp->StatusCode, ': ', $resp->BodyStr, "\n";
    exit;
}

$scheduled = new JsonObject();
try {
    $scheduled->Load($resp->BodyStr);
} catch (\Chilkat2\Exception $e) {
    echo "Unexpected response: expected JSON.\n";
    exit;
}

echo $scheduled->StringOf('status'), ': ', $scheduled->StringOf('send_at'), "\n";