| | 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 | |
|---|