filmo has asked for the wisdom of the Perl Monks concerning the following question:

Want to use MIME::Lite (and indirectly Net::SMTP) to send emails to a remote email server. (i.e. not localhost) I know how to form the e-mail using MIME::Lite, but don't know how to establish an SMTP connection to a remote mail server that requires authentication. The POD on Net::SMTP isn't clear on this and I can't seem to get it to work.

What are the arguments (@args) I need to send:

$mime->send_by_smtp(@args);

so that it will log into the SMTP server, authenticate the account ID and then send the fully formed text/html/attachement e-mail created with MIME::Lite?
--
Filmo the Klown

Replies are listed 'Best First'.
Re: Send and Receive Mail
by rob_au (Abbot) on Dec 09, 2002 at 03:02 UTC
    You may find this node from the code catacombs which adds PLAIN, LOGIN and CRAM-MD5 authentication mechanisms to Net::SMTP of use in this endeavour.

     

    perl -le 'print+unpack("N",pack("B32","00000000000000000000000111110110"))'

Re: Send and Receive Mail
by pg (Canon) on Dec 09, 2002 at 03:26 UTC
    You have to use the auth method defined in Net::SMTP, by doing this, you don't need to worry how to deal with the SASL (Simple Authentication and Security Layer).
    $smtp->auth("myuid", "mypasswd");
    MIME::Lite has a method called send_by_smtp, which allows you to pass parameters to the Net::SMTP object that MIME::Lite will create, but those are only the parameters will be passed to Net::SMTP->new, not the auth method we memtioned above.

    MIME::Lite is mainly intended to allow you encode your mail msg, although it provided the send method as bonus, but it is never intended to be a full implementation of those RFC's. Even for msg encoding, it is not fully RFC-822 compliant.

    If you are serious about the full implementation, the right way is only to use MIME::Lite as the MIME encoder, and to use Net::SMTP to deal with the mail server.
      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
Re: Send and Receive Mail
by ph0enix (Friar) on Dec 09, 2002 at 09:25 UTC
      (1) Because we'd have to convert all our existing programs.
      (2) I don't know it and just needed to get this fixed fast... ;)
      --
      Filmo the Klown