File Coverage

blib/lib/Mojolicious/Plugin/Mailgun.pm
Criterion Covered Total %
statement 23 23 100.0
branch 4 8 50.0
condition 1 3 33.3
subroutine 6 6 100.0
pod 1 1 100.0
total 35 41 85.3


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