| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Mojo::Sendgrid::Mail; |
|
2
|
1
|
|
|
1
|
|
4
|
use Mojo::Base -base; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
98
|
use Mojo::UserAgent; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
5
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
has 'sendgrid' => sub { die }; # Mojo::Sendgrid object |
|
7
|
|
|
|
|
|
|
has 'ua' => sub { Mojo::UserAgent->new }; |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
# Implement the defined parameters from Sendgrid Web API v2 mail endpoint |
|
10
|
|
|
|
|
|
|
# Perld doesn't accept - so need to use _ and convert to - for the final req |
|
11
|
|
|
|
|
|
|
my %parameters = ( |
|
12
|
|
|
|
|
|
|
send => { |
|
13
|
|
|
|
|
|
|
required => [qw(to subject from)], |
|
14
|
|
|
|
|
|
|
require_one => [qw(text html)], |
|
15
|
|
|
|
|
|
|
optional => [qw(toname cc ccname bcc bccname fromname replyto date files content headers x_smtpapi)], |
|
16
|
|
|
|
|
|
|
}, |
|
17
|
|
|
|
|
|
|
); |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
has [@{$parameters{send}{required}}] => sub { die "required attribute missing" }; |
|
20
|
|
|
|
|
|
|
has [@{$parameters{send}{require_one}}]; |
|
21
|
|
|
|
|
|
|
has [@{$parameters{send}{optional}}]; |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub send { |
|
24
|
3
|
|
|
3
|
1
|
23
|
my $self = shift; |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
$self->ua->post( |
|
27
|
|
|
|
|
|
|
$self->sendgrid->apiurl => |
|
28
|
|
|
|
|
|
|
{Authorization => $self->_bearer} => |
|
29
|
|
|
|
|
|
|
form => $self->_form => |
|
30
|
|
|
|
|
|
|
# If there are any subscribers to the event issue the request non-blocking and emit event |
|
31
|
|
|
|
|
|
|
# Otherwise issue the request blocking |
|
32
|
1
|
|
|
1
|
|
9590
|
$self->sendgrid->has_subscribers('mail_send') ? sub {$self->sendgrid->emit(mail_send => @_)} : () |
|
33
|
3
|
100
|
|
|
|
9
|
); |
|
34
|
|
|
|
|
|
|
} |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# Create the hash to be supplied to the form option of Mojo::UserAgent |
|
37
|
|
|
|
|
|
|
sub _form { |
|
38
|
3
|
|
|
3
|
|
29
|
my $self = shift->_require_one; |
|
39
|
2
|
|
|
|
|
3
|
return {map {($_=~s/_/-/r)=>$self->$_} grep {$self->$_} @{$parameters{send}{required}}, @{$parameters{send}{optional}}}; |
|
|
9
|
|
|
|
|
32
|
|
|
|
33
|
|
|
|
|
91
|
|
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
4
|
|
|
40
|
|
|
|
|
|
|
} |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# I don't know how else to enforce requiring at least one attribute of a group |
|
43
|
|
|
|
|
|
|
# of options |
|
44
|
|
|
|
|
|
|
sub _require_one { |
|
45
|
3
|
|
|
3
|
|
4
|
my $self = shift; |
|
46
|
3
|
|
|
|
|
4
|
my $require_one = 0; |
|
47
|
3
|
|
50
|
|
|
5
|
push @{$parameters{send}{required}}, $_ and $require_one++ for grep {$self->$_} @{$parameters{send}{require_one}}; |
|
|
6
|
|
|
|
|
18
|
|
|
|
3
|
|
|
|
|
8
|
|
|
|
2
|
|
|
|
|
16
|
|
|
48
|
3
|
100
|
66
|
|
|
7
|
die sprintf "one of %s attribute missing", join ',', @{$parameters{send}{require_one}} if @{$parameters{send}{require_one}} && !$require_one; |
|
|
1
|
|
|
|
|
9
|
|
|
|
3
|
|
|
|
|
20
|
|
|
49
|
2
|
|
|
|
|
3
|
$self; |
|
50
|
|
|
|
|
|
|
} |
|
51
|
|
|
|
|
|
|
|
|
52
|
3
|
|
|
3
|
|
34
|
sub _bearer { sprintf "Bearer %s", shift->sendgrid->apikey } |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
1; |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=encoding utf8 |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=head1 NAME |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
Mojo::Sendgrid::Mail - Mail endpoint of the Sendgrid API implementation for |
|
61
|
|
|
|
|
|
|
the Mojolicious framework |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=head1 VERSION |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
0.01 |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
See L |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
L is the mail endpoint of the Sendgrid API and is non- |
|
74
|
|
|
|
|
|
|
blocking thanks to L from the wonderful L framework. |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
This class inherits from L. |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head1 EVENTS |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head2 mail_send |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Emitted after a Sendgrid API response is received, if there are any subscribers. |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head2 to (required) |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
Email address of the recipients. |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head2 from (required) |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
Email address of the sender. |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head2 subject (required) |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
The subject of your email. |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head2 text (must include at least one of the text or html attributes) |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
The plain text content of your email message. |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head2 html (must include at least one of the text or html attributes) |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
The HTML content of your email message. |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head2 toname |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Give a name to the recipient. |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head2 cc |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Email address of the CC'd recipients. |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head2 ccname |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
This is the name appended to the cc field. |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head2 bcc |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Email address of the BCC'd recipients. |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head2 bccname |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
This is the name appended to the bcc field. |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head2 fromname |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
This is the name appended to the from email field. |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head2 replyto |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
Append a reply-to field to your email message. |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head2 date |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
Specify the date header of your email. |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head2 files |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Files to be attached. |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head2 content |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Content IDs of the files to be used as inline images. |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head2 headers |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
A collection of key/value pairs in JSON format. |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head2 x_smtpapi |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
Please review the SMTP API to view documentation on what you can do with the |
|
153
|
|
|
|
|
|
|
JSON headers. |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head1 METHODS |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head2 send |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
$self = $self->send; |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
Build the Web request to send and deliver an email using Sendgrid. |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head1 COPYRIGHT |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
This program is free software, you can redistribute it and/or modify it under |
|
166
|
|
|
|
|
|
|
the terms of the Artistic License version 2.0. |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head1 AUTHOR |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Stefan Adams - C |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=cut |