Newsgroup: it.comp.lang.perl
Titolo: Script per l'invio di email

Ho un server NT 4 con IIS 4 ed MDAEMON come server di posta elettronica.
Ho installato il Perl versione 5.00502.
Dovrei creare uno script che mi consenta di trasmettere posta elettronica
tramite web.
Ho provato con le funzioni della libreria Socket ma non riesco a connettermi
al server di posta: navigando in rete ho trovato questa funzione ma non
riesco a farla andare :
use Socket ;
$SMTP_SERVER = "nome del server di posta elettronica" ;
$SEND_MAIL = "" ;
# ---------------------------------------------------------------------
# sendmail
# ========
#
# Sendmail.pm routine below by Milivoj Ivkovic
# ---------------------------------------------------------------------
sub sendmail  {
# error codes below for those who bother to check result codes <gr>
# 1 success
# -1 $smtphost unknown
# -2 socket() failed
# -3 connect() failed
# -4 service not available
# -5 unspecified communication error
# -6 local user $to unknown on host $smtp
# -7 transmission of message failed
# -8 argument $to empty
#
#  Sample call:
#
# &sendmail($from, $reply, $to, $smtp, $subject, $message );
#
#  Note that there are several commands for cleaning up possible bad
inputs - if you
#  are hard coding things from a library file, so of those are unnecesssary
#
    my ($fromaddr, $replyaddr, $to, $smtp, $subject, $message) = @_;
    $to =~ s/[ \t]+/, /g; # pack spaces and add comma
    $fromaddr =~ s/.*<([^\s]*?)>/$1/; # get from email address
    $replyaddr =~ s/.*<([^\s]*?)>/$1/; # get reply email address
    $replyaddr =~ s/^([^\s]+).*/$1/; # use first address
    $message =~ s/^\./\.\./gm; # handle . as first character
    $message =~ s/\r\n/\n/g; # handle line ending
    $message =~ s/\n/\r\n/g;
    $smtp =~ s/^\s+//g; # remove spaces around $smtp
    $smtp =~ s/\s+$//g;
    if (!$to)
    {
 return(-8);
    }
 if ($SMTP_SERVER ne "")
  {
    my($proto) = (getprotobyname('tcp'))[2];
    my($port) = (getservbyname('smtp', 'tcp'))[2];
    my($smtpaddr) = ($smtp =~
       /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/)
 ? pack('C4',$1,$2,$3,$4)
     : (gethostbyname($smtp))[4];
    if (!defined($smtpaddr))
    {
 return(-1);
    }
    if (!socket(MAIL, AF_INET, SOCK_STREAM, $proto))
    {
 return(-2);
    }
    if (!connect(MAIL, pack('Sna4x8', AF_INET, $port, $smtpaddr)))
    {
 return(-3);
    }
    my($oldfh) = select(MAIL);
    $| = 1;
    select($oldfh);
    $_ = <MAIL>;
    if (/^[45]/)
    {
 close(MAIL);
 return(-4);
    }
    print MAIL "helo $SMTP_SERVER\r\n";
    $_ = <MAIL>;
    if (/^[45]/)
    {
 close(MAIL);
 return(-5);
    }
    print MAIL "mail from: <$fromaddr>\r\n";
    $_ = <MAIL>;
    if (/^[45]/)
    {
 close(MAIL);
 return(-5);
    }
    foreach (split(/, /, $to))
    {
 print MAIL "rcpt to: <$_>\r\n";
 $_ = <MAIL>;
 if (/^[45]/)
 {
     close(MAIL);
     return(-6);
 }
    }
    print MAIL "data\r\n";
    $_ = <MAIL>;
    if (/^[45]/)
    {
 close MAIL;
 return(-5);
    }
   }
  if ($SEND_MAIL ne "")
   {
     open (MAIL,"| $SEND_MAIL");
   }
    print MAIL "To: $to\n";
    print MAIL "From: $fromaddr\n";
    print MAIL "Reply-to: $replyaddr\n" if $replyaddr;
    print MAIL "X-Mailer: Perl Powered Socket Mailer\n";
    print MAIL "Subject: $subject\n\n";
    print MAIL "$message";
    print MAIL "\n.\n";
 if ($SMTP_SERVER ne "")
  {
    $_ = <MAIL>;
    if (/^[45]/)
    {
 close(MAIL);
 return(-7);
    }
    print MAIL "quit\r\n";
    $_ = <MAIL>;
  }
    close(MAIL);
    return(1);
}
Qalcuno mi puo` aiutare a farla funzionare ? O mi sa consigliare qualche
altro metodo ?
Grazie.

 a farla andare :
  Cerca sul CPAN il modulo Net::SMTP oppure i MailTools: con due righe
di codice risolvi il tuo problema.
	Alla prossima,
		Marco