Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

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.


SmtpApiHeader.pm

smtpapiheaderpm page anchor

_88
# !/usr/bin/perl
_88
_88
# Version 1.0
_88
# Last Updated 6/22/2009
_88
use strict;
_88
package SmtpApiHeader;
_88
use JSON;
_88
_88
sub new
_88
{
_88
my $self = shift;
_88
my @a = ();
_88
$self = { 'data' => { }};
_88
bless($self);
_88
return $self;
_88
}
_88
_88
sub addTo
_88
{
_88
my $self = shift;
_88
my @to = @_;
_88
push(@{$self->{data}->{to}}, @to);
_88
}
_88
_88
sub addSubVal
_88
{
_88
my $self = shift;
_88
my $var = shift;
_88
my @val = @_;
_88
_88
if (!defined($self->{data}->{sub}->{$var}))
_88
{
_88
$self->{data}->{sub}->{$var} = ();
_88
}
_88
push(@{$self->{data}->{sub}->{$var}}, @val);
_88
}
_88
_88
sub setUniqueArgs
_88
{
_88
my $self = shift;
_88
my $val = shift;
_88
if (ref($val) eq 'HASH')
_88
{
_88
$self->{data}->{unique_args} = $val;
_88
}
_88
}
_88
_88
sub setCategory
_88
{
_88
my $self = shift;
_88
my $cat = shift;
_88
$self->{data}->{category} = $cat;
_88
}
_88
_88
sub addFilterSetting
_88
{
_88
my $self = shift;
_88
my $filter = shift;
_88
my $setting = shift;
_88
my $val = shift;
_88
if (!defined($self->{data}->{filters}->{$filter}))
_88
{
_88
$self->{data}->{filters}->{$filter} = {};
_88
}
_88
if (!defined($self->{data}->{filters}->{$filter}->{settings}))
_88
{
_88
$self->{data}->{filters}->{$filter}->{settings} = {};
_88
}
_88
$self->{data}->{filters}->{$filter}->{settings}->{$setting} = $val;
_88
}
_88
_88
sub asJSON
_88
{
_88
my $self = shift;
_88
my $json = JSON->new;
_88
$json->space_before(1);
_88
$json->space_after(1);
_88
return $json->encode($self->{data});
_88
}
_88
_88
sub as_string
_88
{
_88
my $self = shift;
_88
my $json = $self->asJSON;
_88
$json =~ s/(.{1,72})(\s)/$1\n /g;
_88
my $str = "X-SMTPAPI: $json";
_88
return $str;
_88
}



_129
# !/usr/bin/perl
_129
use SmtpApiHeader;
_129
_129
my @receiver = ('kyle','bob','someguy');
_129
_129
my $hdr = SmtpApiHeader->new;
_129
_129
my $time = '1pm';
_129
my $name = 'kyle';
_129
_129
$hdr->addFilterSetting('subscriptiontrack', 'enable', 1);
_129
$hdr->addFilterSetting('twitter', 'enable', 1); #please check the apps available for your current package at https://sendgrid.com/pricing
_129
$hdr->addTo(@receiver);
_129
$hdr->addTo('kyle2');
_129
_129
$hdr->addSubVal('-time-', $time);
_129
_129
$hdr->addSubVal('-name-', $time);
_129
$hdr->setUniqueArgs({'test'=>1, 'foo'=>2});
_129
_129
print $hdr->as_string;
_129
_129
print "\n";
_129
</code>
_129
_129
## Full Perl Example
_129
<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>
_129
<ul class="regular">
_129
<li>MIME::Entity</li>
_129
<li>Authen::SASL</li>
_129
<li>JSON</li>
_129
</ul>
_129
<code>
_129
# !/usr/bin/perl
_129
use strict;
_129
use SmtpApiHeader;
_129
use MIME::Entity;
_129
use Net::SMTP;
_129
_129
my $hdr = SmtpApiHeader->new;
_129
_129
# The list of addresses this message will be sent to
_129
my @toList = ('isaac@example', 'tim@example', 'jose@example');
_129
_129
# The names of the recipients
_129
my @nameList = ('Isaac', 'Tim', 'Jose');
_129
_129
# Another substitution variable
_129
my @timeList = ('4pm', '1pm', '2pm');
_129
_129
# Set all of the above variables
_129
$hdr->addTo(@toList);
_129
$hdr->addSubVal('-name-', @nameList);
_129
$hdr->addSubVal('-time-', @timeList);
_129
_129
# Specify that this is an initial contact message
_129
$hdr->setCategory("initial");
_129
_129
# Enable a text footer and set it
_129
$hdr->addFilterSetting('footer', 'enable', 1);
_129
$hdr->addFilterSetting('footer', "text/plain", "Thank you for your business");
_129
_129
my $from = 'you@yourdomain.com';
_129
_129
# For multiple recipient emails, the 'to' address is irrelevant
_129
my $to = 'example@example.com';
_129
my $plain = <<EOM;
_129
Hello -name-,
_129
_129
Thank you for your interest in our products. We have set up an appointment
_129
to call you at -time- EST to discuss your needs in more detail.
_129
_129
Regards,
_129
Fred
_129
EOM
_129
_129
my $html = <<EOM;
_129
<html>
_129
<head></head>
_129
<body>
_129
<p>Hello -name-,<br />
_129
Thank you for your interest in our products. We have set up an appointment<br />
_129
to call you at -time- EST to discuss your needs in more detail.<br />
_129
_129
Regards,<br />
_129
Fred<br />
_129
</p>
_129
</body>
_129
</html>
_129
EOM
_129
_129
# Create the MIME message that will be sent. Check out MIME::Entity on CPAN for more details
_129
my $mime = MIME::Entity->build(Type => 'multipart/alternative' ,
_129
_129
Encoding => '-SUGGEST',
_129
From => $from,
_129
To => $to,
_129
Subject => 'Contact Response for <name> at <time>');
_129
_129
# Add the header
_129
$mime->head->add("X-SMTPAPI", $hdr->asJSON);
_129
_129
# Add body
_129
$mime->attach(Type => 'text/plain',
_129
Encoding =>'-SUGGEST',
_129
Data => $plain);
_129
_129
$mime->attach(Type => 'text/html',
_129
Encoding =>'-SUGGEST',
_129
Data => $html);
_129
_129
# Login credentials
_129
my $username = 'apikey';
_129
my $api_key = "your_api_key";
_129
_129
# Open a connection to the SendGrid mail server
_129
my $smtp = Net::SMTP->new('smtp.sendgrid.net',
_129
Port=> 587,
_129
Timeout => 20,
_129
Hello => "yourdomain.com");
_129
_129
# Authenticate
_129
$smtp->auth($username, $api_key);
_129
_129
# Send the rest of the SMTP stuff to the server
_129
$smtp->mail($from);
_129
$smtp->to($to);
_129
$smtp->data($mime->stringify);
_129
$smtp->quit();


Rate this page: