Skip to contentSkip to navigationSkip to topbar
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
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";
24
</code>
25
26
## Full Perl Example
27
<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/perl
35
use strict;
36
use SmtpApiHeader;
37
use MIME::Entity;
38
use Net::SMTP;
39
40
my $hdr = SmtpApiHeader->new;
41
42
# The list of addresses this message will be sent to
43
my @toList = ('isaac@example', 'tim@example', 'jose@example');
44
45
# The names of the recipients
46
my @nameList = ('Isaac', 'Tim', 'Jose');
47
48
# Another substitution variable
49
my @timeList = ('4pm', '1pm', '2pm');
50
51
# Set all of the above variables
52
$hdr->addTo(@toList);
53
$hdr->addSubVal('-name-', @nameList);
54
$hdr->addSubVal('-time-', @timeList);
55
56
# Specify that this is an initial contact message
57
$hdr->setCategory("initial");
58
59
# Enable a text footer and set it
60
$hdr->addFilterSetting('footer', 'enable', 1);
61
$hdr->addFilterSetting('footer', "text/plain", "Thank you for your business");
62
63
my $from = 'you@yourdomain.com';
64
65
# For multiple recipient emails, the 'to' address is irrelevant
66
my $to = 'example@example.com';
67
my $plain = <<EOM;
68
Hello -name-,
69
70
Thank you for your interest in our products. We have set up an appointment
71
to call you at -time- EST to discuss your needs in more detail.
72
73
Regards,
74
Fred
75
EOM
76
77
my $html = <<EOM;
78
<html>
79
<head></head>
80
<body>
81
<p>Hello -name-,<br />
82
Thank you for your interest in our products. We have set up an appointment<br />
83
to call you at -time- EST to discuss your needs in more detail.<br />
84
85
Regards,<br />
86
Fred<br />
87
</p>
88
</body>
89
</html>
90
EOM
91
92
# Create the MIME message that will be sent. Check out MIME::Entity on CPAN for more details
93
my $mime = MIME::Entity->build(Type => 'multipart/alternative' ,
94
95
Encoding => '-SUGGEST',
96
From => $from,
97
To => $to,
98
Subject => 'Contact Response for <name> at <time>');
99
100
# Add the header
101
$mime->head->add("X-SMTPAPI", $hdr->asJSON);
102
103
# Add body
104
$mime->attach(Type => 'text/plain',
105
Encoding =>'-SUGGEST',
106
Data => $plain);
107
108
$mime->attach(Type => 'text/html',
109
Encoding =>'-SUGGEST',
110
Data => $html);
111
112
# Login credentials
113
my $username = 'apikey';
114
my $api_key = "your_api_key";
115
116
# Open a connection to the SendGrid mail server
117
my $smtp = Net::SMTP->new('smtp.sendgrid.net',
118
Port=> 587,
119
Timeout => 20,
120
Hello => "yourdomain.com");
121
122
# Authenticate
123
$smtp->auth($username, $api_key);
124
125
# Send the rest of the SMTP stuff to the server
126
$smtp->mail($from);
127
$smtp->to($to);
128
$smtp->data($mime->stringify);
129
$smtp->quit();

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.