forked from sendgrid/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperl.html
More file actions
238 lines (195 loc) · 4.97 KB
/
Copy pathperl.html
File metadata and controls
238 lines (195 loc) · 4.97 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
---
layout: page
weight: 0
title: Perl
navigation:
show: true
---
{% anchor h2 %}
SmtpApiHeader.pm
{% endanchor %}
{% codeblock lang:perl %}
#!/usr/bin/perl
# Version 1.0
# Last Updated 6/22/2009
use strict;
package SmtpApiHeader;
use JSON;
sub new
{
my $self = shift;
my @a = ();
$self = { 'data' => { }};
bless($self);
return $self;
}
sub addTo
{
my $self = shift;
my @to = @_;
push(@{$self->{data}->{to}}, @to);
}
sub addSubVal
{
my $self = shift;
my $var = shift;
my @val = @_;
if (!defined($self->{data}->{sub}->{$var}))
{
$self->{data}->{sub}->{$var} = ();
}
push(@{$self->{data}->{sub}->{$var}}, @val);
}
sub setUniqueArgs
{
my $self = shift;
my $val = shift;
if (ref($val) eq 'HASH')
{
$self->{data}->{unique_args} = $val;
}
}
sub setCategory
{
my $self = shift;
my $cat = shift;
$self->{data}->{category} = $cat;
}
sub addFilterSetting
{
my $self = shift;
my $filter = shift;
my $setting = shift;
my $val = shift;
if (!defined($self->{data}->{filters}->{$filter}))
{
$self->{data}->{filters}->{$filter} = {};
}
if (!defined($self->{data}->{filters}->{$filter}->{settings}))
{
$self->{data}->{filters}->{$filter}->{settings} = {};
}
$self->{data}->{filters}->{$filter}->{settings}->{$setting} = $val;
}
sub asJSON
{
my $self = shift;
my $json = JSON->new;
$json->space_before(1);
$json->space_after(1);
return $json->encode($self->{data});
}
sub as_string
{
my $self = shift;
my $json = $self->asJSON;
$json =~ s/(.{1,72})(\s)/$1\n /g;
my $str = "X-SMTPAPI: $json";
return $str;
}
{% endcodeblock %}
{% anchor h2 %}
Example Perl Usage
{% endanchor %}
{% codeblock lang:perl %}
#!/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 sendgrid.com/pricing.html
$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>
{% anchor h2 %}
Full Perl Example
{% endanchor %}
<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 subsitution 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 = '[email protected]';
# For multiple recipient e-mails, the 'to' address is irrelivant
my $to = '[email protected]';
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 = '[email protected]';
my $password = "yourpassword";
# Open a connection to the SendGrid mail server
my $smtp = Net::SMTP->new('smtp.sendgrid.net',
Port=> 25,
Timeout => 20,
Hello => "yourdomain.com");
# Authenticate
$smtp->auth($username, $password);
# Send the rest of the SMTP stuff to the server
$smtp->mail($from);
$smtp->to($to);
$smtp->data($mime->stringify);
$smtp->quit();
{% endcodeblock %}