SQL Server Requires Chilkat v11.0.0+
SQL Server
SendGrid: Send product updates with a topic unsubscribe link
See more SendGrid Examples
Use the numeric ID of your existing Product updates unsubscribe group and a verified sender. The ASM tag expands to the recipient-specific link for this group. A click opts the recipient out of this topic; a global unsubscribe applies across topics.
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, 'personalizations[0].to[0].email', 'recipient@example.com'
EXEC sp_OAMethod @message, 'UpdateInt', @success OUT, 'asm.group_id', 12345
EXEC sp_OAMethod @message, 'UpdateString', @success OUT, 'subject', 'Product updates: November edition'
EXEC sp_OAMethod @message, 'UpdateString', @success OUT, 'content[0].type', 'text/html'
EXEC sp_OAMethod @message, 'UpdateString', @success OUT, 'content[0].value', '<p>The new catalog is available.</p><p><a href="<%asm_group_unsubscribe_raw_url%>">Unsubscribe from product updates</a></p>'
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