drodinthe559 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, is there a good way to add a result set that's stored in an array into the body of message in Outlook. I'm pulling data from a report each night and sending an email in the morning.

#!/usr/bin/perl -w use strict; use warnings; use POSIX; use Win32::OLE; open SOURCE, "< //reports/test.txt"; my $procdate = POSIX::strftime('%m/%d/%Y %H:%M', localtime); my @ies_details; my $Outlook = Win32::OLE->GetActiveObject('Outlook.Application') || Wi +n32::OLE->new('Outlook.Application'); #$Outlook = new Win32::OLE('Outlook.Application'); while(<SOURCE>) { my $source = $_; next if ($source =~ m/^(\s)*$/); next unless (substr($source,11,4) =~ m/(\d){4}/); if (substr($source,79,10) =~ /(\d){10}/) { push @ies_details,$source; } } my $test; foreach (@ies_details) { $test = $test . $_; }; my $mailitem = $Outlook->CreateItem(0); #Create mail Item unless ($mailitem) {die "Outlook is not running, cannot send mail. +\n";} $mailitem->{'To'} = 'test@yahoo.com'; $mailitem->{'Cc'} = 'test@test.com'; $mailitem->{'Subject'} = 'Summary Report ' . $procdate; $mailitem->{'Body'} = 'Below is a summary.' . "\n\n" . '-----------------------------------------' . "\n\n" . $test; $mailitem->{'Importance'} = 0; # 2=high, 1=normal, 0=low $mailitem->Send;

Replies are listed 'Best First'.
Re: Add Results to Outlook body
by rev_1318 (Chaplain) on Jun 09, 2011 at 17:25 UTC

    What do you mean by "good way"? There are allways more ways to do it...

    If the above code works, it works. Not that there are things to improve; for example, you fill an array, which you later join. Why not join the lines directly?A (slightly rewriten) version of your code could be:

    #!/usr/bin/perl use strict; use warnings; use POSIX; use Win32::OLE; open my $source, "<", "//reports/test.txt" or die "cannot open file: $!\n"; my $ies_details; while( <$source> ) { next unless substr $source,11,4 =~ /\d{4}/; next unless substr $source,79,10 =~ /\d{10}/; $ies_details .= $_; } my $Outlook = Win32::OLE->GetActiveObject('Outlook.Application') || Win32::OLE->new('Outlook.Application'); my $mailitem = $Outlook->CreateItem(0) or die "Outlook is not running, cannot send mail.\n"; my $procdate = POSIX::strftime('%m/%d/%Y %H:%M', localtime); $mailitem->{'To'} = 'test@yahoo.com'; $mailitem->{'Cc'} = 'test@test.com'; $mailitem->{'Subject'} = 'Summary Report ' . $procdate; $mailitem->{'Body'} = "Below is a summary\n\n" . "-----------------------------------------\n\ +n" . $ies_details; $mailitem->{'Importance'} = 0; # 2=high, 1=normal, 0=low $mailitem->Send;

    Paul

      Thank you, Paul. This is a lot more elegant than what I had. Thanks again for your help.