--- trunk/Nos.pm 2005/05/18 13:12:54 45 +++ trunk/Nos.pm 2005/05/24 17:04:01 50 @@ -26,7 +26,7 @@ use Email::Simple; use Email::Address; use Mail::DeliveryStatus::BounceParser; -use Data::Dumper; + =head1 NAME @@ -92,6 +92,7 @@ $nos->new_list( list => 'My list', + from => 'Outgoing from comment', email => 'my-list@example.com', ); @@ -252,8 +253,6 @@ my $this_user = $users->search( $key => $args->{$key} )->first || return; -print Dumper($this_user); - $this_user->delete || croak "can't delete user\n"; return $users->dbi_commit || croak "can't commit"; @@ -325,14 +324,47 @@ Send queued messages or just ones for selected list - $nos->send_queued_messages("My list"); + $nos->send_queued_messages( + list => 'My list', + driver => 'smtp', + sleep => 3, + ); + +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 + +Default sleep wait between two messages is 3 seconds. =cut sub send_queued_messages { my $self = shift; - my $list_name = shift; + my $arg = {@_}; + + my $list_name = $arg->{'list'} || ''; + my $driver = $arg->{'driver'} || ''; + my $sleep = $arg->{'sleep'}; + $sleep ||= 3 unless defined($sleep); + + my $email_send_driver = 'Email::Send::IO'; + my @email_send_options; + + 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'); @@ -371,19 +403,30 @@ 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 Sender: header"; + $m_obj->header_set('Errors-To', $from_email_only) || croak "can't set Errors-To: 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, @@ -391,6 +434,11 @@ hash => $hash, }); $sent->dbi_commit; + + if ($sleep) { + warn "sleeping $sleep seconds\n"; + sleep($sleep); + } } } $m->all_sent(1); @@ -425,6 +473,8 @@ my $to = $m->header('To') || die "can't find To: address in incomming message\n"; + my $return_path = $m->header('Return-Path') || ''; + my @addrs = Email::Address->parse( $to ); die "can't parse To: $to address\n" unless (@addrs); @@ -440,29 +490,40 @@ } } - croak "can't find hash in e-mail $to\n" unless ($hash); + #warn "can't find hash in e-mail $to\n" unless ($hash); my $sent = $self->{'loader'}->find_class('sent'); # will use null if no matching message_id is found - my $sent_msg = $sent->search( hash => $hash )->first; + my $sent_msg; + $sent_msg = $sent->search( hash => $hash )->first if ($hash); my ($message_id, $user_id) = (undef, undef); # init with NULL 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 $users = $self->{'loader'}->find_class('users'); + my $from = $m->header('From'); + $from = $1 if ($from =~ m/<(.*)>/); + my $this_user = $users->search( email => $from )->first; + $user_id = $this_user->id if ($this_user); } 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); + if ($return_path eq '<>' || $return_path eq '') { + no warnings; + my $bounce = eval { Mail::DeliveryStatus::BounceParser->new( + $arg->{'message'}, { report_non_bounces=>1 }, + ) }; + #warn "can't check if this message is bounce!" if ($@); + + $is_bounce++ if ($bounce && $bounce->is_bounce); + } my $received = $self->{'loader'}->find_class('received'); @@ -476,10 +537,7 @@ $this_received->dbi_commit; - print "message_id: ",($message_id || "not found")," -- $is_bounce\n"; - - - warn "inbox is not yet implemented"; +# print "message_id: ",($message_id || "not found")," -- $is_bounce\n"; } @@ -493,6 +551,7 @@ my $list_obj = $nos->_add_list( list => 'My list', + from => 'Outgoing from comment', email => 'my-list@example.com', ); @@ -512,6 +571,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 +579,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;