--- trunk/Nos.pm 2005/05/18 13:12:54 45 +++ trunk/Nos.pm 2005/05/24 14:02:05 47 @@ -28,6 +28,12 @@ use Mail::DeliveryStatus::BounceParser; use Data::Dumper; +my $email_send_driver = 'Email::Send::IO'; +my @email_send_options; + +#$email_send_driver = 'Sendmail'; + + =head1 NAME Nos - Notice Sender core module @@ -92,6 +98,7 @@ $nos->new_list( list => 'My list', + from => 'Outgoing from comment', email => 'my-list@example.com', ); @@ -325,7 +332,20 @@ Send queued messages or just ones for selected list - $nos->send_queued_messages("My list"); + $nos->send_queued_messages("My list",'smtp'); + +Second option is driver which will be used for e-mail delivery. If not +specified, C driver will be used which will dump e-mail to C. + +Other valid drivers are: + +=over 10 + +=item smtp + +Send e-mail using SMTP server at 127.0.0.1 + +=back =cut @@ -334,6 +354,14 @@ my $list_name = shift; + my $driver = shift || ''; + + if (lc($driver) eq 'smtp') { + $email_send_driver = 'Email::Send::SMTP'; + @email_send_options = ['127.0.0.1']; + } + warn "using $driver [$email_send_driver]\n"; + my $lists = $self->{'loader'}->find_class('lists'); my $queue = $self->{'loader'}->find_class('queue'); my $user_list = $self->{'loader'}->find_class('user_list'); @@ -371,19 +399,29 @@ my $hash = $auth->generate_hash( $to_email ); - my $from = $u->list_id->name . " <" . $from . "+" . $hash . ( $domain ? "@" . $domain : '' ). ">"; - my $to = $u->user_id->name . " <$to_email>"; + my $from_addr; + my $from_email_only = $from . "+" . $hash . ( $domain ? '@' . $domain : ''); + $from_addr .= '"' . $u->list_id->from_addr . '" ' if ($u->list_id->from_addr); + $from_addr .= '<' . $from_email_only . '>'; + my $to = '"' . $u->user_id->name . '" <' . $to_email . '>'; my $m_obj = Email::Simple->new($msg) || croak "can't parse message"; - $m_obj->header_set('From', $from) || croak "can't set From: header"; + $m_obj->header_set('Return-Path', $from_email_only) || croak "can't set Return-Path: header"; + $m_obj->header_set('Sender', $from_email_only) || croak "can't set Return-Path: header"; + $m_obj->header_set('Errors-To', $from_email_only) || croak "can't set Return-Path: header"; + $m_obj->header_set('From', $from_addr) || croak "can't set From: header"; $m_obj->header_set('To', $to) || croak "can't set To: header"; $m_obj->header_set('X-Nos-Version', $VERSION); $m_obj->header_set('X-Nos-Hash', $hash); - # FIXME do real sending :-) - send IO => $m_obj->as_string; + # really send e-mail + if (@email_send_options) { + send $email_send_driver => $m_obj->as_string, @email_send_options; + } else { + send $email_send_driver => $m_obj->as_string; + } $sent->create({ message_id => $m->message_id, @@ -452,17 +490,22 @@ if ($sent_msg) { $message_id = $sent_msg->message_id || carp "no message_id"; $user_id = $sent_msg->user_id || carp "no user_id"; + } else { + warn "can't find sender with hash $hash\n"; } my $is_bounce = 0; - my $bounce = eval { Mail::DeliveryStatus::BounceParser->new( - $arg->{'message'}, { report_non_bounces=>1 }, - ) }; - carp "can't check if this message is bounce!" if ($@); - - $is_bounce++ if ($bounce && $bounce->is_bounce); + { + no warnings; + my $bounce = eval { Mail::DeliveryStatus::BounceParser->new( + $arg->{'message'}, { report_non_bounces=>1 }, + ) }; + carp "can't check if this message is bounce!" if ($@); + + $is_bounce++ if ($bounce && $bounce->is_bounce); + } my $received = $self->{'loader'}->find_class('received'); @@ -493,6 +536,7 @@ my $list_obj = $nos->_add_list( list => 'My list', + from => 'Outgoing from comment', email => 'my-list@example.com', ); @@ -512,6 +556,7 @@ my $name = $arg->{'list'} || confess "can't add list without name"; my $email = $arg->{'email'} || confess "can't add list without e-mail"; + my $from_addr = $arg->{'from'}; my $lists = $self->{'loader'}->find_class('lists'); @@ -519,9 +564,14 @@ name => $name, email => $email, }); - + croak "can't add list $name\n" unless ($l); + if ($from_addr && $l->from_addr ne $from_addr) { + $l->from_addr($from_addr); + $l->update; + } + $l->dbi_commit; return $l;