Objective-C Requires Chilkat v11.0.0+
Objective-C
SendGrid: Send an order receipt with a Dynamic Template
See more SendGrid Examples
Activate a version of your receipt template and set its subject to Receipt for order {{order_number}}. Try this HTML in the template:
<p>Thank you, {{first_name}}.</p>
<ul>{{#each items}}<li>{{quantity}} × {{name}}</li>{{/each}}</ul>Replace the template ID, verified sender, and recipient. Template data is visible to the recipient. The separate, opaque notification reference is for matching delivery events to an application record; keep personal data out of custom arguments.
Chilkat Objective-C Downloads
#import <CkoHttp.h>
#import <CkoHttpResponse.h>
#import <CkoJsonObject.h>
BOOL success = NO;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = NO;
CkoHttp *http = [[CkoHttp alloc] init];
http.RequireSslCertVerify = YES;
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.AuthToken = @"SENDGRID_API_KEY";
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
CkoJsonObject *message = [[CkoJsonObject alloc] init];
[message UpdateString: @"from.email" value: @"sender@example.com"];
[message UpdateString: @"template_id" value: @"d-YOUR_ACTIVE_TEMPLATE_ID"];
[message UpdateString: @"personalizations[0].to[0].email" value: @"recipient@example.com"];
[message UpdateString: @"personalizations[0].dynamic_template_data.order_number" value: @"1042"];
[message UpdateString: @"personalizations[0].dynamic_template_data.first_name" value: @"Avery"];
[message UpdateString: @"personalizations[0].dynamic_template_data.items[0].name" value: @"Notebook"];
[message UpdateInt: @"personalizations[0].dynamic_template_data.items[0].quantity" value: [NSNumber numberWithInt: 2]];
[message UpdateString: @"personalizations[0].dynamic_template_data.items[1].name" value: @"Pen"];
[message UpdateInt: @"personalizations[0].dynamic_template_data.items[1].quantity" value: [NSNumber numberWithInt: 1]];
// This reference is returned on Event Webhook events; it is not a template variable.
[message UpdateString: @"personalizations[0].custom_args.notification_ref" value: @"ntf_8042"];
success = [http HttpJson: @"POST" url: @"https://api.sendgrid.com/v3/mail/send" json: message contentType: @"application/json" response: resp];
if (success != YES) {
if ([resp.StatusCode intValue] == 0) {
NSLog(@"%@",http.LastErrorText);
}
else {
NSLog(@"%@%d%@%@",@"HTTP ",[resp.StatusCode intValue],@": ",resp.BodyStr);
}
return;
}
if ([resp.StatusCode intValue] != 202) {
NSLog(@"%@%d%@%@",@"HTTP ",[resp.StatusCode intValue],@": ",resp.BodyStr);
return;
}
NSLog(@"%@",@"Accepted for delivery (HTTP 202).");