SMTP Perl Code Example
(error)
Danger
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";
The following code builds a MIME mail message demonstrating all the portions of the SMTP API protocol. To use this example, install the following Perl modules:
- MIME::Entity
- Authen::SASL
- JSON
1# !/usr/bin/perl2use strict;3use SmtpApiHeader;4use MIME::Entity;5use Net::SMTP;67my $hdr = SmtpApiHeader->new;89# The list of addresses this message will be sent to10my @toList = ('isaac@example', 'tim@example', 'jose@example');1112# The names of the recipients13my @nameList = ('Isaac', 'Tim', 'Jose');1415# Another substitution variable16my @timeList = ('4pm', '1pm', '2pm');1718# Set all of the above variables19$hdr->addTo(@toList);20$hdr->addSubVal('-name-', @nameList);21$hdr->addSubVal('-time-', @timeList);2223# Specify that this is an initial contact message24$hdr->setCategory("initial");2526# Enable a text footer and set it27$hdr->addFilterSetting('footer', 'enable', 1);28$hdr->addFilterSetting('footer', "text/plain", "Thank you for your business");2930my $from = 'you@example.com';3132# For multiple recipient emails, the 'to' address is irrelevant33my $to = 'example@example.com';34my $plain = <<EOM;35Hello -name-,3637Thank you for your interest in our products. We have set up an appointment38to call you at -time- EST to discuss your needs in more detail.3940Regards,41Fred42EOM4344my $html = <<EOM;45<html>46<head></head>47<body>48<p>Hello -name-,<br />49Thank you for your interest in our products. We have set up an appointment<br />50to call you at -time- EST to discuss your needs in more detail.<br />5152Regards,<br />53Fred<br />54</p>55</body>56</html>57EOM5859# Create the MIME message that will be sent. Check out MIME::Entity on CPAN for more details60my $mime = MIME::Entity->build(Type => 'multipart/alternative' ,6162Encoding => '-SUGGEST',63From => $from,64To => $to,65Subject => 'Contact Response for <name> at <time>');6667# Add the header68$mime->head->add("X-SMTPAPI", $hdr->asJSON);6970# Add body71$mime->attach(Type => 'text/plain',72Encoding =>'-SUGGEST',73Data => $plain);7475$mime->attach(Type => 'text/html',76Encoding =>'-SUGGEST',77Data => $html);7879# Login credentials80my $username = 'apikey';81my $api_key = "your_api_key";8283# Open a connection to the SendGrid mail server84my $smtp = Net::SMTP->new('smtp.sendgrid.net',85Port=> 587,86Timeout => 20,87Hello => "example.com");8889# Authenticate90$smtp->auth($username, $api_key);9192# Send the rest of the SMTP stuff to the server93$smtp->mail($from);94$smtp->to($to);95$smtp->data($mime->stringify);96$smtp->quit();