1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
# mail-test.pl - Copyright (C) 2003 Pat Thoyts <patthoyts@users.sf.net>
#
# 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 <perl-test-script@' . hostname() . '>';
my $ro_address = 'Tcl Server <tcl-smtpd@' . $smtp_smart_host . '>';
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 = <<EOF;
This is a sample message in no particular format, sent by Perl's
Net::SMTP package.
Let's check the transparency code with a sentance ending on the next line
. Like this!
EOF
Sendmail($header . "\n" . $message . "\n");
}
sub test_ok {
my $header = 'From: ' . $sender_address . "\n";
$header .= 'To: ' . $recipient_address . "\n";
$header .= 'Subject: perl test' . "\n";
my $message = <<EOF;
This is a sample message in no particular format, sent by Perl's
Net::SMTP package.
Let's check the transparency code with a sentance ending on the next line
. Like this!
EOF
Sendmail($header . "\n" . $message . "\n");
}
# -------------------------------------------------------------------------
# Sendmail replacement (replaces exec'ing /usr/lib/sendmail...)
#
# Just call this function with the entire mail (headers and body together).
# The recipient and sender addresses are extracted from the mail text.
# -------------------------------------------------------------------------
sub Sendmail {
my ($msg) = (@_);
my @rcpts = ();
my $from = $smtp_default_from;
# Process the message headers to identify the recipient list.
my @msg = split(/^$/m, $msg);
my $header = $msg[0];
$header =~ s/\n\s+/ /g; # fix continuation lines
my @lines = split(/^/m, $header);
chomp(@lines);
foreach my $line (@lines) {
my ($key, $value) = split(/:\s*/, $line, 2);
if ($key =~ /To|CC|BCC/i ) {
push(@rcpts, $value);
}
if ($key =~ /From/i) {
$from = $value;
}
}
my $smtp = Net::SMTP->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);
}
}
}
# -------------------------------------------------------------------------
|