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

I'm using the sample script for SMTP-Server from below:
# SERVER use warnings; use strict; use Net::SMTP::Server; use Net::SMTP::Server::Client; use Data::Dumper; our $host = $ARGV[0] || "0.0.0.0" ; our $server = new Net::SMTP::Server($host) || croak("Unable to open SMTP server socket"); print "Waiting for incoming SMTP connections on ".($host eq "0.0.0.0" +? "all IP addresses":$host)."\n"; $| = 1; while(my $conn = $server->accept()) { print "Incoming mail ... from " . $conn->peerhost() ; my $client = new Net::SMTP::Server::Client($conn) || croak("Unable to process incoming request"); if (!$client->process) { print "\nfailed to receive complete e-mail message\n"; next; } print " received\n"; print "From: $client->{FROM}\n"; my $to = $client->{TO}; my $toList = @{$to} ? join(",",@{$to}) : $to; print "To: $toList\n"; print "\n" ; print $client->{MSG}; }
Here is the code for my client ssending an email

# CLIENT use warnings; use strict; use Net::SMTP my $SMTP_HOST = 'localhost'; sub send_mail { my ($from, $to_addr, $msg); $from = shift; $to_addr = shift; $msg = shift; # # Open a SMTP session # $smtp = Net::SMTP->new( $SMTP_HOST, 'Debug' => 1, # Change to a 1 to + turn on debug messages ); if(!defined($smtp) || !($smtp)) { print "SMTP ERROR: Unable to open smtp session.\n"; return 0; } # # Pass the 'from' email address, exit if error # if (! ($smtp->mail( $from ) ) ) { return 0; } # # Pass the recipient address(es) # if (! ($smtp->recipient( ( ref($to_addr) ? @$to_addr : $to_add +r ) ) ) ) { return 0; } # # Send the message # $smtp->data( $msg ); $smtp->quit; } sub main { send_mail( 'from@domain.com', 'to@domain.com', 'This is a test email.\n' ); }

I've tried using Net::SMTP module to send an email but it complains everytime I set the SMTP server arg to localhost. Both scripts live on the same box. I basically need the ability to send an email from a PC that doesn't have an SMTP server...Any Ideas?

Replies are listed 'Best First'.
Re: Sending Email from SMTP-Server
by ig (Vicar) on May 05, 2009 at 03:58 UTC

    It would be easier to help you if you provided the error message that your client produces when you set the SMTP server to localhost. The error messages often give a good indication of what the problem is.

    You are missing a colon after "use Net::SMTP", in the client.

    You are missing a declaration of $smtp to satisfy strict, in the client (e.g. "use $smtp = ...").

    You are missing "use Carp;", in the server.

    With these three changes made, your code works for me.