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