line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mail::SendGrid::SmtpApiHeader; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
43299
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
38
|
|
4
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
32
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
5
|
use JSON; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
4570
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub new { |
11
|
1
|
|
|
1
|
1
|
12
|
my $class = shift; |
12
|
1
|
|
33
|
|
|
11
|
return bless { 'data' => { } }, ref $class || $class; |
13
|
|
|
|
|
|
|
} |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub addTo { |
17
|
1
|
|
|
1
|
1
|
406
|
my $self = shift; |
18
|
1
|
|
|
|
|
2
|
push @{ $self->{data}{to} }, @_; |
|
1
|
|
|
|
|
8
|
|
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub addSubVal { |
23
|
2
|
|
|
2
|
1
|
9
|
my $self = shift; |
24
|
2
|
|
|
|
|
3
|
my $var = shift; |
25
|
2
|
|
|
|
|
2
|
push @{ $self->{data}{sub}{$var} }, @_; |
|
2
|
|
|
|
|
11
|
|
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub setUniqueArgs { |
30
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
31
|
0
|
|
|
|
|
0
|
my $val = shift; |
32
|
0
|
0
|
|
|
|
0
|
$self->{data}{unique_args} = $val if ref $val eq 'HASH'; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub setCategory { |
37
|
1
|
|
|
1
|
1
|
5
|
my $self = shift; |
38
|
1
|
|
|
|
|
2
|
my $cat = shift; |
39
|
1
|
|
|
|
|
4
|
$self->{data}{category} = $cat; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub addFilterSetting { |
44
|
3
|
|
|
3
|
1
|
13
|
my $self = shift; |
45
|
3
|
|
|
|
|
4
|
my $filter = shift; |
46
|
|
|
|
|
|
|
|
47
|
3
|
|
100
|
|
|
20
|
my ($settings) = ( $self->{data}{filters}{$filter}{settings} ||= {} ); |
48
|
|
|
|
|
|
|
|
49
|
3
|
|
|
|
|
10
|
while (@_) { |
50
|
4
|
|
|
|
|
5
|
my $setting = shift; |
51
|
4
|
|
|
|
|
5
|
my $value = shift; |
52
|
4
|
|
|
|
|
16
|
$settings->{$setting} = $value; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub addUniqueArgs { |
58
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
59
|
|
|
|
|
|
|
|
60
|
0
|
|
0
|
|
|
0
|
my ($unique_args) = ( $self->{data}{unique_args} ||= {} ); |
61
|
|
|
|
|
|
|
|
62
|
0
|
|
|
|
|
0
|
while (@_) { |
63
|
0
|
|
|
|
|
0
|
my $name = shift; |
64
|
0
|
|
|
|
|
0
|
my $value = shift; |
65
|
0
|
|
|
|
|
0
|
$unique_args->{$name} = $value; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
my $JSON; |
71
|
|
|
|
|
|
|
sub asJSON { |
72
|
1
|
|
|
1
|
1
|
2
|
my $self = shift; |
73
|
1
|
|
33
|
|
|
8
|
$JSON ||= _build_json(); |
74
|
1
|
|
|
|
|
29
|
return $JSON->encode($self->{data}); |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
my $JSON_PRETTY; |
79
|
|
|
|
|
|
|
sub asJSONPretty { |
80
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
81
|
0
|
|
0
|
|
|
0
|
$JSON_PRETTY ||= _build_json()->pretty(1); |
82
|
0
|
|
|
|
|
0
|
return $JSON_PRETTY->encode($self->{data}); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub as_string { |
87
|
1
|
|
|
1
|
1
|
5
|
my $self = shift; |
88
|
1
|
|
|
|
|
3
|
my $json = $self->asJSON; |
89
|
1
|
|
|
|
|
29
|
$json =~ s/(.{1,72})(\s)/$1\n /g; |
90
|
1
|
|
|
|
|
4
|
my $str = "X-SMTPAPI: $json"; |
91
|
1
|
|
|
|
|
3
|
return $str; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub _build_json { |
96
|
1
|
|
|
1
|
|
12
|
my $json = JSON->new; |
97
|
1
|
|
|
|
|
9
|
$json->space_before(1); |
98
|
1
|
|
|
|
|
6
|
$json->space_after(1); |
99
|
1
|
|
|
|
|
7
|
$json->ascii(1); |
100
|
1
|
|
|
|
|
9
|
return $json; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
1; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head1 NAME |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Mail::SendGrid::SmtpApiHeader - generate SendGrid's SMTP extension header |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head1 SYNOPSIS |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
use Mail::SendGrid::SmtpApiHeader; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
# Use AnyEvent as usual |
115
|
|
|
|
|
|
|
my $cond = AnyEvent->condvar; |
116
|
|
|
|
|
|
|
http_get "http://search.cpan.org/", sub { $cond->send(); }; |
117
|
|
|
|
|
|
|
$cond->recv(); |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=head1 DESCRIPTION |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
This module generates the custom SMTP extension header used to configure SendGrid's SMTP |
122
|
|
|
|
|
|
|
platform. |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head1 METHODS |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head2 new |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
Used to create a new instance. The constructor takes no arguments. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
my $headers = Mail::SendGrid::SmtpApiHeader->new(); |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head2 addTo |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
Adds the given email address to the list of recipients (i.e. I). |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
$headers->addTo( |
137
|
|
|
|
|
|
|
'me@example.com', |
138
|
|
|
|
|
|
|
'you@example.com', |
139
|
|
|
|
|
|
|
); |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head2 addSubVal |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
Specify substitution variables for multi recipient e-mails. This would allow you to, for |
144
|
|
|
|
|
|
|
example, substitute the string with a recipient's name. 'val' can be either a scalar or an |
145
|
|
|
|
|
|
|
array. It is the user's responsibility to ensure that there are an equal number of |
146
|
|
|
|
|
|
|
substitution values as there are recipients. |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
$headers->addSubVal(names => "Me", "You"); |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head2 setUniqueArgs |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
Specify any unique argument values. |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
$headers->setUniqueArgs( |
155
|
|
|
|
|
|
|
{ |
156
|
|
|
|
|
|
|
test => 1, |
157
|
|
|
|
|
|
|
foo => 2, |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
); |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head2 setCategory |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
Sets a category for an e-mail to be logged as. You may use any category name you like. |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
$header->setCategory('send-001'); |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=head2 addFilterSetting |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
Adds/changes a setting for a filter. Settings specified in the header will override |
170
|
|
|
|
|
|
|
configured settings. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
# Enable a text footer and set it |
173
|
|
|
|
|
|
|
$header->addFilterSetting(footer => |
174
|
|
|
|
|
|
|
'enable', 1, |
175
|
|
|
|
|
|
|
'text/plain', "Thank you for your business", |
176
|
|
|
|
|
|
|
); |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head2 addUniqueArgs |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
Add unique argument values to the existing unique arguments. |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
$headers->addUniqueArgs( |
183
|
|
|
|
|
|
|
test => 1, |
184
|
|
|
|
|
|
|
foo => 2, |
185
|
|
|
|
|
|
|
); |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=head2 as_string |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
Returns the full header which can be inserted into an e-mail. |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=head2 asJSON |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
Returns the JSON version of the requested data. |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=head2 asJSONPretty |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
Returns the JSON version of the requested data in a more human readable way. |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head1 AUTHOR |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
SendGrid |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
Booking.com |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
Emmanuel Rodriguez |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=head1 COPYRIGHT |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
Copyright (C) 2010 by SendGrid |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
Copyright (C) 2011 by Booking.com |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
Copyright (C) 2012 by Emmanuel Rodriguez |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=cut |