Hello,
I need to write a short Perl program that will send email containing an HTML table with data. My script succeeds sending on the the email, but the HTML does not get interpreted by the client as HTML and appears as HTML tags with the data. My guess is it is something with the HTML that needs to tell the client to interpret the HTML and not display as text. Here is the code I use:
# *** MODULES *** use Socket; use Sys::Hostname; use Net::SMTP; use Win32::OLE qw(in with);
print ("$host");
# *** CONSTANTS *** my %detectedErrorState = ( "0" => [ "Unknown" ], "1" => [ "Other" ], "2" => [ "No error" ], "3" => [ "Low paper" ], "4" => [ "No paper" ], "5" => [ "Low toner" ], "6" => [ "No toner" ], "7" => [ "Door open" ], "8" => [ "Jammed" ], "9" => [ "Offline" ], "10" => [ "Service requested" ], "11" => [ "Output bin full" ]);
my %Status = ( "1" => [ "Other" ], "2" => [ "Unknown" ], "3" => [ "Idle" ], "4" => [ "Printing" ], "5" => [ "Warmup" ], "6" => [ "Stopped printing" ], "7" => [ "Offline" ]);
# *** VARIABLES *** my $count = 0; my $smtp_host = 'mail.------.com'; my $from = 'me.------.com'; my @to = ('fun.----.com','test.----.com'); my $subject = ''; #my $header = "Please review the following printing items:\n\n"; my $header = ""; my $message = ""; my $host = hostname();
# *** MAIN *** $header = "\n\n\n<html><head><h1>PRINTER TEST</h1></head><body><table>"; $message = $header . $message; $message = $message . "<tr><td>PRINTER</td><td>JOBS</td><td>MESSAGE</td><td>STATUS</td><td>MODEL</td></tr>"; &hw_inv; $message = $message . "</table></body></html>\n\n\n"; if ($count > 0) { &SendEmail; } else { print ("$printer_server: ($count) Printer Alerts. Done.\n"); } exit(0); # *** END MAIN ***
# *** SUB SENDMAIL *** sub SendEmail { my $smtp; if ( $smtp = new Net::SMTP( $smtp_host ) ) { $smtp -> mail( $from ); $smtp -> to( @to ); $smtp -> data(); $smtp -> datasend("To: $to\n"); $smtp -> datasend("Subject: $subject\n"); $smtp -> datasend("\n"); $smtp -> datasend("$message\n"); $smtp -> datasend(); $smtp -> quit(); print("Success: Sent message to @to\n"); print("$subject\n"); } else { print("Failed: send message to @to\n"); } } # *** END SENDMAIL ***
# *** SUB HW_INV *** sub hw_inv { # Gather Printer Status $WMI = Win32::OLE->new('WbemScripting.SWbemLocator') || die "Cannot access WMI on local server: ", Win32::OLE->LastError; $Services = $WMI->ConnectServer($host) || die "Cannot access WMI on remote machine: ", Win32::OLE->LastError; # Gather Computer System Information $printer_set = $Services->InstancesOf("Win32_Printer"); foreach $printer (in($printer_set)) { $printer_name = $printer->{'Caption'}; $printer_desc = $printer->{'Description'}; $printer_error = $printer->{'DetectedErrorState'}; $printer_driver = $printer->{'DriverName'}; $printer_jobs = $printer->{'JobCountSinceLastReset'}; $printer_location = $printer->{'Location'}; $printer_state = $printer->{'PrinterState'}; $printer_status = $printer->{'PrinterStatus'}; $printer_stat = $printer->{'Status'}; $printer_server = $printer->{'SystemName'};
if ($printer_error != 0) { $count++; #$message = $message . "$printer_name\t$printer_jobs\t\t$detectedErrorState{$printer_error}[0]\t\t$Status{$printer_status}[0]\t\t\t$printer_driver\n"; $message = $message . "<tr><td>$printer_name</td><td>$printer_jobs</td><td>$detectedErrorState{$printer_error}[0]</td><td>$Status{$printer_status}[0]</td><td>$printer_driver</td></tr>"; } if (($printer_error == 0) && ($printer_status == 1)) { $count++; #$message = $message . "$printer_name\t$printer_jobs\t\t$detectedErrorState{$printer_error}[0]\t\t$Status{$printer_status}[0]\t\t\t$printer_driver\n"; $message = $message . "<tr><td>$printer_name</td><td>$printer_jobs</td><td>$detectedErrorState{$printer_error}[0]</td><td>$Status{$printer_status}[0]</td><td>$printer_driver</td></tr>"; }
} $subject = "$printer_server: ($count) Printer Alerts"; } # *** END HW_INV ***
Frank
Pikelner
|
- Re: Opps sorry. sending email in HTML format Frank Pikelner
- Re: Opps sorry. sending email in HTML format Michael D. Smith
- Fwd: sending email in HTML format Michael D. Smith