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.
1# !/usr/bin/perl23# Version 1.04# Last Updated 6/22/20095use strict;6package SmtpApiHeader;7use JSON;89sub new10{11my $self = shift;12my @a = ();13$self = { 'data' => { }};14bless($self);15return $self;16}1718sub addTo19{20my $self = shift;21my @to = @_;22push(@{$self->{data}->{to}}, @to);23}2425sub addSubVal26{27my $self = shift;28my $var = shift;29my @val = @_;3031if (!defined($self->{data}->{sub}->{$var}))32{33$self->{data}->{sub}->{$var} = ();34}35push(@{$self->{data}->{sub}->{$var}}, @val);36}3738sub setUniqueArgs39{40my $self = shift;41my $val = shift;42if (ref($val) eq 'HASH')43{44$self->{data}->{unique_args} = $val;45}46}4748sub setCategory49{50my $self = shift;51my $cat = shift;52$self->{data}->{category} = $cat;53}5455sub addFilterSetting56{57my $self = shift;58my $filter = shift;59my $setting = shift;60my $val = shift;61if (!defined($self->{data}->{filters}->{$filter}))62{63$self->{data}->{filters}->{$filter} = {};64}65if (!defined($self->{data}->{filters}->{$filter}->{settings}))66{67$self->{data}->{filters}->{$filter}->{settings} = {};68}69$self->{data}->{filters}->{$filter}->{settings}->{$setting} = $val;70}7172sub asJSON73{74my $self = shift;75my $json = JSON->new;76$json->space_before(1);77$json->space_after(1);78return $json->encode($self->{data});79}8081sub as_string82{83my $self = shift;84my $json = $self->asJSON;85$json =~ s/(.{1,72})(\s)/$1\n /g;86my $str = "X-SMTPAPI: $json";87return $str;88}
1# !/usr/bin/perl2use SmtpApiHeader;34my @receiver = ('kyle','bob','someguy');56my $hdr = SmtpApiHeader->new;78my $time = '1pm';9my $name = 'kyle';1011$hdr->addFilterSetting('subscriptiontrack', 'enable', 1);12$hdr->addFilterSetting('twitter', 'enable', 1); #please check the apps available for your current package at https://sendgrid.com/pricing13$hdr->addTo(@receiver);14$hdr->addTo('kyle2');1516$hdr->addSubVal('-time-', $time);1718$hdr->addSubVal('-name-', $time);19$hdr->setUniqueArgs({'test'=>1, 'foo'=>2});2021print $hdr->as_string;2223print "\n";24</code>2526## Full Perl Example27<p>The following code builds a MIME mail message demonstrating all the portions of the SMTP API protocol. To use this example, you will need to have the following perl modules installed:</p>28<ul class="regular">29<li>MIME::Entity</li>30<li>Authen::SASL</li>31<li>JSON</li>32</ul>33<code>34# !/usr/bin/perl35use strict;36use SmtpApiHeader;37use MIME::Entity;38use Net::SMTP;3940my $hdr = SmtpApiHeader->new;4142# The list of addresses this message will be sent to43my @toList = ('isaac@example', 'tim@example', 'jose@example');4445# The names of the recipients46my @nameList = ('Isaac', 'Tim', 'Jose');4748# Another substitution variable49my @timeList = ('4pm', '1pm', '2pm');5051# Set all of the above variables52$hdr->addTo(@toList);53$hdr->addSubVal('-name-', @nameList);54$hdr->addSubVal('-time-', @timeList);5556# Specify that this is an initial contact message57$hdr->setCategory("initial");5859# Enable a text footer and set it60$hdr->addFilterSetting('footer', 'enable', 1);61$hdr->addFilterSetting('footer', "text/plain", "Thank you for your business");6263my $from = 'you@yourdomain.com';6465# For multiple recipient emails, the 'to' address is irrelevant66my $to = 'example@example.com';67my $plain = <<EOM;68Hello -name-,6970Thank you for your interest in our products. We have set up an appointment71to call you at -time- EST to discuss your needs in more detail.7273Regards,74Fred75EOM7677my $html = <<EOM;78<html>79<head></head>80<body>81<p>Hello -name-,<br />82Thank you for your interest in our products. We have set up an appointment<br />83to call you at -time- EST to discuss your needs in more detail.<br />8485Regards,<br />86Fred<br />87</p>88</body>89</html>90EOM9192# Create the MIME message that will be sent. Check out MIME::Entity on CPAN for more details93my $mime = MIME::Entity->build(Type => 'multipart/alternative' ,9495Encoding => '-SUGGEST',96From => $from,97To => $to,98Subject => 'Contact Response for <name> at <time>');99100# Add the header101$mime->head->add("X-SMTPAPI", $hdr->asJSON);102103# Add body104$mime->attach(Type => 'text/plain',105Encoding =>'-SUGGEST',106Data => $plain);107108$mime->attach(Type => 'text/html',109Encoding =>'-SUGGEST',110Data => $html);111112# Login credentials113my $username = 'apikey';114my $api_key = "your_api_key";115116# Open a connection to the SendGrid mail server117my $smtp = Net::SMTP->new('smtp.sendgrid.net',118Port=> 587,119Timeout => 20,120Hello => "yourdomain.com");121122# Authenticate123$smtp->auth($username, $api_key);124125# Send the rest of the SMTP stuff to the server126$smtp->mail($from);127$smtp->to($to);128$smtp->data($mime->stringify);129$smtp->quit();