@@ -30,7 +30,6 @@ sub new {
30
30
url => $Url . ' /' ,
31
31
domain => $Domain ,
32
32
from => $From ,
33
-
34
33
};
35
34
36
35
$self -> {get } = sub {
@@ -60,35 +59,41 @@ sub _handle_response {
60
59
61
60
my $rc = $response -> code;
62
61
63
- return 1 if $rc == 200;
62
+ return 1 if $rc == 200;
63
+
64
+ my $json = from_json($response -> decoded_content);
65
+ if ($json -> {message }) {
66
+ die $response -> status_line." " .$json -> {message };
67
+ }
64
68
65
69
die " Bad Request - Often missing a required parameter" if $rc == 400;
66
70
die " Unauthorized - No valid API key provided" if $rc == 401;
67
71
die " Request Failed - Parameters were valid but request failed" if $rc == 402;
68
72
die " Not Found - The requested item doesn’t exist" if $rc == 404;
69
73
die " Server Errors - something is wrong on Mailgun’s end" if $rc >= 500;
70
-
71
74
}
72
75
73
76
sub send {
74
77
my ($self , $msg ) = @_ ;
75
78
76
- $msg -> {from } = $msg -> {from } // $self -> {from };
77
- $msg -> {to } = $msg -> {to } // die " You must specify an email address to send to" ;
79
+ $msg -> {from } = $msg -> {from } || $self -> {from }
80
+ or die " You must specify an email address to send from" ;
81
+ $msg -> {to } = $msg -> {to }
82
+ or die " You must specify an email address to send to" ;
78
83
if (ref $msg -> {to } eq ' ARRAY' ) {
79
84
$msg -> {to } = join (' ,' ,@{$msg -> {to }});
80
85
}
81
86
82
87
$msg -> {subject } = $msg -> {subject } // " " ;
83
88
$msg -> {text } = $msg -> {text } // " " ;
84
89
85
- my $attachments = delete $msg -> { attachments };
86
- my $content = [ % $msg];
87
- if ( $attachments && ref $attachments eq ' ARRAY ' ) {
88
- for my $a ( @$ attachments ) {
89
- push @$content , attachment => $a ;
90
- }
91
- }
90
+ my $content = _prepare_content(
91
+ $msg ,
92
+ {
93
+ attachments => ' attachment ' ,
94
+ tags => ' o:tag ' ,
95
+ },
96
+ );
92
97
93
98
my $r = $self -> {post }-> ($self , ' messages' , $content );
94
99
@@ -97,6 +102,32 @@ sub send {
97
102
return from_json($r -> decoded_content);
98
103
}
99
104
105
+ sub _prepare_content {
106
+ my ($msg , $msg_key__content_key ) = @_ ;
107
+
108
+ my $content = [];
109
+
110
+ while ( my ( $msg_key , $content_key ) = each %$msg_key__content_key ) {
111
+ my $extra_content = _get_extra_content($msg , $msg_key , $content_key );
112
+ push @$content , @$extra_content ;
113
+ }
114
+
115
+ push @$content , %$msg ;
116
+
117
+ return $content ;
118
+ }
119
+
120
+ sub _get_extra_content {
121
+ my ($msg , $msg_key , $content_key ) = @_ ;
122
+
123
+ my $array = delete $msg -> {$msg_key } || [];
124
+
125
+ return [
126
+ map { $content_key => $_ }
127
+ @$array
128
+ ];
129
+ }
130
+
100
131
sub _get_route {
101
132
my ($self , $path ) = @_ ;
102
133
@@ -114,7 +145,7 @@ sub _get_route {
114
145
sub unsubscribes {
115
146
my ($self , $method , $data ) = @_ ;
116
147
$method = $method // ' get' ;
117
-
148
+
118
149
my $r = $self -> {lc ($method )}-> ($self ,' unsubscribes' ,$data );
119
150
_handle_response($r );
120
151
return from_json($r -> decoded_content);
0 commit comments