I've been trying to modify a simple SMTP mail send script that I found online, but am struggling, being a Perl newbie and all. The script is as follows:
sub send_mail # send SMTP mail
my ($from, $to_addr, $subject, $body, $msg);
$from = shift;
$to_addr = shift;
$subject = shift;
$body = shift;
$date = strftime "%a, %d %b %y %H:%M:%S %z",localtime;
$msg = "MIME-Version: 1.0\n"
. "From: $from\n"
. "To: " . ( ref($to_addr) ? join(';', @$to_addr) : $to_addr ) . "\n"
. "Date: $date\n"
. "Subject: $subject\n\n" # Double \n
# Open an SMTP session
$smtp = Net::SMTP->new( $SMTP_HOST,
'Debug' => 0, # Change to a 1 to turn on debug messages
if(!defined($smtp) || !($smtp))
print "SMTP ERROR: Unable to open smtp session.\n";
# Pass the 'from' email address, exit if error
if (! ($smtp->mail( $from ) ) )
# Pass the recipient address(es)
if (! ($smtp->recipient( ( ref($to_addr) ? @$to_addr : $to_addr ) ) ) )
# Send the message
$smtp->data( $msg );
I would like to pass the subroutine an extra parameter "bcc" which it would then use, if passed, to bcc the email. At the moment I can't work out how to do it.
Also I don't understand how comes the script can handle multiple "to" addresses, which it apparently can - how does it know which parameter is an email address and which is one of the parameters passed after the email addys? I get the impression its this line : ( ref($to_addr) ? join(';', @$to_addr) : $to_addr ) -- but no idea how it works!