File Coverage

blib/lib/Mojolicious/Plugin/Mailgun.pm
Criterion Covered Total %
statement 31 31 100.0
branch 5 10 50.0
condition 1 3 33.3
subroutine 8 8 100.0
pod 1 1 100.0
total 46 53 86.7


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