16 |
our @EXPORT = qw( |
our @EXPORT = qw( |
17 |
); |
); |
18 |
|
|
19 |
our $VERSION = '0.7'; |
our $VERSION = '0.8'; |
20 |
|
|
21 |
use Class::DBI::Loader; |
use Class::DBI::Loader; |
22 |
use Email::Valid; |
use Email::Valid; |
27 |
use Email::Address; |
use Email::Address; |
28 |
use Mail::DeliveryStatus::BounceParser; |
use Mail::DeliveryStatus::BounceParser; |
29 |
use Class::DBI::AbstractSearch; |
use Class::DBI::AbstractSearch; |
30 |
|
use SQL::Abstract; |
31 |
use Mail::Alias; |
use Mail::Alias; |
32 |
use Cwd qw(abs_path); |
use Cwd qw(abs_path); |
33 |
|
|
476 |
|
|
477 |
=back |
=back |
478 |
|
|
479 |
|
Any other driver name will try to use C<Email::Send::that_driver> module. |
480 |
|
|
481 |
Default sleep wait between two messages is 3 seconds. |
Default sleep wait between two messages is 3 seconds. |
482 |
|
|
483 |
|
This method will return number of succesfully sent messages. |
484 |
|
|
485 |
=cut |
=cut |
486 |
|
|
487 |
sub send_queued_messages { |
sub send_queued_messages { |
494 |
my $sleep = $arg->{'sleep'}; |
my $sleep = $arg->{'sleep'}; |
495 |
$sleep ||= 3 unless defined($sleep); |
$sleep ||= 3 unless defined($sleep); |
496 |
|
|
497 |
|
# number of messages sent o.k. |
498 |
|
my $ok = 0; |
499 |
|
|
500 |
my $email_send_driver = 'Email::Send::IO'; |
my $email_send_driver = 'Email::Send::IO'; |
501 |
my @email_send_options; |
my @email_send_options; |
502 |
|
|
503 |
if (lc($driver) eq 'smtp') { |
if (lc($driver) eq 'smtp') { |
504 |
$email_send_driver = 'Email::Send::SMTP'; |
$email_send_driver = 'Email::Send::SMTP'; |
505 |
@email_send_options = ['127.0.0.1']; |
@email_send_options = ['127.0.0.1']; |
506 |
|
} elsif ($driver && $driver ne '') { |
507 |
|
$email_send_driver = 'Email::Send::' . $driver; |
508 |
} else { |
} else { |
509 |
warn "dumping all messages to STDERR\n"; |
warn "dumping all messages to STDERR\n"; |
510 |
} |
} |
574 |
} |
} |
575 |
|
|
576 |
croak "can't send e-mail: $sent_status\n\nOriginal e-mail follows:\n".$m_obj->as_string unless ($sent_status); |
croak "can't send e-mail: $sent_status\n\nOriginal e-mail follows:\n".$m_obj->as_string unless ($sent_status); |
577 |
my @bad = @{ $sent_status->prop('bad') }; |
my @bad; |
578 |
|
@bad = @{ $sent_status->prop('bad') } if (eval { $sent_status->can('prop') }); |
579 |
croak "failed sending to ",join(",",@bad) if (@bad); |
croak "failed sending to ",join(",",@bad) if (@bad); |
580 |
|
|
581 |
if ($sent_status) { |
if ($sent_status) { |
589 |
|
|
590 |
print " - $sent_status\n"; |
print " - $sent_status\n"; |
591 |
|
|
592 |
|
$ok++; |
593 |
} else { |
} else { |
594 |
warn "ERROR: $sent_status\n"; |
warn "ERROR: $sent_status\n"; |
595 |
} |
} |
605 |
$m->dbi_commit; |
$m->dbi_commit; |
606 |
} |
} |
607 |
|
|
608 |
|
return $ok; |
609 |
|
|
610 |
} |
} |
611 |
|
|
612 |
=head2 inbox_message |
=head2 inbox_message |
705 |
# print "message_id: ",($message_id || "not found")," -- $is_bounce\n"; |
# print "message_id: ",($message_id || "not found")," -- $is_bounce\n"; |
706 |
} |
} |
707 |
|
|
708 |
|
=head2 received_messages |
709 |
|
|
710 |
|
Returns all received messages for given list or user. |
711 |
|
|
712 |
|
my @received = $nos->received_messages( |
713 |
|
list => 'My list', |
714 |
|
email => "john.doe@example.com", |
715 |
|
from_date => '2005-01-01 10:15:00', |
716 |
|
to_date => '2005-01-01 12:00:00', |
717 |
|
message => 0, |
718 |
|
); |
719 |
|
|
720 |
|
If don't specify C<list> or C<email> it will return all received messages. |
721 |
|
Results will be sorted by received date, oldest first. |
722 |
|
|
723 |
|
Other optional parametars include: |
724 |
|
|
725 |
|
=over 10 |
726 |
|
|
727 |
|
=item from_date |
728 |
|
|
729 |
|
Date (in ISO format) for lower limit of dates received |
730 |
|
|
731 |
|
=item to_date |
732 |
|
|
733 |
|
Return just messages older than this date |
734 |
|
|
735 |
|
=item message |
736 |
|
|
737 |
|
Include whole received message in result. This will probably make result |
738 |
|
array very large. Use with care. |
739 |
|
|
740 |
|
=back |
741 |
|
|
742 |
|
Each element in returned array will have following structure: |
743 |
|
|
744 |
|
my $row = { |
745 |
|
id => 42, # unique ID of received message |
746 |
|
list => 'My list', # useful if filtering by email |
747 |
|
ext_id => 9999, # ext_id from message sender |
748 |
|
email => 'jdoe@example.com', # e-mail of message sender |
749 |
|
bounced => 0, # true if message is bounce |
750 |
|
date => '2005-08-24 18:57:24', # date of receival in ISO format |
751 |
|
} |
752 |
|
|
753 |
|
If you specified C<message> option, this hash will also have C<message> key |
754 |
|
which will contain whole received message. |
755 |
|
|
756 |
|
=cut |
757 |
|
|
758 |
|
sub received_messages { |
759 |
|
my $self = shift; |
760 |
|
|
761 |
|
my $arg = {@_} if (@_); |
762 |
|
|
763 |
|
# croak "need list name or email" unless ($arg->{'list'} || $arg->{'email'}); |
764 |
|
|
765 |
|
my $sql = qq{ |
766 |
|
select |
767 |
|
received.id as id, |
768 |
|
lists.name as list, |
769 |
|
users.ext_id as ext_id, |
770 |
|
users.email as email, |
771 |
|
}; |
772 |
|
$sql .= qq{ message,} if ($arg->{'message'}); |
773 |
|
$sql .= qq{ |
774 |
|
bounced,received.date as date |
775 |
|
from received |
776 |
|
join lists on lists.id = list_id |
777 |
|
join users on users.id = user_id |
778 |
|
}; |
779 |
|
|
780 |
|
my $order = qq{ order by date desc }; |
781 |
|
|
782 |
|
my $where; |
783 |
|
|
784 |
|
$where->{'lists.name'} = lc($arg->{'list'}) if ($arg->{'list'}); |
785 |
|
$where->{'users.email'} = lc($arg->{'email'}) if ($arg->{'email'}); |
786 |
|
$where->{'received.date'} = { '>=', $arg->{'date_from'} } if ($arg->{'date_from'}); |
787 |
|
$where->{'received.date'} = { '<=', $arg->{'date_to'} } if ($arg->{'date_to'}); |
788 |
|
|
789 |
|
# hum, yammy one-liner |
790 |
|
my($stmt, @bind) = SQL::Abstract->new->where($where); |
791 |
|
|
792 |
|
my $dbh = $self->{'loader'}->find_class('received')->db_Main; |
793 |
|
|
794 |
|
my $sth = $dbh->prepare($sql . $stmt . $order); |
795 |
|
$sth->execute(@bind); |
796 |
|
return $sth->fetchall_hash; |
797 |
|
} |
798 |
|
|
799 |
|
|
800 |
=head1 INTERNAL METHODS |
=head1 INTERNAL METHODS |
801 |
|
|
1028 |
aliases => '/etc/aliases', |
aliases => '/etc/aliases', |
1029 |
); |
); |
1030 |
|
|
1031 |
|
If you are writing SOAP server (like C<soap.cgi> example), you will need to |
1032 |
|
call this method once to make new instance of Nos::SOAP and specify C<dsn> |
1033 |
|
and options for it. |
1034 |
|
|
1035 |
=cut |
=cut |
1036 |
|
|
1037 |
sub new { |
sub new { |
1189 |
} |
} |
1190 |
} |
} |
1191 |
|
|
|
=head1 UNIMPLEMENTED FUNCTIONS |
|
|
|
|
|
This is a stub for documentation of unimplemented functions. |
|
|
|
|
1192 |
=head2 MessagesReceived |
=head2 MessagesReceived |
1193 |
|
|
1194 |
|
Return statistics about received messages. |
1195 |
|
|
1196 |
my @result = MessagesReceived( |
my @result = MessagesReceived( |
1197 |
list => 'My list', |
list => 'My list', |
1198 |
email => 'jdoe@example.com', |
email => 'jdoe@example.com', |
1199 |
|
from_date => '2005-01-01 10:15:00', |
1200 |
|
to_date => '2005-01-01 12:00:00', |
1201 |
|
message => 0, |
1202 |
); |
); |
1203 |
|
|
1204 |
You can specify just C<list> or C<email> or any combination of those. |
You must specify C<list> or C<email> or any combination of those two. Other |
1205 |
|
parametars are optional. |
1206 |
|
|
1207 |
It will return array of hashes with following structure: |
For format of returned array element see C<received_messages>. |
1208 |
|
|
1209 |
{ |
=cut |
1210 |
id => 42, # unique ID of received message |
|
1211 |
list => 'My list', # useful only of filtering by email |
sub MessagesReceived { |
1212 |
ext_id => 9999, # ext_id from message user |
my $self = shift; |
1213 |
email => 'jdoe@example.com', # e-mail of user |
|
1214 |
bounced => 0, # true value if message is bounce |
if ($_[0] !~ m/^HASH/) { |
1215 |
date => '2005-08-24 18:57:24', # date of recival in ISO format |
die "need at least list or email" unless (scalar @_ < 2); |
1216 |
} |
return $nos->received_messages( |
1217 |
|
list => $_[0], email => $_[1], |
1218 |
|
from_date => $_[2], to_date => $_[3], |
1219 |
|
message => $_[4] |
1220 |
|
); |
1221 |
|
} else { |
1222 |
|
my $arg = shift; |
1223 |
|
die "need list or email argument" unless ($arg->{'list'} || $arg->{'email'}); |
1224 |
|
return $nos->received_messages( $arg ); |
1225 |
|
} |
1226 |
|
} |
1227 |
|
|
1228 |
|
### |
1229 |
|
|
1230 |
|
=head1 UNIMPLEMENTED SOAP FUNCTIONS |
1231 |
|
|
1232 |
|
This is a stub for documentation of unimplemented functions. |
1233 |
|
|
1234 |
=head2 MessagesReceivedByDate |
=head2 MessagesReceivedByDate |
1235 |
|
|
1236 |
=head2 MessagesReceivedByDateWithContent |
=head2 MessagesReceivedByDateWithContent |
1237 |
|
|
1238 |
=head2 ReceivedMessasgeContent |
=head2 ReceivedMessageContent |
1239 |
|
|
1240 |
Return content of received message. |
Return content of received message. |
1241 |
|
|
1242 |
my $mail_body = ReceivedMessageContent( id => 42 ); |
my $mail_body = ReceivedMessageContent( id => 42 ); |
1243 |
|
|
|
=cut |
|
1244 |
|
|
1245 |
|
|
1246 |
|
|
|
|
|
|
### |
|
|
|
|
1247 |
=head1 NOTE ON ARRAYS IN SOAP |
=head1 NOTE ON ARRAYS IN SOAP |
1248 |
|
|
1249 |
Returning arrays from SOAP calls is somewhat fuzzy (at least to me). It |
Returning arrays from SOAP calls is somewhat fuzzy (at least to me). It |