Saturday, 7 September 2013

Perl Oracle DB connectivity and send mail

Perl Script Create report using mentioned Script and send mail to Team.
This need Sendmail to configured on servers along with SMTP details.


#!/usr/bin/perl

#

#use EXCEL;

#use MAIL;

#use CGI;

use DBI;

#use Spreadsheet::WriteExcel::Big;

$attachement=$ARGV[0];

#$subject_line=$ARGV[1];

#$ENV{ORACLE_HOME} = '/u01/app/oracle/product/9.2.0';  ##Linux

$ENV{ORACLE_HOME} = 'C:\app\Dilip\product\11.1.0\db_1';

$stsql = "select * from dba_segments where rownum<=10";

my $ORACLE_SID="orcl";

my $user = 'xxxx';

my $password = 'xxxxx';

#my $data_source = 'dbi:Oracle:ORCL';

my $data_source = 'dbi:Oracle:orcl';

my $dbh =  DBI->connect($data_source, $user, $password)

  || die "Can't connect to $data_source: $DBI::errstr";

$sth = $dbh->prepare("select * from  dba_segments where rownum<=10 and segment_type='TABLE'")

       || die $dbh->errstr;

$sth->execute() || die $sth->errstr;

# Hash Table for column wise return

#while (my $r = $sth->fetchrow_hashref) {

   #print "$r->{OWNER}, $r->{SEGMENT_NAME}\n";

#

#}

open my $fh, '>>', $attachement or die "Could not open file Output.csv: $!";

### Retrieve the returned rows of data All Rows

  while ( @row = $sth->fetchrow_array(  ) ) {

     # print "Row: @row\n";

     #push(@row);

     #print $fh qq{$res->{'Name'}\t$res->{'CompanyName'}\n};

     print $fh qq{@row\n};

  }

$sth->finish;

$dbh->disconnect();

close $fh;

send_mail($attachement,$subject_line,"","xyz\@gmail.com","","");

sub get_data

{

  undef $/;

  open (my $QFH, "< summary.sql") or die "error can't open this file $!";

  my $sth= $dbh_oracle->prepare(<$QFH>) or

      die ("Cannot connect to the database: ".$DBI::errstr."\n");

  $sth->execute;

  close $QFH;

  my $row = $sth->fetchrow_hashref;

  $sth->finish;

  return @$row{'MYTABLE','FLAG'};

}

sub send_mail {

#my ($dbh,$SEP_COL,$querystr_v,$SHEET_NAME)=@_;

#my ($SEP_COL,$querystr)=@_;

my ($attachement,$subject_line,$message,$to,$cc,$bcc)=@_;

my %mime =('GZ',"application/x-gzip", );

                   my $mailprog = '/usr/sbin/sendmail -t';

                   #my $subject_line = "Mail Demo gip file";

                   open(MAIL, "|$mailprog -t ") || &Abort ("Error sending mail Error is $!");

                   print MAIL "To: $to\n";

                   #print MAIL "Bcc:  xxxx@xyz.com, xxxx\@xyz.com\n";

                   #print MAIL "From: mobile\@xyz.com\n";

                   print MAIL "Subject: $subject_line\n";

                   print MAIL "MIME-Version: 1.0\n";

                   #Part 2 starts

                   print MAIL "Content-Type: multipart/mixed; boundary=------------$boundary\n";

                   print MAIL "\n";

                   print MAIL "This is a multi-part message in MIME format.\n";

                   print MAIL "--------------$boundary\n";

                   print MAIL "Content-Type: text/html; charset=us-ascii\n";

                   print MAIL "Content-Transfer-Encoding: 7bit\n\n";

                   print MAIL "$message\r\n";

                   print MAIL "--------------$boundary\n";

                   print MAIL "Content-Type: name='$attachement'\n";

                   print MAIL "Content-Transfer-Encoding: base64\n";

                   print MAIL "Content-Disposition: attachment; filename=$attachement\n\n";

                   my $buf; $/=0;

                   open INPUT, "$attachement"; # should be readable, we checked above [-r]

                   binmode INPUT if ($^O eq 'NT' or $^O eq 'MSWin32');

                   while(read(INPUT, $buf, 60*57))

                   {

                   print MAIL &encode_base64($buf);

                   }

                   close INPUT;

                   print MAIL "\n--------------$boundary--\n";

                   print MAIL "\n";

                   close MAIL;

                   sub Abort {

                   my ($msg) = @_;

                   } # end of sub Abort...

                   sub encode_base64 #($)

                   {

                                   my ($res, $eol, $padding) = ("", "\n", undef);

                                   while (($_[0] =~ /(.{1,45})/gs))

                                   {

                                   $res .= substr(pack('u', $1), 1);

                                   chop $res;

                                   }

                                   $res =~ tr#` -_#AA-Za-z0-9+/#;                  # ` help emacs

                                   $padding = (3 - length($_[0]) % 3) % 3;         # fix padding at the end

                                   $res =~ s#.{$padding}$#'=' x $padding#e if $padding; # pad eoedv data with =s

                                   $res =~ s#(.{1,76})#$1$eol#g if (length $eol); # lines of at least 76 characters

                                   return $res;

                   }

#exit;

}

exit;

###For DBI refer http://oreilly.com/catalog/perldbi/chapter/ch04.html

No comments:

Post a Comment