# mail-test.pl - Copyright (C) 2003 Pat Thoyts # # Send some mail from Perl. # # This sends two messages, one valid and one without a recipient using the # SMTP protocol. # # usage: ./mail-test.pl smtpd-host ?smtpd-port? # # ------------------------------------------------------------------------- use diagnostics; use strict; use Net::SMTP; use Sys::Hostname; my ($smtp_smart_host, $smtp_smart_port) = (shift, shift); $smtp_smart_host = 'localhost' if (!$smtp_smart_host); $smtp_smart_port = 25 if (!$smtp_smart_port); my $smtp_default_from = 'postmaster@' . hostname(); my $smtp_timeout = 120; my $smtp_log_mail = 0; my $smtp_debug = 1; my $sender_address = 'perl-test-script@' . hostname() . ''; my $recipient_address = 'tcl-smtpd@' . $smtp_smart_host . ''; my $from_address = 'Perl Test Script '; my $ro_address = 'Tcl Server '; print "Sending valid message\n"; test_ok(); print "Sending invalid message\n"; test_no_rcpt(); sub test_no_rcpt { my $header = 'From: ' . $sender_address . "\n"; $header .= 'Subject: perl test' . "\n"; my $message = <new($smtp_smart_host, Hello => hostname(), Port => $smtp_smart_port, Timeout => $smtp_timeout, Debug => $smtp_debug) || die "SMTP failed to connect: $!"; $smtp->mail($from, (Size=>length($msg), Bits=>'8')); $smtp->to(@rcpts); if ($smtp->data()) { # start sending data; $smtp->datasend($msg); # send the message $smtp->dataend(); # finished sending data } else { $smtp->reset(); } $smtp->quit; # end of session if ( $smtp_log_mail ) { if ( open(MAILLOG, ">> data/maillog") ) { print MAILLOG "From $from at ", localtime() . "\n"; print MAILLOG "To: " . join(@rcpts, ',') . "\n"; print MAILLOG $msg . "\n\n"; close(MAILLOG); } } } # -------------------------------------------------------------------------