These examples require the JSON Ruby Library.
Categories and Unique Arguments will be stored as a "Not PII" field and may be used for counting or other operations as SendGrid runs its systems. These fields generally cannot be redacted or removed. You should take care not to place PII in this field. SendGrid does not treat this data as PII, and its value may be visible to SendGrid employees, stored long-term, and may continue to be stored after you've left SendGrid's platform.
This header is required for each example.
_63# !/usr/bin/ruby_63# Version 1.0_63# Last Updated 6/22/2009_63require 'json'_63_63class SmtpApiHeader_63_63 def initialize()_63 @data = {}_63 end_63_63 def addTo(to)_63 @data['to'] ||= []_63 @data['to'] += to.kind_of?(Array) ? to : [to]_63 end_63_63 def addSubVal(var, val)_63 if not @data['sub']_63 @data['sub'] = {}_63 end_63 if val.instance_of?(Array)_63 @data['sub'][var] = val_63 else_63 @data['sub'][var] = [val]_63 end_63 end_63_63 def setUniqueArgs(val)_63 if val.instance_of?(Hash)_63 @data['unique_args'] = val_63 end_63 end_63_63 def setCategory(cat)_63_63 @data['category'] = cat_63 end_63_63 def addFilterSetting(fltr, setting, val)_63 if not @data['filters']_63 @data['filters'] = {}_63 end_63 if not @data['filters'][fltr]_63 @data['filters'][fltr] = {}_63 end_63 if not @data['filters'][fltr]['settings']_63 @data['filters'][fltr]['settings'] = {}_63 end_63 @data['filters'][fltr]['settings'][setting] = val_63 end_63_63 def asJSON()_63 json = JSON.generate @data_63 return json.gsub(/(["\]}])([,:])(["\[{])/, '\\1\\2 \\3')_63 end_63_63 def as_string()_63 json = asJSON()_63 str = 'X-SMTPAPI: %s' % json.gsub(/(.{1,72})( +|$\n?)|(.{1,72})/,"\\1\\3\n")_63 return str_63 end_63_63end
_34require './SmtpApiHeader.rb'_34require 'mail'_34_34Mail.defaults do_34 delivery_method :smtp, { :address => 'smtp.sendgrid.net',_34 :port => 587,_34 :domain => 'sendgrid.com',_34 :user_name => 'apikey',_34 :api_key => 'yourSendGridAPIKey',_34 :authentication => 'plain',_34 :enable_starttls_auto => true }_34end_34_34hdr = SmtpApiHeader.new_34_34receiver = ['recipienteexampexample@example.com', 'recipient2@domain.com']_34_34hdr.addTo(receiver)_34hdr.setUniqueArgs({'test' => 1, 'foo' =>2})_34hdr.setCategory('yourCategory')_34_34mail = Mail.deliver do_34 header['X-SMTPAPI'] = hdr.asJSON()_34 to 'willnotdeliver@domain.com' # When using SMTPAPI's 'to' parameter, this address will not get delivered to_34 from 'yourEmailAddress@domain.com'_34 subject 'Ruby Example using X-SMTPAPI header'_34 text_part do_34 body 'You would put your content here, but I am going to say: Hello world!'_34 end_34 html_part do_34 content_type 'text/html; charset=UTF-8'_34 body '<b>Hello world!</b> Glad to have you here!'_34 end_34end