Sample code for 30+ languages & platforms
Rust 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 Rust Downloads

Rust

// Assumes Chilkat is unlocked. See Global Unlock Sample.
let _ = false;
let http = chilkat::Http::new();
http.set_require_ssl_cert_verify(true);
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.set_auth_token("SENDGRID_API_KEY");
let resp = chilkat::HttpResponse::new();

// Leave this empty to create a draft. To schedule, supply the ID of an already reviewed draft.
let reviewed_draft_id = String::new();
if reviewed_draft_id == "" {
    let draft = chilkat::JsonObject::new();
    let _ = draft.update_string("name", "November newsletter");
    let _ = draft.update_string("send_to.list_ids[0]", "YOUR_LIST_UUID");
    let _ = draft.update_int("email_config.sender_id", 12345);
    let _ = draft.update_int("email_config.suppression_group_id", 67890);
    let _ = draft.update_string("email_config.design_id", "YOUR_NEWSLETTER_DESIGN_UUID");
    if http.http_json("POST", "https://api.sendgrid.com/v3/marketing/singlesends", &draft, "application/json", &resp).is_err() {
        if resp.status_code() == 0 {
            println!("{}", http.last_error_text());
        } else {
            println!("HTTP {}: {}", resp.status_code(), resp.body_str());
        }

        return;
    }

    if resp.status_code() != 201 {
        println!("HTTP {}: {}", resp.status_code(), resp.body_str());
        return;
    }

    let created = chilkat::JsonObject::new();
    if created.load(&resp.body_str()).is_err() {
        println!("Unexpected response: expected JSON.");
        return;
    }

    let draft_id = created.string_of("id").unwrap_or_default();
    if !created.has_member("id") {
        println!("Missing id in response.");
        return;
    }

    println!("Draft created: {}. Review it in Marketing Campaigns before scheduling.", draft_id);
    return;
}

// The example selects tomorrow at the current UTC time. Use your approved send time.
let release_time = chilkat::DateTime::new();
let _ = release_time.set_from_current_system_time();
let _ = release_time.add_days(1);
let schedule = chilkat::JsonObject::new();
let _ = schedule.update_string("send_at", &release_time.get_as_timestamp(false).unwrap_or_default());
let _ = http.set_url_var("draft", &reviewed_draft_id);

if http.http_json("PUT", "https://api.sendgrid.com/v3/marketing/singlesends/{$draft}/schedule", &schedule, "application/json", &resp).is_err() {
    if resp.status_code() == 0 {
        println!("{}", http.last_error_text());
    } else {
        println!("HTTP {}: {}", resp.status_code(), resp.body_str());
    }

    return;
}

if resp.status_code() != 201 {
    println!("HTTP {}: {}", resp.status_code(), resp.body_str());
    return;
}

let scheduled = chilkat::JsonObject::new();
if scheduled.load(&resp.body_str()).is_err() {
    println!("Unexpected response: expected JSON.");
    return;
}

println!("{}: {}", scheduled.string_of("status").unwrap_or_default(), scheduled.string_of("send_at").unwrap_or_default());