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

I am trying to send email by perl script.

use Mail::Sendmail; %mail = ( To => 'email', From => 'email', Message => "This is a minimalistic message" ); if (sendmail %mail) { print "Mail sent OK.\n" } else { print "Error sending mail: $Mail::Sendmail::error \n" }

When I run this code I get error as : "Error sending mail: connect to localhost failed (No connection could be made because the target machine actively refused it.)"

What can be the problem?

Replies are listed 'Best First'.
Re: Cant send Email
by NetWallah (Canon) on May 27, 2011 at 06:09 UTC
    The default install uses 'localhost' as the mail server. Is it a mail server rnning SMTP? Can you telnet to port 25 on the local host ?

    If not, add this before calling sendmail:

    $mail{smtp} = 'my.mail.server';

         Syntactic sugar causes cancer of the semicolon.        --Alan Perlis

Re: Cant send Email
by Anonymous Monk on May 27, 2011 at 06:14 UTC
    What can be the problem?

    Exactly what the error message says it is, localhost is refusing to accept SMTP connection. Maybe there is no SMTP server on localhost, maybe it needs password, ....

Re: Cant send Email
by dbs (Sexton) on May 27, 2011 at 13:33 UTC
    I like this mail code, try it! Note, it has my own touch to it ...scalar @_ part I mean.
    use MIME:Lite; sub _mailme($$$;$) { my $msg; if (scalar @_ == 3) { my ($to, $subject); ($to, $subject, $runlog) = @_; $msg = MIME::Lite->new( From => 'MAILER-DAEMON root', To => "$to", Subject => "$subject", Type => 'multipart/related' ); $msg->attach( Type =>'TEXT/html', Data =>"See attachment!" ); $msg->attach( Type => 'TEXT', Disposition => 'attachment', Path => "$runlog", Filename => "$runlog" ); } elsif (scalar @_ == 4) { my ($to, $subjct); ($to, $subject, $runlog, $mksysblog) = @_; $msg = MIME::Lite->new( From => 'MAILER-DAEMON root', To => "$to", Subject => "$subject", Type => 'multipart/related' ); $msg->attach( Type =>'TEXT/html', Data =>"See attachment!" ); $msg->attach( Type => 'TEXT', Disposition => 'attachment', Path => "$runlog", Filename => "$runlog" ); $msg->attach( Type => 'TEXT', Disposition => 'attachment', Path => "$mksysblog", Filename => "$mksysblog" ); } $msg->send; }

      Besides the question of whether this solves the ability to sendmail on localhost, this code has serious duplication. I would read all four variables from @_, and then determine what to do based on whether the fourth variable has a value or not. Using prototypes for subroutines is deprecated, I would avoid it.

      use MIME:Lite; sub _mailme { my ($to, $subject, $runlog, $mksysblog) = @_; $msg = MIME::Lite->new( From => 'MAILER-DAEMON root', To => "$to", Subject => "$subject", Type => 'multipart/related' ); $msg->attach( Type =>'TEXT/html', Data =>"See attachment!" ); $msg->attach( Type => 'TEXT', Disposition => 'attachment', Path => "$runlog", Filename => "$runlog" ); if ( $mksysblog ) { $msg->attach( Type => 'TEXT', Disposition => 'attachment', Path => "$mksysblog", Filename => "$mksysblog" ); } $msg->send; }

      As Occam said: Entia non sunt multiplicanda praeter necessitatem.

      Question: How does this fix the issue posed by the OP?

      Answer: It doesn't. This will try to use the sendmail process on the system which will also fail given that there isn't a mail server configured on localhost!