blib/lib/Email/SendGrid/V3.pm

filter: Statementstmt 34.6% 89 / 257 Branchbran 17.3% 25 / 144 Conditioncond 17.7% 8 / 45 MC/DCmcdc 4.1% 1 / 24 Subroutinesub 35.4% 17 / 48 Podpod 100% 37 / 37 totaltotal 31.8% 177 / 555 CC 94 SCAR 30.6
CC
94
Cov
27%
ganalytics
72.0
add_attachment
56.0
subscription_tracking
42.0
CRAP 21.3
↑ Summary
1 package Email::SendGrid::V3;
2  
3 3 our $AUTHORITY = 'cpan:GSG';
4 # ABSTRACT: Class for building a message to be sent through the SendGrid v3 Web API
5 3 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 our $VERSION = 'v0.900.1'; # VERSION
7  
8 3 ▶use v5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 ▶use constant DEFAULT_ENDPOINT => 'https://api.sendgrid.com/v3/mail/send';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 3 ▶use namespace::clean; # don't export the above
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 #pod =encoding utf8
21 #pod
22 #pod =head1 SYNOPSIS
23 #pod
24 #pod use Email::SendGrid::V3;
25 #pod
26 #pod my $sg = Email::SendGrid::V3->new(api_key => 'XYZ123');
27 #pod
28 #pod my $result = $sg->from('nobody@example.com')
29 #pod ->subject('A test message for you')
30 #pod ->add_content('text/plain', 'This is a test message sent with SendGrid')
31 #pod ->add_envelope( to => [ 'nobody@example.com' ] )
32 #pod ->send;
33 #pod
34 #pod print $result->{success} ? "It worked" : "It failed: " . $result->{reason};
35 #pod
36 #pod =head1 DESCRIPTION
37 #pod
38 #pod This module allows for easy integration with the SendGrid email distribution
39 #pod service and its v3 Web API. All instance methods are chainable.
40 #pod
41 #pod For the full details of the SendGrid v3 API, see L<https://sendgrid.com/docs/API_Reference/api_v3.html>
42 #pod
43 #pod =head1 CLASS METHODS
44 #pod
45 #pod =head2 Creation
46 #pod
47 #pod =head3 new(%args)
48 #pod
49 #pod Creates a new Email::SendGrid object. Optional param: 'api_key'
50 #pod
51 #pod =cut
52  
53 3 ▶sub new {
Subroutine new: called CC=2 SCAR=7.4
Pod documented
54 3 my ($class, %args) = @_;
55 3 ▶ $class = ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
030
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
56  
57 3 my $self = bless +{
58 %args,
59 data => {},
60 }, $class;
61  
62 3 return $self;
63 }
64  
65 #pod =head1 INSTANCE METHODS
66 #pod
67 #pod =head2 Sending / Validating
68 #pod
69 #pod =head3 send(%args)
70 #pod
71 #pod Sends the API request and returns a result hashref with these keys:
72 #pod
73 #pod =over 4
74 #pod
75 #pod =item *
76 #pod
77 #pod C<success> - Boolean indicating whether the operation returned a 2XX status code
78 #pod
79 #pod =item *
80 #pod
81 #pod C<status> - The HTTP status code of the response
82 #pod
83 #pod =item *
84 #pod
85 #pod C<reason> - The response phrase returned by the server
86 #pod
87 #pod =item *
88 #pod
89 #pod C<content> - The body of the response, including a detailed error message, if any.
90 #pod
91 #pod =back
92 #pod
93 #pod =cut
94  
95 2 ▶sub send {
Subroutine send: called CC=5 SCAR=18.1
Pod documented
96 2 my ($self, %args) = @_;
97 2 ▶ my $api_key = $args{api_key} || $self->{api_key} or croak "API key is required to send";
Branch: 1 branch
BranchTrueFalseTotal
my $api_key = $args{"api_key"} || $self->{"api_key"} or croak("API key is required to send") 2 0 2
Condition33%1/3
$args{'api_key'} || $self->{'api_key'}
l!l&&r!l&&!r
020
MC/DC0%0/2
$args{'api_key'} || $self->{'api_key'}
$args{'api_key'} $self->{'api_key'}
Truth table33%1/3
A || B
A= $args{'api_key'}B= $self->{'api_key'}
ABresult
1X1
011
000
98 2 ▶ my $endpoint = $args{endpoint} || $self->{endpoint} || DEFAULT_ENDPOINT;
Condition40%2/5
$args{'endpoint'} || $self->{'endpoint'} || 'https://api.sendgrid.com/v3/mail/send'
l!l
02
$args{"endpoint"} or $self->{"endpoint"}
l!l&&r!l&&!r
002
MC/DC0%0/2
$args{'endpoint'} || $self->{'endpoint'} || 'https://api.sendgrid.com/v3/mail/send'
$args{"endpoint"} $self->{"endpoint"}
Truth table33%1/3
A or B
A= $args{"endpoint"}B= $self->{"endpoint"}
ABresult
1X1
011
000
99 2 my $payload = $self->_payload;
100  
101 2 my $http = HTTP::Tiny->new(
102 keep_alive => 0,
103 default_headers => {
104 'Content-Type' => 'application/json',
105 'Authorization' => "Bearer $api_key",
106 },
107 );
108  
109 2 my $response = $http->post(
110 $endpoint, { content => $payload },
111 );
112  
113 2 return $response;
114 }
115  
116 #pod =head3 validate(%args)
117 #pod
118 #pod Temporarily sets the 'sandbox_mode' flag to true, and submits the API request
119 #pod to SendGrid. Returns the same hashref format as send(). If the 'success' key
120 #pod is true, the API request is valid.
121 #pod
122 #pod =cut
123  
124 0 ▶sub validate {
Subroutine validate: not called CC=1 SCAR=6.9
Pod documented
125 0 my ($self, %args) = @_;
126  
127 0 local $self->{data}{mail_settings}{sandbox_mode} = { enable => JSON::true };
128  
129 0 return $self->send(%args);
130 }
131  
132 4 ▶sub _payload {
Subroutine _payload: called CC=1 SCAR=0.0
133 4 my ($self) = @_;
134 4 return JSON->new->utf8->canonical->encode( $self->{data} );
135 }
136  
137 #pod =head2 Personalizations / Envelopes
138 #pod
139 #pod =head3 $self->add_envelope(%args);
140 #pod
141 #pod Once you've defined the general message parameters (by setting from, content, etc)
142 #pod You must add at least one envelope. Each envelope represents one personalized copy
143 #pod of a message, and who should receive it. Some parameters can only be set at the message
144 #pod level, some only at the envelope level, and some at both (the envelop-level settings will
145 #pod override the message-level settings).
146 #pod
147 #pod You must specify at least the 'to' argument, which is an array of recipient emails. This
148 #pod can be a plain array of addresses, or an array of hashes with 'email' and 'name' keys.
149 #pod
150 #pod The 'cc' and 'bcc' arguments are optional, but follow the same format of the 'to' argument.
151 #pod
152 #pod In addition to specifying the envelope recipients via to/cc/bcc, you can also override the
153 #pod message 'subject', 'send_at', 'headers' hash, 'substitutions' hash, and 'custom_args' hash.
154 #pod See the message-level methods for more details on those parameters.
155 #pod
156 #pod =cut
157  
158 6 ▶sub add_envelope {
Subroutine add_envelope: called CC=15 SCAR=36.2
Pod documented
159 6 my ($self, %args) = @_;
160  
161 6 my $to = _standardize_recips('to', $args{to});
162 6 my $cc = _standardize_recips('cc', $args{cc});
163 6 my $bcc = _standardize_recips('bcc', $args{bcc});
164  
165 6 ▶ croak "Envelope must include at least one 'to' address" unless @$to;
Branch: 1 branch
BranchTrueFalseTotal
unless @$to 0 6 6
166  
167 6 my $envelope = { to => $to };
168 6 ▶ $envelope->{cc} = $cc if @$cc;
Branch: 1 branch
BranchTrueFalseTotal
if @$cc 0 6 6
169 6 ▶ $envelope->{bcc} = $bcc if @$bcc;
Branch: 1 branch
BranchTrueFalseTotal
if @$bcc 0 6 6
170  
171 6 ▶ $envelope->{subject} = $args{subject} if $args{subject};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"subject"} 0 6 6
172 6 ▶ $envelope->{send_at} = $args{send_at} if $args{send_at};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"send_at"} 0 6 6
173  
174 6 ▶ if ($args{headers}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"headers"}) 0 6 6
175 0 ▶ croak "Envelope headers must be a hashref" unless ref $args{headers} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args{"headers"} eq "HASH" 0 0 0
176 0 $envelope->{headers} = $args{headers};
177 }
178  
179 6 ▶ if ($args{substitutions}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"substitutions"}) 0 6 6
180 0 ▶ croak "Envelope substitutions must be a hashref" unless ref $args{substitutions} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args{"substitutions"} eq "HASH" 0 0 0
181 0 $envelope->{substitutions} = $args{substitutions};
182 }
183  
184 6 ▶ if ($args{dynamic_template_data}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"dynamic_template_data"}) 0 6 6
185 0 ▶ croak "Envelope dynamic_template_data must be a hashref" unless ref $args{dynamic_template_data} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args{"dynamic_template_data"} eq "HASH" 0 0 0
186 0 $envelope->{dynamic_template_data} = $args{dynamic_template_data};
187 }
188  
189 6 ▶ if ($args{custom_args}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"custom_args"}) 0 6 6
190 0 ▶ croak "Envelope custom args must be a hashref" unless ref $args{custom_args} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args{"custom_args"} eq "HASH" 0 0 0
191 0 $envelope->{custom_args} = $args{custom_args};
192 }
193  
194 6 ▶ $self->{data}{personalizations} ||= [];
Condition100%2/2
$self->{"data"}{"personalizations"} ||= []
l!l
33
MC/DC100%1/1
$self->{"data"}{"personalizations"} ||= []
$self->{"data"}{"personalizations"}
Truth table100%2/2
A
A= $self->{"data"}{"personalizations"}
Aresult
11
00
195  
196 6 push @{ $self->{data}{personalizations} }, $envelope;
197  
198 6 return $self;
199 }
200  
201 18 ▶sub _standardize_recips {
Subroutine _standardize_recips: called CC=9 SCAR=22.4
202 18 my ($name, $data) = @_;
203 18 my $reftype = ref $data;
204  
205 18 ▶ return [] unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 12 6 18
206  
207 6 ▶ if (! $reftype) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $reftype) { } 1 5 6
elsif ($reftype eq 'HASH') { } 1 4 5
elsif ($reftype ne 'ARRAY') { } 0 4 4
208 1 $data = [$data];
209 }
210 elsif ($reftype eq 'HASH') {
211 1 $data = [$data];
212 }
213 elsif ($reftype ne 'ARRAY') {
214 0 croak "Envelope $name must be an array";
215 }
216  
217 6 my @return;
218 6 foreach my $recip (@$data) {
219 7 ▶ next unless $recip;
Branch: 1 branch
BranchTrueFalseTotal
unless $recip 0 7 7
220  
221 7 my $recipreftype = ref $recip;
222  
223 7 ▶ if (! $recipreftype) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $recipreftype) { } 5 2 7
elsif ($recipreftype eq 'HASH') { } 2 0 2
224 5 push @return, {
225 email => $recip,
226 };
227 }
228 elsif ($recipreftype eq 'HASH') {
229 2 push @return, $recip;
230 }
231 else {
232 0 croak "Invalid envelope $name";
233 }
234 }
235  
236 6 return \@return;
237 }
238  
239 #pod =head3 $self->clear_envelopes();
240 #pod
241 #pod Clears all of the currently defined envelopes from this message.
242 #pod
243 #pod =cut
244  
245 0 ▶sub clear_envelopes {
Subroutine clear_envelopes: not called CC=1 SCAR=6.9
Pod documented
246 0 my ($self) = @_;
247  
248 0 delete $self->{data}{personalizations};
249  
250 0 return $self;
251 }
252  
253 #pod =head2 Messages
254 #pod
255 #pod =head3 $self->from($email, $name);
256 #pod
257 #pod Sets the name/email address of the sender.
258 #pod Email is required, name is optional.
259 #pod
260 #pod =cut
261  
262 3 ▶sub from {
Subroutine from: called CC=3 SCAR=11.3
Pod documented
263 3 my ($self, $email, $name) = @_;
264  
265 3 ▶ croak "From email is required" unless $email;
Branch: 1 branch
BranchTrueFalseTotal
unless $email 0 3 3
266  
267 3 $self->{data}{from} = { email => $email };
268 3 ▶ $self->{data}{from}{name} = $name if $name;
Branch: 1 branch
BranchTrueFalseTotal
if $name 0 3 3
269  
270 3 return $self;
271 }
272  
273 #pod =head3 $self->subject($subject);
274 #pod
275 #pod Sets the subject of the message.
276 #pod Required, but can be overridden at the message personalization level.
277 #pod
278 #pod =cut
279  
280 3 ▶sub subject {
Subroutine subject: called CC=2 SCAR=7.0
Pod documented
281 3 my ($self, $subject) = @_;
282  
283 3 ▶ croak "Subject is required" unless $subject;
Branch: 1 branch
BranchTrueFalseTotal
unless $subject 0 3 3
284  
285 3 $self->{data}{subject} = $subject;
286  
287 3 return $self;
288 }
289  
290 #pod =head3 $self->reply_to($email, $name);
291 #pod
292 #pod Sets the target that will be used if the recipient wants to reply to this message.
293 #pod Email is required, name is optional.
294 #pod
295 #pod =cut
296  
297 0 ▶sub reply_to {
Subroutine reply_to: not called CC=3 SCAR=24.8
Pod documented
298 0 my ($self, $email, $name) = @_;
299  
300 0 ▶ croak "Reply-to email is required" unless $email;
Branch: 1 branch
BranchTrueFalseTotal
unless $email 0 0 0
301  
302 0 $self->{data}{reply_to} = { email => $email };
303 0 ▶ $self->{data}{reply_to}{name} = $name if $name;
Branch: 1 branch
BranchTrueFalseTotal
if $name 0 0 0
304  
305 0 return $self;
306 }
307  
308 #pod =head3 $self->clear_content();
309 #pod
310 #pod Clears out all of the message body parts.
311 #pod
312 #pod =cut
313  
314 0 ▶sub clear_content {
Subroutine clear_content: not called CC=1 SCAR=6.9
Pod documented
315 0 my ($self) = @_;
316  
317 0 delete $self->{data}{content};
318  
319 0 return $self;
320 }
321  
322 #pod =head3 $self->add_content($type, $value);
323 #pod
324 #pod Adds a message body part. Both type and value are required.
325 #pod $type should be something like "text/plain" or "text/html".
326 #pod
327 #pod =cut
328  
329 3 ▶sub add_content {
Subroutine add_content: called CC=4 SCAR=15.0
Pod documented
330 3 my ($self, $type, $value) = @_;
331  
332 3 ▶ croak "Content type and value are required" unless $type && $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $type and $value 0 3 3
Condition33%1/3
$type and $value
!ll&&!rl&&r
003
MC/DC0%0/2
$type and $value
$type $value
Truth table33%1/3
A and B
A= $typeB= $value
ABresult
0X0
100
111
333  
334 3 ▶ $self->{data}{content} ||= [];
Condition50%1/2
$self->{"data"}{"content"} ||= []
l!l
03
MC/DC0%0/1
$self->{"data"}{"content"} ||= []
$self->{"data"}{"content"}
Truth table50%1/2
A
A= $self->{"data"}{"content"}
Aresult
11
00
335  
336 3 push @{ $self->{data}{content} }, {
337 type => $type,
338 value => $value,
339 };
340  
341 3 return $self;
342 }
343  
344 #pod =head3 $self->clear_attachments();
345 #pod
346 #pod Removes all attachments from this message.
347 #pod
348 #pod =cut
349  
350 0 ▶sub clear_attachments {
Subroutine clear_attachments: not called CC=1 SCAR=6.9
Pod documented
351 0 my ($self) = @_;
352  
353 0 delete $self->{data}{attachments};
354  
355 0 return $self;
356 }
357  
358 #pod =head3 $self->add_attachment($filename, $content, %args);
359 #pod
360 #pod Adds a new attachment to this message. $filename specifies the file name the recipient will see.
361 #pod $content should be the Base64-encoded data of the file. Optional arguments are 'type' (the mime type
362 #pod of the file, such as "image/jpeg"), 'disposition' must be "inline" or "attachment", and 'content_id'
363 #pod which is used to identify embedded inline attachments.
364 #pod
365 #pod =cut
366  
367 0 ▶sub add_attachment {
Subroutine add_attachment: not called CC=7 SCAR=40.3
Pod documented
368 0 my ($self, $filename, $content, %args) = @_;
369  
370 0 ▶ croak "Attachment filename and content are required" unless $filename && $content;
Branch: 1 branch
BranchTrueFalseTotal
unless $filename and $content 0 0 0
Condition0%0/3
$filename and $content
!ll&&!rl&&r
000
MC/DC0%0/2
$filename and $content
$filename $content
Truth table0%0/3
A and B
A= $filenameB= $content
ABresult
0X0
100
111
371  
372 0 my $new_attachment = {
373 filename => $filename,
374 content => $content,
375 };
376  
377 0 ▶ $new_attachment->{type} = $args{type} if $args{type};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"type"} 0 0 0
378 0 ▶ $new_attachment->{disposition} = $args{disposition} if $args{disposition};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"disposition"} 0 0 0
379 0 ▶ $new_attachment->{content_id} = $args{content_id} if $args{content_id};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"content_id"} 0 0 0
380  
381 0 ▶ $self->{data}{attachments} ||= [];
Condition0%0/2
$self->{"data"}{"attachments"} ||= []
l!l
00
MC/DC0%0/1
$self->{"data"}{"attachments"} ||= []
$self->{"data"}{"attachments"}
Truth table0%0/2
A
A= $self->{"data"}{"attachments"}
Aresult
11
00
382 0 push @{ $self->{data}{attachments} }, $new_attachment;
383  
384 0 return $self;
385 }
386  
387 #pod =head3 $self->template_id($template_id);
388 #pod
389 #pod Specifies the template you'd like to use for this message. Templates are managed via the
390 #pod SendGrid application website. If the template includes a subject or body, those do not need
391 #pod to be specified via this api.
392 #pod
393 #pod =cut
394  
395 0 ▶sub template_id {
Subroutine template_id: not called CC=2 SCAR=17.9
Pod documented
396 0 my ($self, $template_id) = @_;
397  
398 0 delete $self->{data}{template_id};
399 0 ▶ $self->{data}{template_id} = $template_id if $template_id;
Branch: 1 branch
BranchTrueFalseTotal
if $template_id 0 0 0
400  
401 0 return $self;
402 }
403  
404 #pod =head3 $self->clear_sections();
405 #pod
406 #pod Clears all substitution sections defined in this message.
407 #pod
408 #pod =cut
409  
410 0 ▶sub clear_sections {
Subroutine clear_sections: not called CC=1 SCAR=6.9
Pod documented
411 0 my ($self) = @_;
412  
413 0 delete $self->{data}{sections};
414  
415 0 return $self;
416 }
417  
418 #pod =head3 $self->remove_section($key);
419 #pod
420 #pod Removes one substitution section defined in this message.
421 #pod
422 #pod =cut
423  
424 0 ▶sub remove_section {
Subroutine remove_section: not called CC=2 SCAR=17.9
Pod documented
425 0 my ($self, $key) = @_;
426  
427 0 ▶ croak "Section key name is required" unless $key;
Branch: 1 branch
BranchTrueFalseTotal
unless $key 0 0 0
428  
429 0 delete $self->{data}{sections}{$key};
430  
431 0 return $self;
432 }
433  
434 #pod =head3 $self->set_section($key, $value);
435 #pod
436 #pod Sets one new substitution section for this message. Each occurrence of $key
437 #pod in each body part will be replaced with $value prior to personalization
438 #pod substitutions (if any).
439 #pod
440 #pod =cut
441  
442 0 ▶sub set_section {
Subroutine set_section: not called CC=3 SCAR=24.8
Pod documented
443 0 my ($self, $key, $value) = @_;
444  
445 0 ▶ croak "Section key name is required" unless $key;
Branch: 1 branch
BranchTrueFalseTotal
unless $key 0 0 0
446  
447 0 ▶ $self->{data}{sections} ||= {};
Condition0%0/2
$self->{"data"}{"sections"} ||= {}
l!l
00
MC/DC0%0/1
$self->{"data"}{"sections"} ||= {}
$self->{"data"}{"sections"}
Truth table0%0/2
A
A= $self->{"data"}{"sections"}
Aresult
11
00
448 0 $self->{data}{sections}{$key} = $value;
449  
450 0 return $self;
451 }
452  
453 #pod =head3 $self->set_sections(%sections);
454 #pod
455 #pod Sets all substitution sections for this message at once. For each key/val pair,
456 #pod occurrences of the key in the message body will be replaced by the value prior to
457 #pod personalization substitutions (if any).
458 #pod
459 #pod =cut
460  
461 0 ▶sub set_sections {
Subroutine set_sections: not called CC=2 SCAR=17.9
Pod documented
462 0 my ($self, %sections) = @_;
463  
464 0 delete $self->{data}{sections};
465 0 ▶ $self->{data}{sections} = \%sections if %sections;
Branch: 1 branch
BranchTrueFalseTotal
if %sections 0 0 0
466  
467 0 return $self;
468 }
469  
470 #pod =head3 $self->clear_headers();
471 #pod
472 #pod Clears all custom headers defined for this message.
473 #pod
474 #pod =cut
475  
476 0 ▶sub clear_headers {
Subroutine clear_headers: not called CC=1 SCAR=6.9
Pod documented
477 0 my ($self) = @_;
478  
479 0 delete $self->{data}{headers};
480  
481 0 return $self;
482 }
483  
484 #pod =head3 $self->set_headers(%headers);
485 #pod
486 #pod Sets all custom SMTP headers for this message at once. These must be properly encoded
487 #pod if they contain unicode characters. Must not be one of the reserved headers.
488 #pod
489 #pod These can be overridden at the message personalization level.
490 #pod
491 #pod =cut
492  
493 0 ▶sub set_headers {
Subroutine set_headers: not called CC=2 SCAR=17.9
Pod documented
494 0 my ($self, %headers) = @_;
495  
496 0 delete $self->{data}{headers};
497 0 ▶ $self->{data}{headers} = \%headers if %headers;
Branch: 1 branch
BranchTrueFalseTotal
if %headers 0 0 0
498  
499 0 return $self;
500 }
501  
502 #pod =head3 $self->clear_categories();
503 #pod
504 #pod Clears out all categories defined for this message.
505 #pod
506 #pod =cut
507  
508 0 ▶sub clear_categories {
Subroutine clear_categories: not called CC=1 SCAR=6.9
Pod documented
509 0 my ($self) = @_;
510  
511 0 delete $self->{data}{categories};
512  
513 0 return $self;
514 }
515  
516 #pod =head3 $self->set_categories(@categories);
517 #pod
518 #pod Sets the list of categories for this message. The list of categories must be
519 #pod unique and contain no more than 10 items.
520 #pod
521 #pod =cut
522  
523 0 ▶sub set_categories {
Subroutine set_categories: not called CC=3 SCAR=24.8
Pod documented
524 0 my ($self, @categories) = @_;
525  
526 0 ▶ croak "Cannot set more than 10 categories"
Branch: 1 branch
BranchTrueFalseTotal
if scalar @categories > 10 0 0 0
527 if scalar( @categories ) > 10;
528  
529 0 delete $self->{data}{categories};
530  
531 0 ▶ $self->{data}{categories} = \@categories if @categories;
Branch: 1 branch
BranchTrueFalseTotal
if @categories 0 0 0
532  
533 0 return $self;
534 }
535  
536 #pod =head3 $self->add_category($name);
537 #pod
538 #pod Adds a new category for this message. The list of categories must be
539 #pod unique and contain no more than 10 items.
540 #pod
541 #pod =cut
542  
543 0 ▶sub add_category {
Subroutine add_category: not called CC=4 SCAR=30.0
Pod documented
544 0 my ($self, $name) = @_;
545  
546 0 ▶ croak "Category name is required" unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
547  
548 0 ▶ $self->{data}{categories} ||= [];
Condition0%0/2
$self->{"data"}{"categories"} ||= []
l!l
00
MC/DC0%0/1
$self->{"data"}{"categories"} ||= []
$self->{"data"}{"categories"}
Truth table0%0/2
A
A= $self->{"data"}{"categories"}
Aresult
11
00
549  
550 croak "Cannot add more than 10 categories"
551 0 ▶ if scalar( @{ $self->{data}{categories} } ) > 9;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$self->{"data"}{"categories"};} > 9 0 0 0
552  
553 0 push @{ $self->{data}{categories} }, $name;
554  
555 0 return $self;
556 }
557  
558 #pod =head3 $self->clear_custom_args();
559 #pod
560 #pod Clears out all custom arguments defined for this message.
561 #pod
562 #pod =cut
563  
564 0 ▶sub clear_custom_args {
Subroutine clear_custom_args: not called CC=1 SCAR=6.9
Pod documented
565 0 my ($self) = @_;
566  
567 0 delete $self->{data}{custom_args};
568  
569 0 return $self;
570 }
571  
572 #pod =head3 $self->set_custom_args(%args);
573 #pod
574 #pod Sets all custom arguments defined for this message.
575 #pod These can be overridden at the message personalization level.
576 #pod The total size of custom arguments cannot exceed 10,000 bytes.
577 #pod
578 #pod =cut
579  
580 0 ▶sub set_custom_args {
Subroutine set_custom_args: not called CC=2 SCAR=17.9
Pod documented
581 0 my ($self, %args) = @_;
582  
583 0 delete $self->{data}{custom_args};
584  
585 0 ▶ $self->{data}{custom_args} = \%args if %args;
Branch: 1 branch
BranchTrueFalseTotal
if %args 0 0 0
586  
587 0 return $self;
588 }
589  
590 #pod =head3 $self->send_at($timestamp);
591 #pod
592 #pod A unix timestamp (seconds since 1970) specifying when to deliver this message.
593 #pod Cannot be more than 72 hours in the future.
594 #pod
595 #pod This can be overridden at the message personalization level.
596 #pod
597 #pod =cut
598  
599 0 ▶sub send_at {
Subroutine send_at: not called CC=2 SCAR=17.9
Pod documented
600 0 my ($self, $timestamp) = @_;
601  
602 0 delete $self->{data}{send_at};
603 0 ▶ $self->{data}{send_at} = $timestamp if $timestamp;
Branch: 1 branch
BranchTrueFalseTotal
if $timestamp 0 0 0
604  
605 0 return $self;
606 }
607  
608 #pod =head3 $self->batch_id($batch_id);
609 #pod
610 #pod Identifies a batch to include this message in. This batch ID can later be used
611 #pod to pause or cancel the delivery of a batch (if a future delivery time was set)
612 #pod
613 #pod =cut
614  
615 0 ▶sub batch_id {
Subroutine batch_id: not called CC=2 SCAR=17.9
Pod documented
616 0 my ($self, $batch_id) = @_;
617  
618 0 delete $self->{data}{batch_id};
619 0 ▶ $self->{data}{batch_id} = $batch_id if $batch_id;
Branch: 1 branch
BranchTrueFalseTotal
if $batch_id 0 0 0
620  
621 0 return $self;
622 }
623  
624 #pod =head3 $self->unsubscribe_group($group_id, @display_groups);
625 #pod
626 #pod If you've set up multiple unsubscribe groups in the SendGrid web application, this method
627 #pod allows you to specify which group this message belongs to. If this is set and the user
628 #pod unsubscribes from this message, they will only be added to the suppression list for that
629 #pod single group. If not set, they will be added to the global unsubscribe list.
630 #pod
631 #pod @display_groups is optional. If specified, when the user clicks "unsubscribe" they will be
632 #pod shown a list of these groups and allowed to choose which ones he/she would like to unsubscribe
633 #pod from.
634 #pod
635 #pod =cut
636  
637 0 ▶sub unsubscribe_group {
Subroutine unsubscribe_group: not called CC=4 SCAR=30.0
Pod documented
638 0 my ($self, $group_id, @display_groups) = @_;
639  
640 0 ▶ croak "Unsubscribe group ID is required" unless $group_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $group_id 0 0 0
641 0 ▶ croak "Cannot display more than 25 groups" if scalar(@display_groups) > 25;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @display_groups > 25 0 0 0
642  
643 0 $self->{data}{asm} = { group_id => $group_id };
644 0 ▶ $self->{data}{asm}{groups_to_display} = \@display_groups if @display_groups;
Branch: 1 branch
BranchTrueFalseTotal
if @display_groups 0 0 0
645  
646 0 return $self;
647 }
648  
649 #pod =head3 $self->ip_pool_name($pool_name);
650 #pod
651 #pod The IP Pool that you would like to send this email from.
652 #pod
653 #pod =cut
654  
655 0 ▶sub ip_pool_name {
Subroutine ip_pool_name: not called CC=2 SCAR=17.9
Pod documented
656 0 my ($self, $pool_name) = @_;
657  
658 0 delete $self->{data}{ip_pool_name};
659 0 ▶ $self->{data}{ip_pool_name} = $pool_name if $pool_name;
Branch: 1 branch
BranchTrueFalseTotal
if $pool_name 0 0 0
660  
661 0 return $self;
662 }
663  
664 #pod =head3 $self->click_tracking($enable, %args);
665 #pod
666 #pod Whether to enable click-tracking for this message. If enabled, any URLs in the body of this
667 #pod message will be rewritten to proxy through SendGrid for tracking purposes. This setting will
668 #pod overwrite the account-level setting if any. One optional argument is accepted: 'enable_text'
669 #pod which controls whether the link-rewriting is also performed for plaintext emails (the rewritten
670 #pod URL will be visible to the recipient)
671 #pod
672 #pod =cut
673  
674 0 ▶sub click_tracking {
Subroutine click_tracking: not called CC=5 SCAR=34.0
Pod documented
675 0 my ($self, $enable, %args) = @_;
676  
677 0 ▶ $enable = $enable ? JSON::true : JSON::false;
Branch: 1 branch
BranchTrueFalseTotal
$enable ? : 0 0 0
678  
679 0 ▶ $self->{data}{tracking_settings} ||= {};
Condition0%0/2
$self->{"data"}{"tracking_settings"} ||= {}
l!l
00
MC/DC0%0/1
$self->{"data"}{"tracking_settings"} ||= {}
$self->{"data"}{"tracking_settings"}
Truth table0%0/2
A
A= $self->{"data"}{"tracking_settings"}
Aresult
11
00
680 0 $self->{data}{tracking_settings}{click_tracking} = { enable => $enable };
681  
682 0 ▶ if (defined $args{enable_text}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"enable_text"}) 0 0 0
683 $self->{data}{tracking_settings}{click_tracking}{enable_text} =
684 0 ▶ $args{enable_text} ? JSON::true : JSON::false;
Branch: 1 branch
BranchTrueFalseTotal
$args{'enable_text'} ? : 0 0 0
685 }
686  
687 0 return $self;
688 }
689  
690 #pod =head3 $self->open_tracking($enable, %args);
691 #pod
692 #pod Whether to enable open-tracking for this message. If enabled, a single transparent pixel image
693 #pod is added to the HTML body of this message and used to determine if and when the recipient opens
694 #pod the message. This setting will overwrite the account-level setting if any. One optional argument
695 #pod is accepted: 'substitution_tag' which identifies a token in the message body that should be replaced
696 #pod with the tracking pixel.
697 #pod
698 #pod =cut
699  
700 0 ▶sub open_tracking {
Subroutine open_tracking: not called CC=4 SCAR=30.0
Pod documented
701 0 my ($self, $enable, %args) = @_;
702  
703 0 ▶ $enable = $enable ? JSON::true : JSON::false;
Branch: 1 branch
BranchTrueFalseTotal
$enable ? : 0 0 0
704  
705 0 ▶ $self->{data}{tracking_settings} ||= {};
Condition0%0/2
$self->{"data"}{"tracking_settings"} ||= {}
l!l
00
MC/DC0%0/1
$self->{"data"}{"tracking_settings"} ||= {}
$self->{"data"}{"tracking_settings"}
Truth table0%0/2
A
A= $self->{"data"}{"tracking_settings"}
Aresult
11
00
706 0 $self->{data}{tracking_settings}{open_tracking} = { enable => $enable };
707 $self->{data}{tracking_settings}{open_tracking}{substitution_tag} =
708 0 ▶ $args{substitution_tag} if $args{substitution_tag};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"substitution_tag"} 0 0 0
709  
710 0 return $self;
711 }
712  
713 #pod =head3 $self->subscription_tracking($enable, %args);
714 #pod
715 #pod Whether to enable a sendgrid-powered unsubscribe link in the footer of the email. You may pass
716 #pod optional arguments 'text' and 'html' to control the verbiage of the unsubscribe link used, OR
717 #pod 'substitution_tag' which is a token that will be replaced with the unsubscribe URL.
718 #pod This setting will overwrite the account-level setting if any.
719 #pod
720 #pod =cut
721  
722 0 ▶sub subscription_tracking {
Subroutine subscription_tracking: not called CC=6 SCAR=37.4
Pod documented
723 0 my ($self, $enable, %args) = @_;
724  
725 0 ▶ $enable = $enable ? JSON::true : JSON::false;
Branch: 1 branch
BranchTrueFalseTotal
$enable ? : 0 0 0
726  
727 0 my $new = { enable => $enable };
728 0 ▶ $new->{text} = $args{text} if $args{text};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"text"} 0 0 0
729 0 ▶ $new->{html} = $args{html} if $args{html};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"html"} 0 0 0
730 0 ▶ $new->{substitution_tag} = $args{substitution_tag} if $args{substitution_tag};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"substitution_tag"} 0 0 0
731  
732 0 ▶ $self->{data}{tracking_settings} ||= {};
Condition0%0/2
$self->{"data"}{"tracking_settings"} ||= {}
l!l
00
MC/DC0%0/1
$self->{"data"}{"tracking_settings"} ||= {}
$self->{"data"}{"tracking_settings"}
Truth table0%0/2
A
A= $self->{"data"}{"tracking_settings"}
Aresult
11
00
733 0 $self->{data}{tracking_settings}{subscription_tracking} = $new;
734  
735 0 return $self;
736 }
737  
738 #pod =head3 $self->ganalytics($enable, %args);
739 #pod
740 #pod Whether to enable google analytics tracking for this message. Optional arguments
741 #pod include 'utm_source', 'utm_medium', 'utm_term', 'utm_content', and 'utm_campaign'.
742 #pod This setting will overwrite the account-level setting if any.
743 #pod
744 #pod =cut
745  
746 0 ▶sub ganalytics {
Subroutine ganalytics: not called CC=8 SCAR=42.8
Pod documented
747 0 my ($self, $enable, %args) = @_;
748  
749 0 ▶ $enable = $enable ? JSON::true : JSON::false;
Branch: 1 branch
BranchTrueFalseTotal
$enable ? : 0 0 0
750  
751 0 my $new = { enable => $enable };
752 0 ▶ $new->{utm_source} = $args{utm_source} if $args{utm_source};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"utm_source"} 0 0 0
753 0 ▶ $new->{utm_medium} = $args{utm_medium} if $args{utm_medium};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"utm_medium"} 0 0 0
754 0 ▶ $new->{utm_term} = $args{utm_term} if $args{utm_term};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"utm_term"} 0 0 0
755 0 ▶ $new->{utm_content} = $args{utm_content} if $args{utm_content};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"utm_content"} 0 0 0
756 0 ▶ $new->{utm_campaign} = $args{utm_campaign} if $args{utm_campaign};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"utm_campaign"} 0 0 0
757  
758 0 ▶ $self->{data}{tracking_settings} ||= {};
Condition0%0/2
$self->{"data"}{"tracking_settings"} ||= {}
l!l
00
MC/DC0%0/1
$self->{"data"}{"tracking_settings"} ||= {}
$self->{"data"}{"tracking_settings"}
Truth table0%0/2
A
A= $self->{"data"}{"tracking_settings"}
Aresult
11
00
759 0 $self->{data}{tracking_settings}{ganalytics} = $new;
760  
761 0 return $self;
762 }
763  
764 #pod =head3 $self->bcc($enable, %args);
765 #pod
766 #pod Whether to BCC a monitoring account when sending this message. Optional arguments
767 #pod include 'email' for the address that will receive the BCC if one is not configured
768 #pod at the account level. This setting will overwrite the account-level setting if any.
769 #pod
770 #pod =cut
771  
772 0 ▶sub bcc {
Subroutine bcc: not called CC=4 SCAR=30.0
Pod documented
773 0 my ($self, $enable, %args) = @_;
774  
775 0 ▶ $enable = $enable ? JSON::true : JSON::false;
Branch: 1 branch
BranchTrueFalseTotal
$enable ? : 0 0 0
776  
777 0 ▶ $self->{data}{mail_settings} ||= {};
Condition0%0/2
$self->{"data"}{"mail_settings"} ||= {}
l!l
00
MC/DC0%0/1
$self->{"data"}{"mail_settings"} ||= {}
$self->{"data"}{"mail_settings"}
Truth table0%0/2
A
A= $self->{"data"}{"mail_settings"}
Aresult
11
00
778 0 $self->{data}{mail_settings}{bcc} = { enable => $enable };
779 0 ▶ $self->{data}{mail_settings}{bcc}{email} = $args{email} if $args{email};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"email"} 0 0 0
780  
781 0 return $self;
782 }
783  
784 #pod =head3 $self->bypass_list_management($enable);
785 #pod
786 #pod Whether to bypass the built-in suppression SendGrid provides, such as unsubscribed
787 #pod recipients, those that have bounced, or marked the emails as spam.
788 #pod This setting will overwrite the account-level setting if any.
789 #pod
790 #pod =cut
791  
792 0 ▶sub bypass_list_management {
Subroutine bypass_list_management: not called CC=3 SCAR=24.8
Pod documented
793 0 my ($self, $enable, %args) = @_;
794  
795 0 ▶ $enable = $enable ? JSON::true : JSON::false;
Branch: 1 branch
BranchTrueFalseTotal
$enable ? : 0 0 0
796  
797 0 ▶ $self->{data}{mail_settings} ||= {};
Condition0%0/2
$self->{"data"}{"mail_settings"} ||= {}
l!l
00
MC/DC0%0/1
$self->{"data"}{"mail_settings"} ||= {}
$self->{"data"}{"mail_settings"}
Truth table0%0/2
A
A= $self->{"data"}{"mail_settings"}
Aresult
11
00
798 0 $self->{data}{mail_settings}{bypass_list_management} = { enable => $enable };
799  
800 0 return $self;
801 }
802  
803 #pod =head3 $self->footer($enable, %args);
804 #pod
805 #pod Whether to add a footer to the outgoing message. Optional arguments include 'html' and
806 #pod 'text' to specify the footers that will be used for each message body type.
807 #pod This setting will overwrite the account-level setting if any.
808 #pod
809 #pod =cut
810  
811 0 ▶sub footer {
Subroutine footer: not called CC=5 SCAR=34.0
Pod documented
812 0 my ($self, $enable, %args) = @_;
813  
814 0 ▶ $enable = $enable ? JSON::true : JSON::false;
Branch: 1 branch
BranchTrueFalseTotal
$enable ? : 0 0 0
815  
816 0 ▶ $self->{data}{mail_settings} ||= {};
Condition0%0/2
$self->{"data"}{"mail_settings"} ||= {}
l!l
00
MC/DC0%0/1
$self->{"data"}{"mail_settings"} ||= {}
$self->{"data"}{"mail_settings"}
Truth table0%0/2
A
A= $self->{"data"}{"mail_settings"}
Aresult
11
00
817 0 $self->{data}{mail_settings}{footer} = { enable => $enable };
818 0 ▶ $self->{data}{mail_settings}{footer}{text} = $args{text} if $args{text};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"text"} 0 0 0
819 0 ▶ $self->{data}{mail_settings}{footer}{html} = $args{html} if $args{html};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"html"} 0 0 0
820  
821 0 return $self;
822 }
823  
824 #pod =head3 $self->sandbox_mode($enable);
825 #pod
826 #pod Whether to enable sandbox mode. When enabled, SendGrid will validate the contents of this
827 #pod API request for correctness, but will not actually send the message.
828 #pod
829 #pod =cut
830  
831 0 ▶sub sandbox_mode {
Subroutine sandbox_mode: not called CC=3 SCAR=24.8
Pod documented
832 0 my ($self, $enable, %args) = @_;
833  
834 0 ▶ $enable = $enable ? JSON::true : JSON::false;
Branch: 1 branch
BranchTrueFalseTotal
$enable ? : 0 0 0
835  
836 0 ▶ $self->{data}{mail_settings} ||= {};
Condition0%0/2
$self->{"data"}{"mail_settings"} ||= {}
l!l
00
MC/DC0%0/1
$self->{"data"}{"mail_settings"} ||= {}
$self->{"data"}{"mail_settings"}
Truth table0%0/2
A
A= $self->{"data"}{"mail_settings"}
Aresult
11
00
837 0 $self->{data}{mail_settings}{sandbox_mode} = { enable => $enable };
838  
839 0 return $self;
840 }
841  
842 #pod =head3 $self->spam_check($enable, %args);
843 #pod
844 #pod Whether to perform a spam check on this message prior to sending. If the message fails the
845 #pod spam check, it will be dropped and not sent. Optional parameters include 'threshold' - an
846 #pod integer score value from 1-10 (default 5) over which a message will be classified as spam,
847 #pod and 'post_to_url' - a SendGrid inbound message parsing URL that will be used to post back
848 #pod notifications of messages identified as spam and dropped. These settings will overwrite
849 #pod the account-level settings if any.
850 #pod
851 #pod =cut
852  
853 0 ▶sub spam_check {
Subroutine spam_check: not called CC=5 SCAR=34.0
Pod documented
854 0 my ($self, $enable, %args) = @_;
855  
856 0 ▶ $enable = $enable ? JSON::true : JSON::false;
Branch: 1 branch
BranchTrueFalseTotal
$enable ? : 0 0 0
857  
858 0 ▶ $self->{data}{mail_settings} ||= {};
Condition0%0/2
$self->{"data"}{"mail_settings"} ||= {}
l!l
00
MC/DC0%0/1
$self->{"data"}{"mail_settings"} ||= {}
$self->{"data"}{"mail_settings"}
Truth table0%0/2
A
A= $self->{"data"}{"mail_settings"}
Aresult
11
00
859 0 $self->{data}{mail_settings}{spam_check} = { enable => $enable };
860 0 ▶ $self->{data}{mail_settings}{spam_check}{threshold} = $args{threshold} if $args{threshold};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"threshold"} 0 0 0
861 0 ▶ $self->{data}{mail_settings}{spam_check}{post_to_url} = $args{post_to_url} if $args{post_to_url};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"post_to_url"} 0 0 0
862  
863 0 return $self;
864 }
865  
866 3 1;
867  
868 __END__
↑ Summary