Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Re: Send and Receive Mail

by filmo (Scribe)
on Dec 15, 2002 at 00:49 UTC ( [id://219925]=note: print w/replies, xml ) Need Help??


in reply to Re: Send and Receive Mail
in thread Send and Receive Mail

Ended up adding this quick and dirty routine to MIME::Lite and it seems to work for authentication. I don't know much about writing modules, but I'm sure there probably a more gracefull and modular way to do the same.
sub send_by_smtp_with_auth { my ($self, $user, $passwd, @args) = @_; ### We need the "From:" and "To:" headers to pass to the SMTP mail +er: my $hdr = $self->fields(); my $from = $self->get('From'); my $to = $self->get('To'); (my $corrected_from = $from) =~ s/.*<\s*(.*)>.*/$1/; ### Sanity check: defined($to) or Carp::croak "send_by_smtp: missing 'To:' address\n +"; defined($user) or Carp::croak "send_by_smtp: missing 'Login Acct' +\n"; defined($passwd) or Carp::croak "send_by_smtp: missing 'passwd'\n +"; defined(@args) or Carp::croak "send_by_smtp: missing \@args\n"; ### Get the destinations as a simple array of addresses: my @to_all = extract_addrs($to); if ($AUTO_CC) { foreach my $field (qw(Cc Bcc)) { my $value = $self->get($field); push @to_all, extract_addrs($value) if defined($value); } } ### Create SMTP client: require Net::SMTP; my $smtp = MIME::Lite::SMTP->new(@args) or Carp::croak("Failed to connect to mail server: $!\n"); if ($user and $passwd) { $smtp->auth($user,$passwd) or Carp::croak("SMTP MAIL authorization failed: $!\n". $sm +tp->message . "\n"); } $smtp->mail($corrected_from) or Carp::croak("SMTP MAIL command failed: $!\n".$smtp->message +."\n"); $smtp->to(@to_all) or Carp::croak("SMTP RCPT command failed: $!\n".$smtp->message +."\n"); $smtp->data() or Carp::croak("SMTP DATA command failed: $!\n".$smtp->message +."\n"); ### MIME::Lite can print() to anything with a print() method: $self->print_for_smtp($smtp); $smtp->dataend(); $smtp->quit; 1; }
--
Filmo the Klown

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://219925]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2024-04-18 16:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found