David H Hagan
David H Hagan

Reputation: 55

Error using Mail::Sendmail to send email from perl script using a windows machine

I have a simple test script for sending an email to myself using the cpan module Mail::Sendmail. I am operating on a Windows machine using Strawberry Perl and through the command line, everything seems fine. I am receiving an error that says connect to localhost failed (No connection could be made because the target machine refused it.)

My script is:

use Mail::Sendmail qw(sendmail %mailcfg);
$mailcfg{from} = '[email protected]';

print "Testing Mail::Sendmail version $Mail::Sendmail::VERSION\n";
print "Default server:  $Mail::Sendmail::mailcfg{smtp}->[0]\n";
print "Default sender:  $Mail::Sendmail::mailcfg{from}\n";

%mail = (   To      =>  '[email protected]',
            From    =>  '[email protected]',
            Message =>  'Test!'

        );

sendmail(%mail) or die $Mail::Sendmail::error;

print "OK.  Log says:\n", $Mail::Sendmail::log;

Is there any reason this would occur?

Upvotes: 2

Views: 3979

Answers (1)

justintime
justintime

Reputation: 3631

By default Mail::Sendmail is configured to send the mail to the localhost, but you are not running an SMTP server there.

You have to configre a suitable server - see the help.

 Default SMTP server(s)
       This is probably all you want to configure. It is usually done
       through $mailcfg{smtp}, which you can edit at the top of the
       Sendmail.pm file.  This is a reference to a list of SMTP servers.
       You can also set it from your script:

       "unshift @{$Mail::Sendmail::mailcfg{'smtp'}} , 'my.mail.server';"

       Alternatively, you can specify the server in the %mail hash you
       send from your script, which will do the same thing:

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

Upvotes: 1

Related Questions