On 02/27/2017 11:34 PM, ToddAndMargo wrote:
Hi All,
Follow up on Net::SMTP:
Thank you for all the help!
I am posting this back to help others. And, yes, I know I
don't have to use "~" so much.
-T
Comments:
1) "@to" is an array, not a string
2) the "To:", "From:", and "Subject:" are headers that
you make up yourself. And they are comments that
show up on the receiver. They are not the actual
"To:", "From:", and "Subject:"
3) you terminate the headers with a blank line and
add them to you message.
4) :diag turns on all output of the transaction. Errors
are output to STDERR ($ERR) regardless of the state
of :diag
Here is a copy of a sub I wrote to handle this:
<code>
sub eMailReport () {
# Reference: https://github.com/retupmoca/P6-Net-SMTP
my $smtp = "aaaaa";
my $port = bbbb;
my $username = "ccc";
my $password = "ddd";
Ooops: forgot this:
my $from = "$username";
my @to = qw [ xxx yyys ]; # This is an array, not a string
# Note: prepend headers followed by a blank line to the message
my $Headers;
for @to { $Headers ~= "To: " ~ "$_" ~ "\n"; }
$Headers ~= "From: " ~ "$IAm " ~ "<" ~ "$username" ~ ">\n";
$Headers ~= "Subject: $IAm ERROR(s) = $ErrorCount\n\n";
my $Message = "$Headers" ~ "$Report";
# Note: :debug will send all output to stderr. (Errors will always
be sent regardless.)
# if ( not my $client = Net::SMTP.new(:server( $smtp ), :port( $port
), :debug ) ) {
if ( not my $client = Net::SMTP.new(:server( $smtp ), :port( $port )
) ) {
$ErrorCount += 1;
PrintRed ( "SMTP: unable to open $smtp\n" );
AddToReport ( "SMTP: unable to open $smtp\n" );
return;
}
if ( not $client.auth( $username, $password ) ) {
$ErrorCount += 1;
PrintRed ( "SMTP: something is wrong with the username and/or
password\n\n" );
AddToReport ( "SMTP: something is wrong with the username and/or
password\n\n" );
return;
}
if ( not $client.send( $from, @to, $Message ) ) { PrintRed "SMTP
Error: Failed to send\n\n"; }
$client.quit;
}
</code>
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Computers are like air conditioners.
They malfunction when you open windows
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~