Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Printing Data to Email body from Perl

by Anonymous Monk
on Oct 17, 2011 at 15:35 UTC ( [id://931951]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,
Below script reads a log file (which contains a list of id's) and selects the same Data from the log file with the Oracle tables. Can someone help how to extent the script in a way that : when selecting /reading if it finds unmatched id's it has to send an email with the Body of the email as:

log: 17
Database: 16
list of not found id :
id1
id2

#!user/local/bin/perl use strict; use warnings; use DBI; my $dir = '/usr/home/Scripts/Test'; # get the text files from the specified dir opendir DIR, $dir or die "could not open directory $dir: $!"; my @files = grep /\.txt$/, readdir DIR; closedir DIR; my $dbh = DBI->connect("dbi:Oracle:****", "****", "*****" ) || die( $DBI::errstr . "\n" ); $db->{AutoCommit} = 0; $db->{RaiseError} = 1; $db->{ora_check_sql} = 0; $db->{RowCacheSize} = 16; foreach my $fil (@files) { # read the ids from the files open IN, "$dir/$fil" or die "could not read $fil: $!"; #my @ids = map { chomp; "'$_'" } <IN>; my @ids = map { "'$_'" } map { chomp; split /\s+/ } <IN>; print "checking ", scalar(@ids), " ids from $fil\n"; print"@ids\n\n"; my $sel=$db->prepare("select (number||'/'||id) as List_ID from Info wh +ere List_ID in (" . join(', ', @ids) . ')'); $sel->execute(); while(my $subref = $SEL->fetchrow_hashref()) { my $list=$subref->{'LIST_ID'}; print "$list\n"; }

Replies are listed 'Best First'.
Re: Printing Data to Email body from Perl
by zentara (Archbishop) on Oct 17, 2011 at 16:04 UTC
    This snippet might be helpful, and there are modules for checking lists.
    #!/usr/bin/perl my @a = qw(a b c d e f); my @b = qw(c d e f g h); my ($only_a, $only_b, $both, $either) = listy(\@a, \@b); print "only in a: @$only_a\n"; print "only in b: @$only_b\n"; print "both: @$both\n"; print "either: @$either\n"; sub listy { my %tmp1; for (0..1) { for my $k (@{$_[$_]}) { $tmp1{$k} .= $_; } } my %tmp2; while (my($k, $v) = each %tmp1) { push @{$tmp2{$v}}, $k; } return @tmp2{"0", "1", "01"}, [keys %tmp1]; }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      Hi,

      Thank you for the code snippet but I am looking for something which can go with the exsisting script rather than new one. so is it possible to extend the current script

        I'm sure you have already looked at the Email modules that CPAN provides. Please tell us why the modules are unsatisfactory and didn't do what you needed. Particularly, MIME::Lite works very well in sending emails.

        If you have other questions than how to send mail from a Perl script, maybe you should make that clearer in your post.

        Also note that we expect you to do your own work. This is not a script writing service, so "something which can go with the existing script" will have to be something you write from the solutions you find.

Re: Printing Data to Email body from Perl
by zentara (Archbishop) on Oct 17, 2011 at 17:49 UTC
    After you check your lists, here is a basic mailing subroutine. Change the Content Type to plain text if you don't want to add attachments.
    #!/usr/bin/perl #This is the sending routine from a larger program which uses both Mim +e::Lite # and Net::SMTP, which does the mail server login. # Variable names passed to the routine should be self explanatory. sub sendmsg { my ( $From_Addr, $Mail_Name, $Subj_Text, $Body_Text, $File_Name, $Mime_Data,) = @_; my $msg = MIME::Lite->new( From =>$From_Addr, To =>$Mail_Name, Subject =>$Subj_Text, Type =>'multipart/mixed', ); $msg->attach(Type=>'TEXT', Data=>$Body_Text, ); $msg->attach(Type=>'application/octet-stream', Data=>$Mime_Data, Filename=>$File_Name, ); MIME::Lite->send('smtp', "mail.somewhere.net", Timeout=>60); }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://931951]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-19 18:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found