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

SendGrid: Schedule a reminder and cancel it by batch ID

See more SendGrid Examples

Schedule a reminder one hour from now. Save its batch ID when it is allocated. If the appointment is canceled, use that saved ID in the cancellation branch. Mail Send accepts a Unix timestamp up to 72 hours ahead; cancel at least 10 minutes before the send time. Cancellation applies to the entire batch.

Create a batch ID ยท Cancel a batch

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

// On cancellation, set this to the batch ID saved with the original appointment.
// This branch does not create a new batch or send another reminder.
$batchToCancel = '';
if ($batchToCancel !== '') {
    $cancellation = new JsonObject();
    $cancellation->UpdateString('batch_id', $batchToCancel);
    $cancellation->UpdateString('status', 'cancel');
    try {
        $http->HttpJson('POST', 'https://api.sendgrid.com/v3/user/scheduled_sends', $cancellation, '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;
    }

    echo "Batch cancellation accepted.\n";
    exit;
}

try {
    $http->HttpNoBody('POST', 'https://api.sendgrid.com/v3/mail/batch', $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;
}

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

$batchId = $batch->StringOf('batch_id');
if (!$batch->HasMember('batch_id')) {
    echo "Missing batch_id in response.\n";
    exit;
}

// Allocate one batch per independently cancelable appointment.
echo 'Save this batch ID with the appointment: ', $batchId, "\n";
$sendTime = new DateTime();
$sendTime->SetFromCurrentSystemTime();
$sendTime->AddSeconds(3600);
$message = new JsonObject();
$message->UpdateString('batch_id', $batchId);
$message->UpdateInt('send_at', $sendTime->GetAsUnixTime(false));
$message->UpdateString('from.email', 'sender@example.com');
$message->UpdateString('personalizations[0].to[0].email', 'recipient@example.com');
$message->UpdateString('subject', 'Appointment reminder');
$message->UpdateString('content[0].type', 'text/plain');
$message->UpdateString('content[0].value', 'Your appointment is tomorrow at 9:30 AM Central.');

try {
    $http->HttpJson('POST', 'https://api.sendgrid.com/v3/mail/send', $message, '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 != 202) {
    echo 'HTTP ', $resp->StatusCode, ': ', $resp->BodyStr, "\n";
    exit;
}

echo 'Reminder scheduled for ', $sendTime->GetAsTimestamp(false), "\n";