SQL Server Requires Chilkat v11.0.0+
SQL Server
SendGrid: Send two private appointment reminders in one request
See more SendGrid Examples
Create an active reminder template with subject Appointment reminder and body Hello {{first_name}}, your appointment is {{appointment_time}}. Use your template ID, verified sender and two test addresses. Each recipient has a separate personalization; placing both addresses in a single To array would expose them to one another and share the same template data.
Chilkat SQL Server Downloads
--
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'
-- Each recipient gets a separate envelope and a private appointment time.
EXEC sp_OAMethod @message, 'UpdateString', @success OUT, 'personalizations[0].to[0].email', 'avery@example.com'
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.appointment_time', 'Tuesday at 9:30 AM Central'
EXEC sp_OAMethod @message, 'UpdateString', @success OUT, 'personalizations[1].to[0].email', 'robin@example.com'
EXEC sp_OAMethod @message, 'UpdateString', @success OUT, 'personalizations[1].dynamic_template_data.first_name', 'Robin'
EXEC sp_OAMethod @message, 'UpdateString', @success OUT, 'personalizations[1].dynamic_template_data.appointment_time', 'Wednesday at 2:00 PM Central'
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