sub my_sendmail { my ($to, $subject, $msg_body, $from_addr, $from_name, $to_name) = @_; my $input; # Make sure variables are set up correctly (ref($to) eq "ARRAY") || die "Argument \$to must be a reference to an array"; $subject = "(No subject)" unless ($subject); # Add the first email address to the To: name $to_name = ($to_name ? "$to_name " : ''); $to_name .= "<$to->[0]>"; # Use default 'from' address if not defined unless ($from_addr) { $from_addr = $default_from_addr; $from_name = $default_from_name; } # Add 'from' address to name. Some ISP's require a valid email in 'From:' $from_name .= " <$from_addr>"; warn "
\n" if ($sendmail_debug);
warn "Calling SMTP server.\n" if ($sendmail_debug);
my $sock = IO::Socket::INET->new(
PeerAddr => $mail_server,
Type => SOCK_STREAM,
PeerPort => 'smtp(25)',
Proto => 'tcp');
unless ($sock) {
warn "Can't open a port on the mail server ($mail_server).";
return 0;
}
$input = <$sock>;
warn "$input\r\n" if ($sendmail_debug);
print $sock "HELO localhost\r\n";
$input = <$sock>;
warn "$input\r\n" if ($sendmail_debug);
print $sock "mail from:<$from_addr>\r\n";
$input = <$sock>;
warn "$input\r\n" if ($sendmail_debug);
# Send to each recipient here. They won't see the others' addresses
my $rcpt;
foreach $rcpt (@$to) {
print $sock "rcpt to:<$rcpt>\r\n";
$input = <$sock>;
warn "$input\r\n" if ($sendmail_debug);
}
print $sock "DATA\r\n";
print $sock "Subject: $subject\r\n";
print $sock "From: $from_name\r\n";
print $sock "Reply-To: $from_addr\r\n";
print $sock "To: $to_name\r\n";
print $sock "\r\n";
#test line to add HTML to the emails
print $sock "Content-type: text/plain\n\n";
print $sock "$msg_body\r\n";
print $sock ".\r\n";
$input = <$sock>;
warn "$input\r\n" if ($sendmail_debug);
print $sock "QUIT";
close ($sock);
return 1;
} # End of my_sendmail()
1; # End of My Sendmail.pm
__END__