Changeset 1832

Show
Ignore:
Timestamp:
11/19/06 07:04:53
Author:
miyagawa
Message:

Support SMTP Auth in Publish::Email. Fixes #183
More refactoring later. via http://www5d.biglobe.ne.jp/~y0ka/2006-04.html#2006-04-16-3

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/refactoring-email/plagger/lib/Plagger/Plugin/Publish/Email.pm

    r1799 r1832  
    142142            $param{Sendmail} = $route->{command} if defined $route->{command}; 
    143143            $outer->send('sendmail', %param); 
     144        } elsif ($route->{via} eq 'smtp-auth') { 
     145            my @args  = $route->{host} ? ($route->{host}) : (); 
     146            $outer->send_by_smtp_auth($route, @args); 
    144147        } else { 
    145148            my @args  = $route->{host} ? ($route->{host}) : (); 
     
    301304sub MIME::Lite::SMTP::TLS::print { shift->datasend(@_) } 
    302305 
     306*MIME::Lite::send_by_smtp_auth = sub { 
     307    my($self, @args) = @_; 
     308 
     309    ### We need the "From:" and "To:" headers to pass to the SMTP mailer: 
     310    my $hdr   = $self->fields(); 
     311    my($from) = MIME::Lite::extract_addrs( $self->get('From') ); 
     312    my $to    = $self->get('To'); 
     313 
     314    ### Sanity check: 
     315    defined($to) or Carp::croak "send_by_smtp_auth: missing 'To:' address\n"; 
     316 
     317    ### Get the destinations as a simple array of addresses: 
     318    my @to_all = MIME::Lite::extract_addrs($to); 
     319    if ($MIME::Lite::AUTO_CC) { 
     320        foreach my $field (qw(Cc Bcc)) { 
     321            my $value = $self->get($field); 
     322            push @to_all, MIME::Lite::extract_addrs($value) if defined($value); 
     323        } 
     324    } 
     325 
     326    ### Create SMTP client: 
     327    require Net::SMTP; 
     328    my $route = shift @args; 
     329    defined($route) or Carp::croak "send_by_smtp_auth: missing route\n"; 
     330    my $smtp = MIME::Lite::SMTP->new(@args) 
     331        or Carp::croak("Failed to connect to mail server: $!\n"); 
     332    $smtp->auth($route->{username}, $route->{password}) 
     333        or Carp::croak("SMTP AUTH command failed: $!\n".$smtp->message."\n"); 
     334    $smtp->mail($from) 
     335        or Carp::croak("SMTP MAIL command failed: $!\n".$smtp->message."\n"); 
     336    $smtp->to(@to_all) 
     337        or Carp::croak("SMTP TO command failed: $!\n".$smtp->message."\n"); 
     338    $smtp->data() 
     339        or Carp::croak("SMTP DATA command failed: $!\n".$smtp->message."\n"); 
     340    ### MIME::Lite can print() to anything with a print() method: 
     341    $self->print_for_smtp($smtp); 
     342    $smtp->dataend(); 
     343    $smtp->quit(); 
     344    1; 
     345}; 
     346 
     347 
    3033481; 
    304349__END__ 
  • branches/refactoring-email/plagger/t/plugins/Publish-Email/base.t

    r1799 r1832  
    1212plugins: 
    1313  - module: Publish::Email 
     14    config: 
     15      mailto: foo@localhost 
    1416--- expected 
    1517ok 1, $block->name;