| 1 | package WWW::Mailgun; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use MIME::Base64; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | require LWP::UserAgent; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | BEGIN { | ||||||||||||||||||||||||||||
| 12 | 1 | ▶ | our $VERSION = 0.52; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | } | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | my @IGNORE_DOMAIN = qw/domains/; | |||||||||||||||||||||||||||
| 16 | 1 | my @GET_METHODS = qw/stats domains log mailboxes/; | |||||||||||||||||||||||||||
| 17 | 1 | my @POST_METHODS = qw//; | |||||||||||||||||||||||||||
| 18 | 1 | my @ALL_METHODS = (@GET_METHODS, @POST_METHODS); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 1 | my $ALIAS__OPTION = { | |||||||||||||||||||||||||||
| 21 | attachments => 'attachment', | ||||||||||||||||||||||||||||
| 22 | tags => 'o:tag', | ||||||||||||||||||||||||||||
| 23 | }; | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 1 | my $OPTION__MAXIMUM = { | |||||||||||||||||||||||||||
| 26 | "o:tag" => 3, | ||||||||||||||||||||||||||||
| 27 | }; | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | sub new { | ||||||||||||||||||||||||||||
| 30 | 1 | ▶ | my ($class, $param) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=17.6
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 1 | ▶ | my $Key = $param->{key} // die "You must specify an API Key"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$param->{"key"} // die("You must specify an API Key")
MC/DC0%0/1
$param->{"key"} // die("You must specify an API Key")
$param->{"key"}
Truth table50%1/2
A
A= $param->{"key"}
| |||||||||||||||||||||||||||||
| 33 | 1 | ▶ | my $Domain = $param->{domain} // die "You need to specify a domain (IE: samples.mailgun.org)"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$param->{"domain"} // die("You need to specify a domain (IE: samples.mailgun.org)")
MC/DC0%0/1
$param->{"domain"} // die("You need to specify a domain (IE: samples.mailgun.org)")
$param->{"domain"}
Truth table50%1/2
A
A= $param->{"domain"}
| |||||||||||||||||||||||||||||
| 34 | 1 | ▶ | my $Url = $param->{url} // "https://api.mailgun.net/v2"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$param->{"url"} // "https://api.mailgun.net/v2"
MC/DC0%0/1
$param->{"url"} // "https://api.mailgun.net/v2"
$param->{"url"}
Truth table50%1/2
A
A= $param->{"url"}
| |||||||||||||||||||||||||||||
| 35 | 1 | ▶ | my $From = $param->{from} // ""; | ||||||||||||||||||||||||||
|
Condition50%1/2
$param->{"from"} // ""
MC/DC0%0/1
$param->{"from"} // ""
$param->{"from"}
Truth table50%1/2
A
A= $param->{"from"}
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 1 | my $self = { | |||||||||||||||||||||||||||
| 38 | ua => LWP::UserAgent->new, | ||||||||||||||||||||||||||||
| 39 | url => $Url . '/', | ||||||||||||||||||||||||||||
| 40 | domain => $Domain, | ||||||||||||||||||||||||||||
| 41 | from => $From, | ||||||||||||||||||||||||||||
| 42 | }; | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | $self->{get} = sub { | ||||||||||||||||||||||||||||
| 45 | 0 | ▶ | my ($self, $type, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 46 | 0 | return my $r = $self->{ua}->get(_get_route($self,[$type, $data])); | |||||||||||||||||||||||||||
| 47 | 1 | }; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | $self->{del} = sub { | ||||||||||||||||||||||||||||
| 50 | 0 | ▶ | my ($self, $type, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 51 | return my $r = $self->{ua}->request( | ||||||||||||||||||||||||||||
| 52 | 0 | HTTP::Request->new( 'DELETE', _get_route( $self, [$type, $data] ) ) | |||||||||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||||||||||
| 54 | 1 | }; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | $self->{post} = sub { | ||||||||||||||||||||||||||||
| 57 | 1 | ▶ | my ($self, $type, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 58 | 1 | return my $r = $self->{ua}->post(_get_route($self,$type), Content_Type => 'multipart/form-data', Content => $data); | |||||||||||||||||||||||||||
| 59 | 1 | }; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 1 | $self->{ua}->default_header('Authorization' => 'Basic ' . encode_base64('api:' . $Key)); | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 1 | return bless $self, $class; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | sub _handle_response { | ||||||||||||||||||||||||||||
| 67 | 1 | ▶ | my ($response) = shift; | ||||||||||||||||||||||||||
|
Subroutine
_handle_response: called CC=9 SCAR=31.8
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 1 | my $rc = $response->code; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 1 | ▶ | return 1 if 200 <= $rc && $rc <= 299; # success | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
200 <= $rc and $rc <= 299
MC/DC0%0/2
200 <= $rc and $rc <= 299
200 <= $rc
$rc <= 299
Truth table33%1/3
A and B
A= 200 <= $rcB= $rc <= 299
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 1 | my $json = from_json($response->decoded_content); | |||||||||||||||||||||||||||
| 74 | 1 | ▶ | if ($json->{message}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | die $response->status_line." ".$json->{message}; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 1 | ▶ | die "Bad Request - Often missing a required parameter" if $rc == 400; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 1 | ▶ | die "Unauthorized - No valid API key provided" if $rc == 401; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | die "Request Failed - Parameters were valid but request failed" if $rc == 402; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | die "Not Found - The requested item doesn’t exist" if $rc == 404; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | die "Server Errors - something is wrong on Mailgun’s end" if $rc >= 500; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | sub send { | ||||||||||||||||||||||||||||
| 86 | 0 | ▶ | my ($self, $msg) = @_; | ||||||||||||||||||||||||||
|
Subroutine
send: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | $msg->{from} = $msg->{from} || $self->{from} | ||||||||||||||||||||||||||||
| 89 | 0 | ▶ | or die "You must specify an email address to send from"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$msg->{'from'} || $self->{'from'}
MC/DC0%0/2
$msg->{'from'} || $self->{'from'}
$msg->{'from'}
$self->{'from'}
Truth table0%0/3
A || B
A= $msg->{'from'}B= $self->{'from'}
| |||||||||||||||||||||||||||||
| 90 | $msg->{to} = $msg->{to} | ||||||||||||||||||||||||||||
| 91 | 0 | ▶ | or die "You must specify an email address to send to"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | if (ref $msg->{to} eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | $msg->{to} = join(',',@{$msg->{to}}); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | $msg->{subject} = $msg->{subject} // ""; | ||||||||||||||||||||||||||
|
Condition0%0/2
$msg->{"subject"} // ""
MC/DC0%0/1
$msg->{"subject"} // ""
$msg->{"subject"}
Truth table0%0/2
A
A= $msg->{"subject"}
| |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | $msg->{text} = $msg->{text} // ""; | ||||||||||||||||||||||||||
|
Condition0%0/2
$msg->{"text"} // ""
MC/DC0%0/1
$msg->{"text"} // ""
$msg->{"text"}
Truth table0%0/2
A
A= $msg->{"text"}
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | my $content = _prepare_content($msg); | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | my $r = $self->{post}->($self, 'messages', $content); | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | _handle_response($r); | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | return from_json($r->decoded_content); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =head2 _prepare_content($option__values) : \@content | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | Given a $option__values hashref, transform it to an arrayref suitable for | ||||||||||||||||||||||||||||
| 111 | sending as multipart/form-data. The core logic here is that array references | ||||||||||||||||||||||||||||
| 112 | are modified from: | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | option => [ value1, value2, ... ] | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | to | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | [ option => value1, option => value2, ... ] | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =cut | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | sub _prepare_content { | ||||||||||||||||||||||||||||
| 123 | 0 | ▶ | my ($option__values) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_prepare_content: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | my $content = []; | |||||||||||||||||||||||||||
| 126 | 0 | my $option__count = {}; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | ▶ | while (my ($option, $values) = each %$option__values) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | $option = $ALIAS__OPTION->{$option} || $option; | ||||||||||||||||||||||||||
|
Condition0%0/3
$ALIAS__OPTION->{$option} || $option
MC/DC0%0/2
$ALIAS__OPTION->{$option} || $option
$ALIAS__OPTION->{$option}
$option
Truth table0%0/3
A || B
A= $ALIAS__OPTION->{$option}B= $option
| |||||||||||||||||||||||||||||
| 130 | 0 | ▶ | $values = ref $values ? $values : [$values]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | for my $value (@$values) { | |||||||||||||||||||||||||||
| 133 | 0 | $option__count->{$option}++; | |||||||||||||||||||||||||||
| 134 | 0 | ▶ | if ($OPTION__MAXIMUM->{$option} && | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$OPTION__MAXIMUM->{$option} and $option__count->{$option} > $OPTION__MAXIMUM->{$option}
MC/DC0%0/2
$OPTION__MAXIMUM->{$option} and $option__count->{$option} > $OPTION__MAXIMUM->{$option}
$OPTION__MAXIMUM->{$option}
$option__count->{$option} > $OPTION__MAXIMUM->{$option}
Truth table0%0/3
A and B
A= $OPTION__MAXIMUM->{$option}B= $option__count->{$option} > $OPTION__MAXIMUM->{$option}
| |||||||||||||||||||||||||||||
| 135 | $option__count->{$option} > $OPTION__MAXIMUM->{$option}) { | ||||||||||||||||||||||||||||
| 136 | 0 | warn "Reached max number of $option, skipping..."; | |||||||||||||||||||||||||||
| 137 | 0 | last; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | 0 | ▶ | $value = [ $value ] if $option eq 'attachment'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 0 | push @$content, $option => $value; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 0 | return $content; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | sub _get_route { | ||||||||||||||||||||||||||||
| 148 | 1 | ▶ | my ($self, $path) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_get_route: called CC=4 SCAR=19.4
| |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 1 | ▶ | if (ref $path eq 'ARRAY'){ | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 151 | 0 | my @clean = grep {defined} @$path; | |||||||||||||||||||||||||||
| 152 | unshift @clean, $self->{domain} | ||||||||||||||||||||||||||||
| 153 | 0 | ▶ | unless $clean[-1] ~~ @IGNORE_DOMAIN; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 0 | $path = join('/',@clean); | |||||||||||||||||||||||||||
| 155 | } elsif (!($path ~~ @IGNORE_DOMAIN)) { | ||||||||||||||||||||||||||||
| 156 | 1 | $path = $self->{domain} . '/' . $path | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | 1 | return $self->{url} . $path; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | sub unsubscribes { | ||||||||||||||||||||||||||||
| 162 | 1 | ▶ | my ($self, $method, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
unsubscribes: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||||||||||
| 163 | 1 | ▶ | $method = $method // 'get'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$method // "get"
MC/DC0%0/1
$method // "get"
$method
Truth table50%1/2
A
A= $method
| |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 1 | my $r = $self->{lc($method)}->($self,'unsubscribes',$data); | |||||||||||||||||||||||||||
| 166 | 1 | _handle_response($r); | |||||||||||||||||||||||||||
| 167 | 0 | return from_json($r->decoded_content); | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | sub complaints { | ||||||||||||||||||||||||||||
| 171 | 0 | ▶ | my ($self, $method, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
complaints: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 172 | 0 | ▶ | $method = $method // 'get'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$method // "get"
MC/DC0%0/1
$method // "get"
$method
Truth table0%0/2
A
A= $method
| |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 0 | my $r = $self->{lc($method)}->($self,'complaints',$data); | |||||||||||||||||||||||||||
| 175 | 0 | _handle_response($r); | |||||||||||||||||||||||||||
| 176 | 0 | return from_json($r->decoded_content); | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | sub bounces { | ||||||||||||||||||||||||||||
| 180 | 0 | ▶ | my ($self, $method, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
bounces: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 181 | 0 | ▶ | $method = $method // 'get'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$method // "get"
MC/DC0%0/1
$method // "get"
$method
Truth table0%0/2
A
A= $method
| |||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 0 | my $r = $self->{lc($method)}->($self,'bounces',$data); | |||||||||||||||||||||||||||
| 184 | 0 | _handle_response($r); | |||||||||||||||||||||||||||
| 185 | 0 | return from_json($r->decoded_content); | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | sub logs { | ||||||||||||||||||||||||||||
| 189 | ## Legacy support. | ||||||||||||||||||||||||||||
| 190 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
logs: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 191 | 0 | return $self->log(); | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | sub AUTOLOAD { ## Handle generic list of requests. | ||||||||||||||||||||||||||||
| 195 | 1 | ▶ | our $AUTOLOAD; | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=4 SCAR=21.7
| |||||||||||||||||||||||||||||
| 196 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 197 | 1 | my @ObjParts = split(/\:\:/, $AUTOLOAD); | |||||||||||||||||||||||||||
| 198 | 1 | my $method = pop(@ObjParts); | |||||||||||||||||||||||||||
| 199 | 1 | ▶ | return if $method eq 'DESTROY'; ## Ignore DESTROY. | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | 0 | ▶ | unless ($method ~~ @ALL_METHODS) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 0 | die("Not a valid method, \"$method\"."); | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | 0 | my $mode = 'get'; | |||||||||||||||||||||||||||
| 204 | 0 | ▶ | $mode = 'post' if $method ~~ @POST_METHODS; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 205 | 0 | my $r = $self->{$mode}->($self, $method, @_); | |||||||||||||||||||||||||||
| 206 | 0 | _handle_response($r); | |||||||||||||||||||||||||||
| 207 | 0 | return from_json($r->decoded_content); | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | =pod | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | =head1 NAME | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | WWW::Mailgun - Perl wrapper for Mailgun (L<http://mailgun.org>) | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | use WWW::Mailgun; | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | my $mg = WWW::Mailgun->new({ | ||||||||||||||||||||||||||||
| 222 | key => 'key-yOuRapiKeY', | ||||||||||||||||||||||||||||
| 223 | domain => 'YourDomain.mailgun.org', | ||||||||||||||||||||||||||||
| 224 | from => 'elb0w <elb0w@YourDomain.mailgun.org>' # Optionally set here, you can set it when you send | ||||||||||||||||||||||||||||
| 225 | }); | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | #sending examples below | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | # Get stats http://documentation.mailgun.net/api-stats.html | ||||||||||||||||||||||||||||
| 230 | my $obj = $mg->stats; | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | # Get logs http://documentation.mailgun.net/api-logs.html | ||||||||||||||||||||||||||||
| 233 | my $obj = $mg->logs; | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | Mailgun is a email service which provides email over a http restful API. | ||||||||||||||||||||||||||||
| 239 | These bindings goal is to create a perl interface which allows you to | ||||||||||||||||||||||||||||
| 240 | easily leverage it. | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | =head1 USAGE | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | =head2 new({key => 'mailgun key', domain => 'your mailgun domain', from => 'optional from') | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | Creates your mailgun object | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | from => the only optional field, it can be set in the message. | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | =head2 send($data) | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | Send takes in a hash of settings | ||||||||||||||||||||||||||||
| 255 | Takes all specificed here L<http://documentation.mailgun.net/api-sending.html> | ||||||||||||||||||||||||||||
| 256 | 'from' is optionally set here, otherwise you can set it in the constructor and it can be used for everything | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | =item Send a HTML message with optional array of attachments | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | $mg->send({ | ||||||||||||||||||||||||||||
| 261 | to => 'some_email@gmail.com', | ||||||||||||||||||||||||||||
| 262 | subject => 'hello', | ||||||||||||||||||||||||||||
| 263 | html => '<html><h3>hello</h3><strong>world</strong></html>', | ||||||||||||||||||||||||||||
| 264 | attachment => ['/Users/elb0w/GIT/Personal/Mailgun/test.pl'] | ||||||||||||||||||||||||||||
| 265 | }); | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | =item Send a text message | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | $mg->send({ | ||||||||||||||||||||||||||||
| 270 | to => 'some_email@gmail.com', | ||||||||||||||||||||||||||||
| 271 | subject => 'hello', | ||||||||||||||||||||||||||||
| 272 | text => 'Hello there' | ||||||||||||||||||||||||||||
| 273 | }); | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | =item Send a MIME multipart message | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | $mg->send({ | ||||||||||||||||||||||||||||
| 278 | to => 'some_email@gmail.com', | ||||||||||||||||||||||||||||
| 279 | subject => 'hello', | ||||||||||||||||||||||||||||
| 280 | text => 'Hello there', | ||||||||||||||||||||||||||||
| 281 | html => '<b>Hello there</b>' | ||||||||||||||||||||||||||||
| 282 | }); | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | =head2 unsubscribes, bounces, spam | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | Helper methods all take a method argument (del, post, get) | ||||||||||||||||||||||||||||
| 288 | L<http://documentation.mailgun.net/api_reference.html> | ||||||||||||||||||||||||||||
| 289 | 'post' optionally takes a hash of properties | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | =item Unsubscribes | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | # View all unsubscribes L<http://documentation.mailgun.net/api-unsubscribes.html> | ||||||||||||||||||||||||||||
| 295 | my $all = $mg->unsubscribes; | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | # Unsubscribe user from all | ||||||||||||||||||||||||||||
| 298 | $mg->unsubscribes('post',{address => 'user@website.com', tag => '*'}); | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | # Delete a user from unsubscriptions | ||||||||||||||||||||||||||||
| 301 | $mg->unsubscribes('del','user@website.com'); | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | # Get a user from unsubscriptions | ||||||||||||||||||||||||||||
| 304 | $mg->unsubscribes('get','user@website.com'); | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | =item Complaints | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | # View all spam complaints L<http://documentation.mailgun.net/api-complaints.html> | ||||||||||||||||||||||||||||
| 311 | my $all = $mg->complaints; | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | # Add a spam complaint for a address | ||||||||||||||||||||||||||||
| 314 | $mg->complaints('post',{address => 'user@website.com'}); | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | # Remove a complaint | ||||||||||||||||||||||||||||
| 317 | $mg->complaints('del','user@website.com'); | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | # Get a complaint for a adress | ||||||||||||||||||||||||||||
| 320 | $mg->complaints('get','user@website.com'); | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | =item Bounces | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | # View the list of bounces L<http://documentation.mailgun.net/api-bounces.html> | ||||||||||||||||||||||||||||
| 325 | my $all = $mg->bounces; | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | # Add a permanent bounce | ||||||||||||||||||||||||||||
| 328 | $mg->bounces('post',{ | ||||||||||||||||||||||||||||
| 329 | address => 'user@website.com', | ||||||||||||||||||||||||||||
| 330 | code => 550, #This is default | ||||||||||||||||||||||||||||
| 331 | error => 'Error Description' #Empty by default | ||||||||||||||||||||||||||||
| 332 | }); | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | # Remove a bounce | ||||||||||||||||||||||||||||
| 335 | $mg->bounces('del','user@website.com'); | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | # Get a bounce for a specific address | ||||||||||||||||||||||||||||
| 338 | $mg->bounces('get','user@website.com'); | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | =head1 TODO | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | =item Mailboxes | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | =item Campaigns | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | =item Mailing Lists | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | =item Routes | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | =head1 Author | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | George Tsafas <elb0w@elbowrage.com> | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | =head1 Support | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | elb0w on irc.freenode.net #perl | ||||||||||||||||||||||||||||
| 357 | L<https://github.com/gtsafas/mailgun.perl> | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | =head1 Resources | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | L<http://documentation.mailgun.net/> | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||