line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::Mailgun; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
689
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
7
|
|
4
|
1
|
|
|
1
|
|
206
|
use Carp 'croak'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
665
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.09'; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
has base_url => sub { Mojo::URL->new('https://api.mailgun.net/v3/'); }; |
9
|
|
|
|
|
|
|
has ua => sub { Mojo::UserAgent->new(); }; |
10
|
|
|
|
|
|
|
has config => sub { +{} }; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub register { |
13
|
1
|
|
|
1
|
1
|
38
|
my ($self, $app, $conf) = @_; |
14
|
1
|
50
|
|
|
|
7
|
$self->config(keys %$conf ? $conf : $app->config->{mailgun}); |
15
|
1
|
|
|
|
|
15
|
$app->ua->server->app($app); |
16
|
1
|
50
|
33
|
|
|
119
|
$self->_test_mode($app) if $ENV{MAILGUN_TEST} // $app->mode eq 'development'; |
17
|
1
|
50
|
|
|
|
590
|
$self->base_url($conf->{base_url}) if $conf->{base_url}; |
18
|
|
|
|
|
|
|
$app->helper( |
19
|
|
|
|
|
|
|
'mailgun.send' => sub { |
20
|
1
|
|
|
1
|
|
15897
|
my ($c, $site, $mail, $cb) = @_; |
21
|
|
|
|
|
|
|
croak "No mailgun config for $site" |
22
|
1
|
50
|
|
|
|
6
|
unless my $config = $self->config->{$site}; |
23
|
1
|
|
|
|
|
12
|
my $url=$self->_make_url($config); |
24
|
1
|
|
|
|
|
5
|
$self->ua->post($url, form => $mail, $cb); |
25
|
1
|
|
|
|
|
1380
|
return $c; |
26
|
|
|
|
|
|
|
} |
27
|
1
|
|
|
|
|
13
|
); |
28
|
|
|
|
|
|
|
$app->helper( |
29
|
|
|
|
|
|
|
'mailgun.send_p' => sub { |
30
|
1
|
|
|
1
|
|
15333
|
my ($c, $site, $mail) = @_; |
31
|
|
|
|
|
|
|
croak "No mailgun config for $site" |
32
|
1
|
50
|
|
|
|
5
|
unless my $config = $self->config->{$site}; |
33
|
1
|
|
|
|
|
12
|
my $url=$self->_make_url($config); |
34
|
1
|
|
|
|
|
17
|
return $self->ua->post_p($url, form => $mail); |
35
|
|
|
|
|
|
|
} |
36
|
1
|
|
|
|
|
426
|
); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub _make_url { |
40
|
2
|
|
|
2
|
|
7
|
my ($self, $config)=@_; |
41
|
2
|
|
|
|
|
8
|
my $url = $self->base_url->clone; |
42
|
2
|
|
|
|
|
102
|
$url->path->merge($config->{domain} . '/messages'); |
43
|
2
|
|
|
|
|
334
|
$url->userinfo('api:' . $config->{api_key}); |
44
|
2
|
|
|
|
|
15
|
return $url; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub _test_mode { |
48
|
1
|
|
|
1
|
|
3
|
my ($self, $app) = @_; |
49
|
1
|
|
|
|
|
4
|
$self->base_url($app->ua->server->nb_url->clone->path('/dummy/mail/')); |
50
|
|
|
|
|
|
|
$app->routes->post( |
51
|
|
|
|
|
|
|
'/dummy/mail/*domain/messages' => sub { |
52
|
2
|
|
|
2
|
|
11123
|
my $c = shift; |
53
|
2
|
|
|
|
|
10
|
$c->render(json => |
54
|
|
|
|
|
|
|
{id => 1, params => $c->req->params->to_hash, url => $c->req->url->to_abs}); |
55
|
|
|
|
|
|
|
} |
56
|
1
|
|
|
|
|
3847
|
); |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
1; |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head1 NAME |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
Mojolicious::Plugin::Mailgun - Easy Email sending with mailgun |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head1 SYNOPSIS |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# Mojolicious::Lite |
69
|
|
|
|
|
|
|
plugin 'mailgun' => { mom => { |
70
|
|
|
|
|
|
|
api_key => '123', |
71
|
|
|
|
|
|
|
domain => 'mom.no', |
72
|
|
|
|
|
|
|
}}; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# Mojolicious |
75
|
|
|
|
|
|
|
$self->plugin(mailgun => { mom => { |
76
|
|
|
|
|
|
|
site => { |
77
|
|
|
|
|
|
|
api_key => '123', |
78
|
|
|
|
|
|
|
domain => 'mom.no', |
79
|
|
|
|
|
|
|
}}); |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# in controller named params |
82
|
|
|
|
|
|
|
$self->mailgun->send( mom => { |
83
|
|
|
|
|
|
|
recipient => 'pop@pop.com', |
84
|
|
|
|
|
|
|
subject => 'use Perl or die;' |
85
|
|
|
|
|
|
|
html => $html, |
86
|
|
|
|
|
|
|
inline => { file => 'public/file.png' }, |
87
|
|
|
|
|
|
|
sub { my $self,$res = shift }, # receives a Mojo::Transaction from mailgun. |
88
|
|
|
|
|
|
|
); |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head1 DESCRIPTION |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
Provides a quick and easy way to send email using the Mailgun API with support for |
94
|
|
|
|
|
|
|
multiple user agents. |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head1 OPTIONS |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
L can be provided a hash of mailgun sites with |
99
|
|
|
|
|
|
|
C and C, or it can read them directly from |
100
|
|
|
|
|
|
|
$c->config->{mailgun} if not provided at load time. |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head1 HELPERS |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
L implements one helper. |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head2 mailgun->send <$site>, <\%post_options>, <$cb> |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
Send a mail with the mailgun API. This is just a thin wrapper around |
110
|
|
|
|
|
|
|
Mojo::UserAgent to handle authentication settings. See the mailgun sending |
111
|
|
|
|
|
|
|
documentation for more information about the supported arguments to the |
112
|
|
|
|
|
|
|
post_options hash. This API can only be used non-blocking, so the callback is |
113
|
|
|
|
|
|
|
required. |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
L |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head1 METHODS |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
L inherits all methods from L |
120
|
|
|
|
|
|
|
and implements the following new ones. |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head2 C |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
$plugin->register; |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Register plugin hooks and helpers in L application. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head1 SEE ALSO |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
L |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head1 AUTHOR |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
Marcus Ramberg |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Copyright (C) 2017 by Marcus Ramberg. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
141
|
|
|
|
|
|
|
under the same terms as Perl itself. |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=cut |