Finding and Printing Data to Email in perl

Posted on 2011-10-14
Last Modified: 2012-05-12

Below script reads a log file and compare the Data from the log file with the Oracle tables.

Can someone help to extent the script in a way that :

After comparing if it finds some Data unmatched it has to send an email with the Body of the email as:
      log: 17
      DB: 16
            list of not found id :

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";

my $sel=$db->prepare("select (number||'/'||id) as List_ID from Info where List_ID in (" . join(', ', @ids) . ')');
while(my $subref = $SEL->fetchrow_hashref()) {
    my $list=$subref->{'LIST_ID'};
print "$list\n";


Open in new window

Question by:new_perl_user

    Author Comment



    can some one please help with the above issue..
    LVL 4

    Accepted Solution

    I don't know if you gave up or nobody answered ...

    But - You can manually create a well formed e-mail message, then pipe your output to sendmail.

    Here's how you pipe to sendmail:  

    open(MAIL,"|/path/to/sendmail -t"); # Open a pipe to Sendmail.  This is done via the |  
    print MAIL "From: $email \n"; 
    print MAIL "To: $recipient\n";
    print MAIL "Reply-To: $email\n";
    print MAIL "Subject: $subject\n";
    # Create the standard headers 
    print MAIL "X-Header: Put your custom headers here\n";
    print MAIL "\n"; # End the headers with a double-newline. 
    print MAIL << "END_DATA";
    Put all of your mail text here. 
    You can use print lines, or a print block like this. 
    Stick your $vars in here , etc.... 
    print MAIL "Variables not found: \n";
    $counter = 0; 
    foreach $var (@array) { 
    	print MAIL "" . ($counter++) . ": $var\n";
    close (MAIL);  # Finish your message. 

    Open in new window


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
    Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now