Skip to contentSkip to navigationSkip to topbar
Page toolsOn this page
Looking for more inspiration?Visit the

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
1
# !/usr/bin/perl
2
3
# Version 1.0
4
# Last Updated 6/22/2009
5
use strict;
6
package SmtpApiHeader;
7
use JSON;
8
9
sub new
10
{
11
my $self = shift;
12
my @a = ();
13
$self = { 'data' => { }};
14
bless($self);
15
return $self;
16
}
17
18
sub addTo
19
{
20
my $self = shift;
21
my @to = @_;
22
push(@{$self->{data}->{to}}, @to);
23
}
24
25
sub addSubVal
26
{
27
my $self = shift;
28
my $var = shift;
29
my @val = @_;
30
31
if (!defined($self->{data}->{sub}->{$var}))
32
{
33
$self->{data}->{sub}->{$var} = ();
34
}
35
push(@{$self->{data}->{sub}->{$var}}, @val);
36
}
37
38
sub setUniqueArgs
39
{
40
my $self = shift;
41
my $val = shift;
42
if (ref($val) eq 'HASH')
43
{
44
$self->{data}->{unique_args} = $val;
45
}
46
}
47
48
sub setCategory
49
{
50
my $self = shift;
51
my $cat = shift;
52
$self->{data}->{category} = $cat;
53
}
54
55
sub addFilterSetting
56
{
57
my $self = shift;
58
my $filter = shift;
59
my $setting = shift;
60
my $val = shift;
61
if (!defined($self->{data}->{filters}->{$filter}))
62
{
63
$self->{data}->{filters}->{$filter} = {};
64
}
65
if (!defined($self->{data}->{filters}->{$filter}->{settings}))
66
{
67
$self->{data}->{filters}->{$filter}->{settings} = {};
68
}
69
$self->{data}->{filters}->{$filter}->{settings}->{$setting} = $val;
70
}
71
72
sub asJSON
73
{
74
my $self = shift;
75
my $json = JSON->new;
76
$json->space_before(1);
77
$json->space_after(1);
78
return $json->encode($self->{data});
79
}
80
81
sub as_string
82
{
83
my $self = shift;
84
my $json = $self->asJSON;
85
$json =~ s/(.{1,72})(\s)/$1\n /g;
86
my $str = "X-SMTPAPI: $json";
87
return $str;
88
}

1
# !/usr/bin/perl
2
use SmtpApiHeader;
3
4
my @receiver = ('kyle','bob','someguy');
5
6
my $hdr = SmtpApiHeader->new;
7
8
my $time = '1pm';
9
my $name = 'kyle';
10
11
$hdr->addFilterSetting('subscriptiontrack', 'enable', 1);
12
$hdr->addFilterSetting('twitter', 'enable', 1); #please check the apps available for your current package at https://sendgrid.com/pricing
13
$hdr->addTo(@receiver);
14
$hdr->addTo('kyle2');
15
16
$hdr->addSubVal('-time-', $time);
17
18
$hdr->addSubVal('-name-', $time);
19
$hdr->setUniqueArgs({'test'=>1, 'foo'=>2});
20
21
print $hdr->as_string;
22
23
print "\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/perl
2
use strict;
3
use SmtpApiHeader;
4
use MIME::Entity;
5
use Net::SMTP;
6
7
my $hdr = SmtpApiHeader->new;
8
9
# The list of addresses this message will be sent to
10
my @toList = ('isaac@example', 'tim@example', 'jose@example');
11
12
# The names of the recipients
13
my @nameList = ('Isaac', 'Tim', 'Jose');
14
15
# Another substitution variable
16
my @timeList = ('4pm', '1pm', '2pm');
17
18
# Set all of the above variables
19
$hdr->addTo(@toList);
20
$hdr->addSubVal('-name-', @nameList);
21
$hdr->addSubVal('-time-', @timeList);
22
23
# Specify that this is an initial contact message
24
$hdr->setCategory("initial");
25
26
# Enable a text footer and set it
27
$hdr->addFilterSetting('footer', 'enable', 1);
28
$hdr->addFilterSetting('footer', "text/plain", "Thank you for your business");
29
30
my $from = 'you@example.com';
31
32
# For multiple recipient emails, the 'to' address is irrelevant
33
my $to = 'example@example.com';
34
my $plain = <<EOM;
35
Hello -name-,
36
37
Thank you for your interest in our products. We have set up an appointment
38
to call you at -time- EST to discuss your needs in more detail.
39
40
Regards,
41
Fred
42
EOM
43
44
my $html = <<EOM;
45
<html>
46
<head></head>
47
<body>
48
<p>Hello -name-,<br />
49
Thank you for your interest in our products. We have set up an appointment<br />
50
to call you at -time- EST to discuss your needs in more detail.<br />
51
52
Regards,<br />
53
Fred<br />
54
</p>
55
</body>
56
</html>
57
EOM
58
59
# Create the MIME message that will be sent. Check out MIME::Entity on CPAN for more details
60
my $mime = MIME::Entity->build(Type => 'multipart/alternative' ,
61
62
Encoding => '-SUGGEST',
63
From => $from,
64
To => $to,
65
Subject => 'Contact Response for <name> at <time>');
66
67
# Add the header
68
$mime->head->add("X-SMTPAPI", $hdr->asJSON);
69
70
# Add body
71
$mime->attach(Type => 'text/plain',
72
Encoding =>'-SUGGEST',
73
Data => $plain);
74
75
$mime->attach(Type => 'text/html',
76
Encoding =>'-SUGGEST',
77
Data => $html);
78
79
# Login credentials
80
my $username = 'apikey';
81
my $api_key = "your_api_key";
82
83
# Open a connection to the SendGrid mail server
84
my $smtp = Net::SMTP->new('smtp.sendgrid.net',
85
Port=> 587,
86
Timeout => 20,
87
Hello => "example.com");
88
89
# Authenticate
90
$smtp->auth($username, $api_key);
91
92
# Send the rest of the SMTP stuff to the server
93
$smtp->mail($from);
94
$smtp->to($to);
95
$smtp->data($mime->stringify);
96
$smtp->quit();