line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mail::IMAP2SMS; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
19843
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
28
|
|
4
|
1
|
|
|
1
|
|
4
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
60
|
|
5
|
1
|
|
|
1
|
|
904
|
use Net::IMAP::Simple; |
|
1
|
|
|
|
|
56273
|
|
|
1
|
|
|
|
|
39
|
|
6
|
1
|
|
|
1
|
|
910
|
use Net::IMAP::Simple::SSL; |
|
1
|
|
|
|
|
86818
|
|
|
1
|
|
|
|
|
30
|
|
7
|
1
|
|
|
1
|
|
1233
|
use Email::Simple; |
|
1
|
|
|
|
|
5439
|
|
|
1
|
|
|
|
|
31
|
|
8
|
1
|
|
|
1
|
|
943
|
use Mail::Sendmail; |
|
1
|
|
|
|
|
11355
|
|
|
1
|
|
|
|
|
121
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
9
|
use vars qw[$VERSION]; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
996
|
|
11
|
|
|
|
|
|
|
$VERSION = $1 if('$Id: IMAP2SMS.pm,v 1.2 2009/05/21 14:49:36 rcrowder Exp $' =~ /,v ([\d_.]+) /); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 NAME |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
Mail::IMAP2SMS - Perl extension for IMAP to SMS. |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=head1 SYNOPSIS |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# Import Module |
20
|
|
|
|
|
|
|
use Mail::IMAP2SMS; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# Instantiate the IMAP2SMS object. |
23
|
|
|
|
|
|
|
my $sms = Mail::IMAP2SMS->new('imap.example.com', 'test@example.com', 'p4$$w0rd', 1); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# Get unseen mail from inbox. |
26
|
|
|
|
|
|
|
my $unseen = $sms->get_unseen('INBOX'); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# Chunk each message into specified size (160) SMSs. |
29
|
|
|
|
|
|
|
foreach (@$unseen) { |
30
|
|
|
|
|
|
|
my ($id) = $_; |
31
|
|
|
|
|
|
|
$sms->chunk($id, 160); |
32
|
|
|
|
|
|
|
} # foreach |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# Send chunked messages. |
35
|
|
|
|
|
|
|
if ($sms->send('7895551234@vtext.com')) { |
36
|
|
|
|
|
|
|
print 'All sent successfully'; |
37
|
|
|
|
|
|
|
} else { |
38
|
|
|
|
|
|
|
print 'Send failed...'; |
39
|
|
|
|
|
|
|
} # if/else |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# Close connection |
42
|
|
|
|
|
|
|
$sms->disconnect; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=head1 DESCRIPTION |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
This module is a quick and easy way to SMS your IMAP email. |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=head1 OBJECT CREATION METHOD |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=over 4 |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=item new |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
my $sms = Mail::IMAP2SMS->new( $server [ :port ], $username, $password, $ssl ); |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
This class method constructs a C object. It takes four required parameters. The server parameter may specify just the |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
server, or both the server and the port. To specify an alternate port, seperate it from the server with a colon (C<:>), C. |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
The ssl (BOOLEAN) parameter may specify whether to use or not use SSL to connect to the specified IMAP server. |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=back |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=cut |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub new { |
67
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
68
|
|
|
|
|
|
|
|
69
|
0
|
|
|
|
|
|
my $self = { |
70
|
|
|
|
|
|
|
server => shift, |
71
|
|
|
|
|
|
|
user => shift, |
72
|
|
|
|
|
|
|
pass => shift, |
73
|
|
|
|
|
|
|
ssl => shift |
74
|
|
|
|
|
|
|
}; |
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
|
$self->{sms} = []; |
77
|
|
|
|
|
|
|
|
78
|
0
|
|
|
|
|
|
bless $self, $class; |
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
$self->_connect; |
81
|
|
|
|
|
|
|
|
82
|
0
|
|
|
|
|
|
return $self; |
83
|
|
|
|
|
|
|
} # new |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub _connect { |
86
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
87
|
|
|
|
|
|
|
|
88
|
0
|
0
|
|
|
|
|
if ($self->{ssl} == 1) { |
89
|
0
|
|
0
|
|
|
|
$self->{imap} = Net::IMAP::Simple::SSL->new($self->{server}) || |
90
|
|
|
|
|
|
|
croak "Unable to connect to IMAP: $Net::IMAP::Simple::errstr\n"; |
91
|
|
|
|
|
|
|
} else { |
92
|
0
|
|
0
|
|
|
|
$self->{imap} = Net::IMAP::Simple->new($self->{server}) || |
93
|
|
|
|
|
|
|
croak "Unable to connect to IMAP: $Net::IMAP::Simple::errstr\n"; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# Log on |
97
|
0
|
0
|
|
|
|
|
if(!$self->{imap}->login($self->{user}, $self->{pass})) { |
98
|
0
|
|
|
|
|
|
croak "Login failed: " . $self->{imap}->errstr . "\n"; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
0
|
|
|
|
|
|
return 1; |
102
|
|
|
|
|
|
|
} # connect |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=pod |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head1 METHODS |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=over 4 |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=item get_seen |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
my $seen = $sms->get_seen('INBOX'); |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
This method takes one required parameter, an IMAP folder. The number of seen |
115
|
|
|
|
|
|
|
messages is returned on success. On failure, nothing is returned. |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=cut |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub get_seen { |
120
|
0
|
|
|
0
|
1
|
|
my ($self, $folder) = @_; |
121
|
|
|
|
|
|
|
|
122
|
0
|
|
|
|
|
|
my $nm = $self->{imap}->select($folder); |
123
|
|
|
|
|
|
|
|
124
|
0
|
|
|
|
|
|
my @seen; |
125
|
|
|
|
|
|
|
|
126
|
0
|
|
|
|
|
|
for (my $i = 1; $i <= $nm; $i++){ |
127
|
0
|
0
|
|
|
|
|
if ($self->{imap}->seen($i)){ |
128
|
0
|
|
|
|
|
|
push @seen, $i; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
0
|
|
|
|
|
|
return \@seen; |
133
|
|
|
|
|
|
|
} # get_seen |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=pod |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=item get_unseen |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
my $unseen = $sms->get_unseen('INBOX'); |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
This method takes one required parameter, an IMAP folder. The number of seen |
142
|
|
|
|
|
|
|
messages is returned on success. On failure, nothing is returned. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=cut |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
sub get_unseen { |
147
|
0
|
|
|
0
|
1
|
|
my ($self, $folder) = @_; |
148
|
|
|
|
|
|
|
|
149
|
0
|
|
|
|
|
|
my $nm = $self->{imap}->select($folder); |
150
|
|
|
|
|
|
|
|
151
|
0
|
|
|
|
|
|
my @unseen; |
152
|
|
|
|
|
|
|
|
153
|
0
|
|
|
|
|
|
for (my $i = 1; $i <= $nm; $i++){ |
154
|
0
|
0
|
|
|
|
|
if (!$self->{imap}->seen($i)) { |
155
|
0
|
|
|
|
|
|
push @unseen, $i; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
0
|
|
|
|
|
|
return \@unseen; |
160
|
|
|
|
|
|
|
} # get_unseen |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=pod |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=item chunk |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
print 'Successfully chunked!' if $sms->chunk($id, 160); |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
This method takes two require parameters, a message ID and a chunking size. |
169
|
|
|
|
|
|
|
The message ID is used to get the message from the IMAP server. Upon getting |
170
|
|
|
|
|
|
|
the message it is determined whether the subject and body can fit in one SMS |
171
|
|
|
|
|
|
|
or if the message must be broken into segments of the proper SMS size. Carriers |
172
|
|
|
|
|
|
|
differ on SMS size thus a size must be specified. On success, boolean true is |
173
|
|
|
|
|
|
|
returned. On failure, nothing is returned. |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=cut |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
sub chunk { |
178
|
0
|
|
|
0
|
1
|
|
my ($self, $id, $size) = @_; |
179
|
|
|
|
|
|
|
|
180
|
0
|
|
|
|
|
|
my $es = Email::Simple->new(join '', @{ $self->{imap}->get($id) } ); |
|
0
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
|
182
|
0
|
|
|
|
|
|
my $sub = $es->header('Subject'); |
183
|
|
|
|
|
|
|
|
184
|
0
|
|
|
|
|
|
my $subl = length($sub); |
185
|
|
|
|
|
|
|
|
186
|
0
|
|
|
|
|
|
my $bodyl = length($es->body); |
187
|
|
|
|
|
|
|
|
188
|
0
|
0
|
|
|
|
|
if (($bodyl + $subl) < $size) { |
189
|
0
|
|
|
|
|
|
push @{ $self->{sms} }, $es; |
|
0
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
} else { |
191
|
0
|
|
|
|
|
|
my $msg_size = $subl; |
192
|
|
|
|
|
|
|
|
193
|
0
|
|
|
|
|
|
my @body = split(/ /, $es->body); |
194
|
|
|
|
|
|
|
|
195
|
0
|
|
|
|
|
|
my $str = ""; |
196
|
|
|
|
|
|
|
|
197
|
0
|
|
|
|
|
|
foreach (@body) { |
198
|
0
|
|
|
|
|
|
my $wordl = length($_); |
199
|
|
|
|
|
|
|
|
200
|
0
|
0
|
|
|
|
|
if (($wordl + $msg_size + 1) > $size) { |
201
|
0
|
|
|
|
|
|
my $msg = Email::Simple->new(join '', @{ $self->{imap}->get($id) } ); |
|
0
|
|
|
|
|
|
|
202
|
0
|
|
|
|
|
|
$msg->body_set("$str"); |
203
|
|
|
|
|
|
|
|
204
|
0
|
|
|
|
|
|
push @{ $self->{sms} }, $msg; |
|
0
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
|
206
|
0
|
|
|
|
|
|
$msg_size = $subl; |
207
|
0
|
|
|
|
|
|
$str = "$_ "; |
208
|
|
|
|
|
|
|
} else { |
209
|
0
|
|
|
|
|
|
$msg_size += ($wordl + 1); |
210
|
0
|
|
|
|
|
|
$str .= "$_ "; |
211
|
|
|
|
|
|
|
} # if/else |
212
|
|
|
|
|
|
|
} # foreach |
213
|
|
|
|
|
|
|
} # if/else |
214
|
|
|
|
|
|
|
|
215
|
0
|
|
|
|
|
|
return 1; |
216
|
|
|
|
|
|
|
} # chunk |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=pod |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=item send |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
print 'Send successful...' if $sms->send('7895551234@vtext.com'); |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
This method requires one parameter, a wireless carrier phone number email address. |
225
|
|
|
|
|
|
|
This method will send all SMS created to the specified email address. If more than |
226
|
|
|
|
|
|
|
three SMS are readied to send, a sending sleep time between each SMS will be applied |
227
|
|
|
|
|
|
|
to reduce the chance of the wireless carrier dropping any messages. On success, a |
228
|
|
|
|
|
|
|
boolean true is returned. On failure, nothing is returned. |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=cut |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
sub send { |
233
|
0
|
|
|
0
|
1
|
|
my ($self, $email) = @_; |
234
|
|
|
|
|
|
|
|
235
|
0
|
|
|
|
|
|
my $count = 1; |
236
|
|
|
|
|
|
|
|
237
|
0
|
|
|
|
|
|
foreach (@{ $self->{sms} }) { |
|
0
|
|
|
|
|
|
|
238
|
0
|
|
|
|
|
|
my %mail = ( To => $email, From => $_->header('From'), |
239
|
|
|
|
|
|
|
'Content-Type' => 'text/plain; charset=us-ascii', |
240
|
|
|
|
|
|
|
'Content-Transfer-Encoding' => '7bit', |
241
|
|
|
|
|
|
|
Subject => "$count " . $_->header('Subject'), Message => $_->body); |
242
|
|
|
|
|
|
|
|
243
|
0
|
0
|
|
|
|
|
if (scalar @{ $self->{sms} } > 3) { |
|
0
|
|
|
|
|
|
|
244
|
0
|
|
|
|
|
|
sleep(3); |
245
|
|
|
|
|
|
|
} |
246
|
|
|
|
|
|
|
|
247
|
0
|
0
|
|
|
|
|
sendmail(%mail) || croak "Unable to send email: $!"; |
248
|
|
|
|
|
|
|
|
249
|
0
|
|
|
|
|
|
$count++; |
250
|
|
|
|
|
|
|
} # foreach |
251
|
|
|
|
|
|
|
|
252
|
0
|
|
|
|
|
|
return 1; |
253
|
|
|
|
|
|
|
} # send |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
=pod |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
=item disconnect |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
print 'Disconnected from IMAP...' if $sms->quit; |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
This method requires no parameters. It simply closes the connection |
262
|
|
|
|
|
|
|
to the IMAP server. On success, a boolean true is returned. On failure, |
263
|
|
|
|
|
|
|
nothing is returned. |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
=cut |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
sub disconnect { |
268
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
269
|
|
|
|
|
|
|
|
270
|
0
|
|
|
|
|
|
$self->{imap}->quit; |
271
|
|
|
|
|
|
|
|
272
|
0
|
|
|
|
|
|
return 1; |
273
|
|
|
|
|
|
|
} # quit |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
=pod |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
=back |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
=cut |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
1; |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
__END__ |