line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::Postage::App; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
31358
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
36
|
|
4
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
28
|
|
5
|
1
|
|
|
1
|
|
1190
|
use JSON; |
|
1
|
|
|
|
|
19137
|
|
|
1
|
|
|
|
|
5
|
|
6
|
1
|
|
|
1
|
|
1159
|
use HTTP::Request::Common qw(POST); |
|
1
|
|
|
|
|
28828
|
|
|
1
|
|
|
|
|
91
|
|
7
|
1
|
|
|
1
|
|
1148
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
24528
|
|
|
1
|
|
|
|
|
29
|
|
8
|
1
|
|
|
1
|
|
820
|
use MIME::Base64; |
|
1
|
|
|
|
|
681
|
|
|
1
|
|
|
|
|
71
|
|
9
|
1
|
|
|
1
|
|
6
|
use File::Spec; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
23
|
|
10
|
1
|
|
|
1
|
|
1663
|
use Digest::SHA qw(sha1_hex); |
|
1
|
|
|
|
|
4786
|
|
|
1
|
|
|
|
|
1049
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub new { |
15
|
0
|
|
|
0
|
1
|
|
my ($class,%args) = @_; |
16
|
0
|
|
|
|
|
|
my $args = \%args; |
17
|
0
|
|
|
|
|
|
my $self = {}; |
18
|
0
|
|
|
|
|
|
$self->{data}->{api_key} = $args->{api_key}; |
19
|
0
|
|
|
|
|
|
$self->{json} = JSON->new; |
20
|
0
|
|
0
|
|
|
|
$self->{url} = $args->{url} || "http://api.postageapp.com/v.1.0/send_message.json"; |
21
|
0
|
|
|
|
|
|
$self->{ua} = LWP::UserAgent->new(agent => 'Net-Postage-App ' . $VERSION); |
22
|
0
|
0
|
|
|
|
|
die 'api_key argument is required' if !$self->{data}->{api_key}; |
23
|
0
|
|
|
|
|
|
return bless $self, $class; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub message { |
27
|
0
|
|
|
0
|
1
|
|
my ($self,%msg) = @_; |
28
|
0
|
|
|
|
|
|
my $message = \%msg; |
29
|
0
|
|
0
|
|
|
|
my $subject = $message->{subject} || die 'subject argument is required'; |
30
|
0
|
|
0
|
|
|
|
my $from = $message->{from} || die 'from argument is required'; |
31
|
0
|
|
|
|
|
|
my $reply_to = $message->{reply_to}; |
32
|
0
|
|
|
|
|
|
my $text = $message->{textmessage}; |
33
|
0
|
|
|
|
|
|
my $html = $message->{htmlmessage}; |
34
|
0
|
|
|
|
|
|
my $template = $message->{template}; |
35
|
0
|
|
|
|
|
|
my $variables = $message->{variables}; |
36
|
0
|
|
|
|
|
|
my $extra_headers = $message->{headers}; |
37
|
0
|
0
|
0
|
|
|
|
if (!$text && !$html && !$template){ |
|
|
|
0
|
|
|
|
|
38
|
0
|
|
|
|
|
|
die 'You have to include (textmessage and/or htmlmessage) or a predefined template'; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
##generate uuid |
42
|
0
|
0
|
|
|
|
|
if ($message->{resend}) { |
43
|
0
|
0
|
|
|
|
|
$self->{data}->{uid} = sha1_hex($subject . ($text ? $text : '') . ($html ? $html : '') . time()); |
|
|
0
|
|
|
|
|
|
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
#do we have attachments |
47
|
0
|
0
|
|
|
|
|
if (my $attachments = $message->{attachments}) { |
48
|
0
|
|
|
|
|
|
my @attachments; |
49
|
0
|
|
|
|
|
|
foreach my $file (@{$attachments}){ |
|
0
|
|
|
|
|
|
|
50
|
0
|
|
|
|
|
|
my $hash = {}; |
51
|
0
|
|
|
|
|
|
my ($vol,$dir,$filename) = File::Spec->splitpath( $file ); |
52
|
|
|
|
|
|
|
##get file content |
53
|
0
|
0
|
|
|
|
|
open(my $fh, '<', $file) or die "can't open file $file $!"; |
54
|
0
|
|
|
|
|
|
my $content = do {local $/; <$fh>}; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
55
|
0
|
|
|
|
|
|
close $fh; |
56
|
|
|
|
|
|
|
|
57
|
0
|
|
|
|
|
|
$self->{data}->{arguments}->{attachments}->{$filename} = { |
58
|
|
|
|
|
|
|
'content_type' => 'application/octet-stream', |
59
|
|
|
|
|
|
|
'content' => encode_base64($content) |
60
|
|
|
|
|
|
|
}; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
###combine arguments |
65
|
0
|
0
|
|
|
|
|
$self->{data}->{arguments}->{content}->{'text/plain'} = $text if $text; |
66
|
0
|
0
|
|
|
|
|
$self->{data}->{arguments}->{content}->{'text/html'} = $html if $html; |
67
|
0
|
0
|
|
|
|
|
$self->{data}->{arguments}->{'template'} = $template if $template; |
68
|
0
|
0
|
|
|
|
|
$self->{data}->{arguments}->{'variables'} = $variables if $variables; |
69
|
0
|
|
|
|
|
|
$self->{data}->{arguments}->{headers} = { |
70
|
|
|
|
|
|
|
"subject" => $subject, |
71
|
|
|
|
|
|
|
"from" => $from, |
72
|
|
|
|
|
|
|
"reply-to" => $reply_to |
73
|
|
|
|
|
|
|
}; |
74
|
|
|
|
|
|
|
|
75
|
0
|
|
|
|
|
|
while( my ($key, $value) = each %$extra_headers ) { |
76
|
0
|
|
|
|
|
|
$self->{data}->{arguments}->{headers}->{$key} = $value; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
0
|
|
|
|
|
|
return $self->{data}; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub to { |
83
|
0
|
|
|
0
|
1
|
|
my ($self,$rec) = @_; |
84
|
0
|
|
|
|
|
|
my %all_rec = (); |
85
|
0
|
|
|
|
|
|
my %hash = (); |
86
|
0
|
0
|
|
|
|
|
if (ref $rec ){ |
87
|
0
|
0
|
|
|
|
|
if (ref $rec eq 'HASH'){ |
|
|
0
|
|
|
|
|
|
88
|
0
|
|
|
|
|
|
%hash = %{$rec}; |
|
0
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
} elsif (ref $rec eq 'ARRAY'){ |
90
|
0
|
|
|
|
|
|
%hash = map { $_ => 1 } @{$rec}; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
0
|
0
|
|
|
|
|
if ($self->{data}->{arguments}->{recipients}){ |
94
|
0
|
|
|
|
|
|
%all_rec = (%hash, %{$self->{data}->{arguments}->{recipients}}); |
|
0
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
} else { |
96
|
0
|
|
|
|
|
|
%all_rec = %hash; |
97
|
|
|
|
|
|
|
} |
98
|
0
|
|
|
|
|
|
$self->{data}->{arguments}->{recipients} = \%all_rec; |
99
|
|
|
|
|
|
|
} else{ |
100
|
0
|
|
|
|
|
|
$self->{data}->{arguments}->{recipients}->{$rec} = 1; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
0
|
|
|
|
|
|
return $self->{data}; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub send { |
107
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
108
|
0
|
|
|
|
|
|
my $json = $self->{json}; |
109
|
0
|
|
|
|
|
|
my $data = $json->encode($self->{data}); |
110
|
0
|
|
|
|
|
|
my $ua = $self->{ua}; |
111
|
0
|
|
|
|
|
|
my $url = $self->{url}; |
112
|
0
|
|
|
|
|
|
my $req = $ua->post( |
113
|
|
|
|
|
|
|
$url, |
114
|
|
|
|
|
|
|
'Content-Type' => 'application/json; charset=utf8', |
115
|
|
|
|
|
|
|
'Content' => $data |
116
|
|
|
|
|
|
|
); |
117
|
|
|
|
|
|
|
|
118
|
0
|
|
|
|
|
|
my $response = $json->decode($req->content); |
119
|
0
|
|
|
|
|
|
$self->{data} = {}; |
120
|
0
|
|
|
|
|
|
$self->{response} = $response; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub response { |
124
|
0
|
|
|
0
|
0
|
|
shift->{response}; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
sub is_success { |
128
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
129
|
0
|
0
|
|
|
|
|
if ($self->response->{response}->{status} eq "ok"){ |
130
|
0
|
|
|
|
|
|
return 1; |
131
|
|
|
|
|
|
|
} else { |
132
|
0
|
|
|
|
|
|
return 0; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub errorMessage { |
137
|
0
|
|
|
0
|
1
|
|
shift->response->{response}->{message}; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
1; |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
__END__ |