blib/lib/Email/Sender/Transport/Mailgun.pm

filter: Statementstmt 100% 79 / 79 Branchbran 78.5% 11 / 14 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 20 / 20 Podpod 0.0% 0 / 3 totaltotal 95.2% 120 / 126 CC 11 SCAR 8.0
CC
11
Cov
97%
send_email
5.0
failure
3.0
_build_uri
3.0
CRAP 2.2
↑ Summary Build »
1 package Email::Sender::Transport::Mailgun;
2 6 our $VERSION = "0.03";
3  
4 6 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 with 'Email::Sender::Transport';
6  
7 6 ▶use HTTP::Tiny qw( );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use HTTP::Tiny::Multipart qw( );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use JSON::MaybeXS qw( );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use MooX::Types::MooseLike::Base qw( ArrayRef Enum Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 {
13 package
14 Email::Sender::Success::MailgunSuccess;
15 6 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 extends 'Email::Sender::Success';
17 6 has id => (
18 is => 'ro',
19 required => 1,
20 );
21 6 ▶ no Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 }
23  
24 6 has [qw( api_key domain )] => (
25 is => 'ro',
26 required => 1,
27 isa => Str,
28 );
29  
30 has [qw( tag )] => (
31 is => 'ro',
32 predicate => 1,
33 isa => ArrayRef[Str],
34 1 ▶ coerce => sub { ref $_[0] ? $_[0] : [ split(/,\s*/, $_[0]) ] },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 1 1
35 6 );
36  
37 has deliverytime => (
38 is => 'ro',
39 predicate => 1,
40 isa => Str,
41 coerce => sub {
42 1 ▶ ref $_[0] eq 'DateTime'
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'DateTime' ? : 1 0 1
43 ? $_[0]->strftime('%a, %d %b %Y %H:%M:%S %z') : $_[0]
44 },
45 6 );
46  
47 6 has [qw( dkim testmode tracking tracking_opens )] => (
48 is => 'ro',
49 predicate => 1,
50 isa => Enum[qw( yes no )],
51 );
52  
53 6 has tracking_clicks => (
54 is => 'ro',
55 predicate => 1,
56 isa => Enum[qw( yes no htmlonly )],
57 );
58  
59 6 has region => (
60 is => 'ro',
61 predicate => 1,
62 isa => Enum[qw( us eu )],
63 );
64  
65 has base_uri => (
66 is => 'lazy',
67 3 ▶ builder => sub { 'https://api.mailgun.net/v3' },
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _build_base_uri: called CC=1 SCAR=0.0
68 6 );
69  
70 6 has uri => (
71 is => 'lazy',
72 );
73  
74 has ua => (
75 is => 'lazy',
76 1 ▶ builder => sub { HTTP::Tiny->new(verify_SSL => 1) },
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _build_ua: called CC=1 SCAR=0.0
77 6 );
78  
79 has json => (
80 is => 'lazy',
81 1 ▶ builder => sub { JSON::MaybeXS->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _build_json: called CC=1 SCAR=0.0
82 6 );
83  
84 # https://documentation.mailgun.com/en/latest/api-sending.html#sending
85 sub send_email {
86 4 ▶ my ($self, $email, $env) = @_;
Subroutine send_email: called CC=5 SCAR=16.1
Pod undocumented
87  
88 my $content = {
89 4 to => ref $env->{to} ? join(',', @{ $env->{to} }) : $env->{to},
90 4 ▶ message => {
Branch: 1 branch
BranchTrueFalseTotal
ref $env->{'to'} ? : 4 0 4
91 filename => 'message.mime',
92 content => $email->as_string,
93 },
94 };
95  
96 4 my @options = qw(
97 deliverytime dkim tag testmode tracking tracking_clicks tracking_opens
98 );
99  
100 4 for my $option (@options) {
101 28 my $has_option = "has_$option";
102 28 ▶ if ($self->$has_option) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->$has_option) 3 25 28
103 3 my $key = "o:$option";
104 3 $key =~ tr/_/-/;
105 3 $content->{$key} = $self->$option;
106 }
107 }
108  
109 4 my $uri = $self->uri . '/messages.mime';
110 4 my $response = $self->ua->post_multipart($uri, $content);
111  
112 $self->failure($response, $env->{to})
113 4 ▶ unless $response->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $response->{"success"} 3 1 4
114  
115 1 return $self->success($response);
116 }
117  
118 sub success {
119 1 ▶ my ($self, $response) = @_;
Subroutine success: called CC=1 SCAR=0.0
Pod undocumented
120  
121 1 my $content = $self->json->decode($response->{content});
122 1 return Email::Sender::Success::MailgunSuccess->new(id => $content->{id});
123 }
124  
125 sub failure {
126 3 ▶ my ($self, $response, $recipients) = @_;
Subroutine failure: called CC=3 SCAR=11.0
Pod undocumented
127  
128 # Most errors have { message => $message } in the content, some, such as
129 # an auth error, have just a plain string.
130 3 my $content = eval { $self->json->decode($response->{content}) };
131 my $message = $content && $content->{message}
132 3 ▶ ? $content->{message} : $response->{content};
Branch: 1 branch
BranchTrueFalseTotal
$content && $content->{'message'} ? : 1 2 3
Condition100%3/3
$content and $content->{"message"}
!ll&&!rl&&r
111
MC/DC100%2/2
$content and $content->{"message"}
$content $content->{"message"}
Truth table100%3/3
A and B
A= $contentB= $content->{"message"}
ABresult
0X0
100
111
133  
134 3 Email::Sender::Failure->throw({
135 message => $message,
136 recipients => $recipients,
137 });
138 }
139  
140 sub _build_uri {
141 7 ▶ my $self = shift;
Subroutine _build_uri: called CC=3 SCAR=11.0
142  
143 7 my ($proto, $rest) = split('://', $self->base_uri);
144 7 my $domain = $self->domain;
145  
146 # Percent-escape anything other than alphanumeric and - _ . ~
147 # https://github.com/sdt/Email-Sender-Transport-Mailgun/issues/4
148 7 my $api_key = $self->api_key;
149 7 $api_key =~ s/[^-_.~0-9a-zA-Z]/sprintf('%%%02x',ord($&))/eg;
150  
151 # adapt endpoint based on region setting.
152 7 ▶ $rest =~ s/(\.mailgun)/sprintf('.%s%s', $self->region, $1)/e
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->region and $self->region ne "us" 1 6 7
Condition100%3/3
defined $self->region and $self->region ne "us"
!ll&&!rl&&r
511
MC/DC100%2/2
defined $self->region and $self->region ne "us"
defined $self->region $self->region ne "us"
Truth table100%3/3
A and B
A= defined $self->regionB= $self->region ne "us"
ABresult
0X0
100
111
153 if defined $self->region && $self->region ne 'us';
154  
155 7 return "$proto://api:$api_key\@$rest/$domain";
156 }
157  
158 6 ▶no Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
159 6 1;
160 __END__
161  
162 =encoding utf-8
163  
164 =for stopwords deliverytime dkim hardcode mailouts prepend templated testmode
165  
166 =head1 NAME
167  
168 Email::Sender::Transport::Mailgun - Mailgun transport for Email::Sender
169  
170 =head1 SYNOPSIS
171  
172 use Email::Sender::Simple qw( sendmail );
173 use Email::Sender::Transport::Mailgun qw( );
174  
175 my $transport = Email::Sender::Transport::Mailgun->new(
176 api_key => '...',
177 domain => '...',
178 );
179  
180 my $message = ...;
181  
182 sendmail($message, { transport => $transport });
183  
184 =head1 DESCRIPTION
185  
186 This transport delivers mail via Mailgun's messages.mime API.
187  
188 =head2 Why use this module?
189  
190 The SMTP transport can also be used to send messages through Mailgun. In this
191 case, Mailgun options must be specified with Mailgun-specific MIME headers.
192  
193 This module exposes those options as attributes, which can be set in code, or
194 via C<EMAIL_SENDER_TRANSPORT_> environment variables.
195  
196 =head2 Why not use this module?
197  
198 This module uses Mailgun's messages.mime API, not the full-blown messages API.
199  
200 If you want to use advanced Mailgun features such as templated batch mailouts
201 or mailing lists, you're better off using something like L<WebService::Mailgun>
202 or L<WWW::Mailgun>.
203  
204 =head1 REQUIRED ATTRIBUTES
205  
206 The attributes all correspond directly to Mailgun parameters.
207  
208 =head2 api_key
209  
210 Mailgun API key. See L<https://documentation.mailgun.com/en/latest/api-intro.html#authentication-1>
211  
212 =head2 domain
213  
214 Mailgun domain. See L<https://documentation.mailgun.com/en/latest/api-intro.html#base-url-1>
215  
216 =head1 OPTIONAL ATTRIBUTES
217  
218 These (except region) correspond to the C<o:> options in the C<messages.mime>
219 section of L<https://documentation.mailgun.com/en/latest/api-sending.html#sending>
220  
221 =head2 deliverytime
222  
223 Desired time of delivery. String or DateTime object.
224  
225 =head2 dkim
226  
227 Enables/disables DKIM signatures. C<'yes'> or C<'no'>.
228  
229 =head2 region
230  
231 Defines used Mailgun region. C<'us'> (default) or C<'eu'>.
232  
233 See L<https://documentation.mailgun.com/en/latest/api-intro.html#mailgun-regions-1>
234  
235 =head2 tag
236  
237 Tag string. Comma-separated string list or arrayref of strings.
238  
239 =head2 testmode
240  
241 Enables sending in test mode. C<'yes'> or C<'no'>.
242  
243 =head2 tracking
244  
245 Toggles tracking. C<'yes'> or C<'no'>.
246  
247 =head2 tracking_clicks
248  
249 Toggles clicks tracking. C<'yes'>, C<'no'> or C<'html_only'>.
250  
251 =head2 tracking_opens
252  
253 Toggles open tracking. C<'yes'> or C<'no'>.
254  
255 =head1 MIME HEADERS
256  
257 The C<o:> options above can also be specified using the C<X-Mailgun-> headers
258 listed here L<https://documentation.mailgun.com/en/latest/user_manual.html#sending-via-smtp>
259  
260 If a single-valued option is specified in both the options and the headers,
261 experimentation shows the header takes precedence. This doesn't seem to be
262 documented, so don't rely on this behaviour.
263  
264 Multi-valued options use both the options and the headers.
265  
266 =head1 ENVIRONMENT
267  
268 The great strength of Email::Sender is that you don't need to hardcode your
269 transport, nor any of the options relating to that transport. They can all be
270 specified via environment variables.
271  
272 To select the Mailgun transport, use C<EMAIL_SENDER_TRANSPORT=Mailgun>.
273  
274 To specify any of the attributes above, prepend the attribute name with
275 C<EMAIL_SENDER_TRANSPORT_>.
276  
277 =over
278  
279 =item EMAIL_SENDER_TRANSPORT_api_key
280  
281 =item EMAIL_SENDER_TRANSPORT_domain
282  
283 =item EMAIL_SENDER_TRANSPORT_deliverytime
284  
285 =item EMAIL_SENDER_TRANSPORT_dkim
286  
287 =item EMAIL_SENDER_TRANSPORT_region
288  
289 =item EMAIL_SENDER_TRANSPORT_tag
290  
291 =item EMAIL_SENDER_TRANSPORT_testmode
292  
293 =item EMAIL_SENDER_TRANSPORT_tracking
294  
295 =item EMAIL_SENDER_TRANSPORT_tracking_clicks
296  
297 =item EMAIL_SENDER_TRANSPORT_tracking_opens
298  
299 =back
300  
301 =head1 LICENSE
302  
303 Copyright (C) Stephen Thirlwall.
304  
305 This library is free software; you can redistribute it and/or modify
306 it under the same terms as Perl itself.
307  
308 =head1 AUTHOR
309  
310 Stephen Thirlwall E<lt>sdt@cpan.orgE<gt>
311  
312 =cut
↑ Summary Build »