28 |
use Mail::DeliveryStatus::BounceParser; |
use Mail::DeliveryStatus::BounceParser; |
29 |
use Data::Dumper; |
use Data::Dumper; |
30 |
|
|
31 |
|
my $email_send_driver = 'Email::Send::IO'; |
32 |
|
my @email_send_options; |
33 |
|
|
34 |
|
#$email_send_driver = 'Sendmail'; |
35 |
|
|
36 |
|
|
37 |
=head1 NAME |
=head1 NAME |
38 |
|
|
39 |
Nos - Notice Sender core module |
Nos - Notice Sender core module |
98 |
|
|
99 |
$nos->new_list( |
$nos->new_list( |
100 |
list => 'My list', |
list => 'My list', |
101 |
|
from => 'Outgoing from comment', |
102 |
email => 'my-list@example.com', |
email => 'my-list@example.com', |
103 |
); |
); |
104 |
|
|
332 |
|
|
333 |
Send queued messages or just ones for selected list |
Send queued messages or just ones for selected list |
334 |
|
|
335 |
$nos->send_queued_messages("My list"); |
$nos->send_queued_messages("My list",'smtp'); |
336 |
|
|
337 |
|
Second option is driver which will be used for e-mail delivery. If not |
338 |
|
specified, C<IO> driver will be used which will dump e-mail to C<STDERR>. |
339 |
|
|
340 |
|
Other valid drivers are: |
341 |
|
|
342 |
|
=over 10 |
343 |
|
|
344 |
|
=item smtp |
345 |
|
|
346 |
|
Send e-mail using SMTP server at 127.0.0.1 |
347 |
|
|
348 |
|
=back |
349 |
|
|
350 |
=cut |
=cut |
351 |
|
|
354 |
|
|
355 |
my $list_name = shift; |
my $list_name = shift; |
356 |
|
|
357 |
|
my $driver = shift || ''; |
358 |
|
|
359 |
|
if (lc($driver) eq 'smtp') { |
360 |
|
$email_send_driver = 'Email::Send::SMTP'; |
361 |
|
@email_send_options = ['127.0.0.1']; |
362 |
|
} |
363 |
|
warn "using $driver [$email_send_driver]\n"; |
364 |
|
|
365 |
my $lists = $self->{'loader'}->find_class('lists'); |
my $lists = $self->{'loader'}->find_class('lists'); |
366 |
my $queue = $self->{'loader'}->find_class('queue'); |
my $queue = $self->{'loader'}->find_class('queue'); |
367 |
my $user_list = $self->{'loader'}->find_class('user_list'); |
my $user_list = $self->{'loader'}->find_class('user_list'); |
399 |
|
|
400 |
my $hash = $auth->generate_hash( $to_email ); |
my $hash = $auth->generate_hash( $to_email ); |
401 |
|
|
402 |
my $from = $u->list_id->name . " <" . $from . "+" . $hash . ( $domain ? "@" . $domain : '' ). ">"; |
my $from_addr; |
403 |
my $to = $u->user_id->name . " <$to_email>"; |
my $email_hash = "+" . $hash . ( $domain ? '@' . $domain : ''); |
404 |
|
my $from_email_only = $from . $email_hash; |
405 |
|
my $from_bounce = $from . '-bounce' . $email_hash; |
406 |
|
|
407 |
|
$from_addr .= '"' . $u->list_id->from_addr . '" ' if ($u->list_id->from_addr); |
408 |
|
$from_addr .= '<' . $from_email_only . '>'; |
409 |
|
my $to = '"' . $u->user_id->name . '" <' . $to_email . '>'; |
410 |
|
|
411 |
my $m_obj = Email::Simple->new($msg) || croak "can't parse message"; |
my $m_obj = Email::Simple->new($msg) || croak "can't parse message"; |
412 |
|
|
413 |
$m_obj->header_set('From', $from) || croak "can't set From: header"; |
$m_obj->header_set('Return-Path', $from_bounce) || croak "can't set Return-Path: header"; |
414 |
|
$m_obj->header_set('Sender', $from_bounce) || croak "can't set Sender: header"; |
415 |
|
$m_obj->header_set('Errors-To', $from_bounce) || croak "can't set Errors-To: header"; |
416 |
|
$m_obj->header_set('From', $from_addr) || croak "can't set From: header"; |
417 |
$m_obj->header_set('To', $to) || croak "can't set To: header"; |
$m_obj->header_set('To', $to) || croak "can't set To: header"; |
418 |
|
|
419 |
$m_obj->header_set('X-Nos-Version', $VERSION); |
$m_obj->header_set('X-Nos-Version', $VERSION); |
420 |
$m_obj->header_set('X-Nos-Hash', $hash); |
$m_obj->header_set('X-Nos-Hash', $hash); |
421 |
|
|
422 |
# FIXME do real sending :-) |
# really send e-mail |
423 |
send IO => $m_obj->as_string; |
if (@email_send_options) { |
424 |
|
send $email_send_driver => $m_obj->as_string, @email_send_options; |
425 |
|
} else { |
426 |
|
send $email_send_driver => $m_obj->as_string; |
427 |
|
} |
428 |
|
|
429 |
$sent->create({ |
$sent->create({ |
430 |
message_id => $m->message_id, |
message_id => $m->message_id, |
466 |
|
|
467 |
my $to = $m->header('To') || die "can't find To: address in incomming message\n"; |
my $to = $m->header('To') || die "can't find To: address in incomming message\n"; |
468 |
|
|
469 |
|
my $return_path = $m->header('Return-Path') || ''; |
470 |
|
|
471 |
my @addrs = Email::Address->parse( $to ); |
my @addrs = Email::Address->parse( $to ); |
472 |
|
|
473 |
die "can't parse To: $to address\n" unless (@addrs); |
die "can't parse To: $to address\n" unless (@addrs); |
483 |
} |
} |
484 |
} |
} |
485 |
|
|
486 |
croak "can't find hash in e-mail $to\n" unless ($hash); |
warn "can't find hash in e-mail $to\n" unless ($hash); |
487 |
|
|
488 |
my $sent = $self->{'loader'}->find_class('sent'); |
my $sent = $self->{'loader'}->find_class('sent'); |
489 |
|
|
495 |
if ($sent_msg) { |
if ($sent_msg) { |
496 |
$message_id = $sent_msg->message_id || carp "no message_id"; |
$message_id = $sent_msg->message_id || carp "no message_id"; |
497 |
$user_id = $sent_msg->user_id || carp "no user_id"; |
$user_id = $sent_msg->user_id || carp "no user_id"; |
498 |
|
} else { |
499 |
|
warn "can't find sender with hash $hash\n"; |
500 |
} |
} |
501 |
|
|
502 |
|
|
503 |
my $is_bounce = 0; |
my $is_bounce = 0; |
504 |
|
|
505 |
my $bounce = eval { Mail::DeliveryStatus::BounceParser->new( |
if ($arg->{'bounce'} || $return_path eq '<>' || $return_path eq '') { |
506 |
$arg->{'message'}, { report_non_bounces=>1 }, |
no warnings; |
507 |
) }; |
my $bounce = eval { Mail::DeliveryStatus::BounceParser->new( |
508 |
carp "can't check if this message is bounce!" if ($@); |
$arg->{'message'}, { report_non_bounces=>1 }, |
509 |
|
) }; |
510 |
$is_bounce++ if ($bounce && $bounce->is_bounce); |
carp "can't check if this message is bounce!" if ($@); |
511 |
|
|
512 |
|
$is_bounce++ if ($bounce && $bounce->is_bounce); |
513 |
|
} |
514 |
|
|
515 |
my $received = $self->{'loader'}->find_class('received'); |
my $received = $self->{'loader'}->find_class('received'); |
516 |
|
|
541 |
|
|
542 |
my $list_obj = $nos->_add_list( |
my $list_obj = $nos->_add_list( |
543 |
list => 'My list', |
list => 'My list', |
544 |
|
from => 'Outgoing from comment', |
545 |
email => 'my-list@example.com', |
email => 'my-list@example.com', |
546 |
); |
); |
547 |
|
|
561 |
|
|
562 |
my $name = $arg->{'list'} || confess "can't add list without name"; |
my $name = $arg->{'list'} || confess "can't add list without name"; |
563 |
my $email = $arg->{'email'} || confess "can't add list without e-mail"; |
my $email = $arg->{'email'} || confess "can't add list without e-mail"; |
564 |
|
my $from_addr = $arg->{'from'}; |
565 |
|
|
566 |
my $lists = $self->{'loader'}->find_class('lists'); |
my $lists = $self->{'loader'}->find_class('lists'); |
567 |
|
|
569 |
name => $name, |
name => $name, |
570 |
email => $email, |
email => $email, |
571 |
}); |
}); |
572 |
|
|
573 |
croak "can't add list $name\n" unless ($l); |
croak "can't add list $name\n" unless ($l); |
574 |
|
|
575 |
|
if ($from_addr && $l->from_addr ne $from_addr) { |
576 |
|
$l->from_addr($from_addr); |
577 |
|
$l->update; |
578 |
|
} |
579 |
|
|
580 |
$l->dbi_commit; |
$l->dbi_commit; |
581 |
|
|
582 |
return $l; |
return $l; |