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

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.

Dynamic Templates

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Assumes Chilkat is unlocked. See Global Unlock Sample.
    SELECT @success = 0
    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @http, 'RequireSslCertVerify', 1
    --  Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
    EXEC sp_OASetProperty @http, 'AuthToken', 'SENDGRID_API_KEY'
    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    DECLARE @message int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @message OUT

    EXEC sp_OAMethod @message, 'UpdateString', @success OUT, 'from.email', 'sender@example.com'
    EXEC sp_OAMethod @message, 'UpdateString', @success OUT, 'template_id', 'd-YOUR_ACTIVE_TEMPLATE_ID'
    EXEC sp_OAMethod @message, 'UpdateString', @success OUT, 'personalizations[0].to[0].email', 'recipient@example.com'
    EXEC sp_OAMethod @message, 'UpdateString', @success OUT, 'personalizations[0].dynamic_template_data.order_number', '1042'
    EXEC sp_OAMethod @message, 'UpdateString', @success OUT, 'personalizations[0].dynamic_template_data.first_name', 'Avery'
    EXEC sp_OAMethod @message, 'UpdateString', @success OUT, 'personalizations[0].dynamic_template_data.items[0].name', 'Notebook'
    EXEC sp_OAMethod @message, 'UpdateInt', @success OUT, 'personalizations[0].dynamic_template_data.items[0].quantity', 2
    EXEC sp_OAMethod @message, 'UpdateString', @success OUT, 'personalizations[0].dynamic_template_data.items[1].name', 'Pen'
    EXEC sp_OAMethod @message, 'UpdateInt', @success OUT, 'personalizations[0].dynamic_template_data.items[1].quantity', 1
    --  This reference is returned on Event Webhook events; it is not a template variable.
    EXEC sp_OAMethod @message, 'UpdateString', @success OUT, 'personalizations[0].custom_args.notification_ref', 'ntf_8042'

    EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'POST', 'https://api.sendgrid.com/v3/mail/send', @message, 'application/json', @resp
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
        IF @iTmp0 = 0
          BEGIN
            EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
            PRINT @sTmp0
          END
        ELSE
          BEGIN

            EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT

            EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
            PRINT 'HTTP ' + @iTmp0 + ': ' + @sTmp0
          END
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @message
        RETURN
      END
    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    IF @iTmp0 <> 202
      BEGIN

        EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT

        EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
        PRINT 'HTTP ' + @iTmp0 + ': ' + @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @message
        RETURN
      END

    PRINT 'Accepted for delivery (HTTP 202).'

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @resp
    EXEC @hr = sp_OADestroy @message


END
GO