#!/usr/bin/perl -w

    package IMAP_Alert;

    # System Modules

    use Data::Dumper;
    
    use DBI;
    
    use Net::IMAP::Client;

    use IMAP::Query qw(build_search_string);

    use POSIX qw(strftime);
    
    use MIME::Lite;
    
    use strict;
    
            
    #  check
    
    sub check_run(){
        
        my $lv;
        
        my $param = shift @_;
        
        print Dumper($param) if($param->{'debug'});
         
        # db handle
    
        my $dbh     =   DBI->connect('DBI:mysql:dbname='.$param->{'db_name'}.';host='.$param->{'host'}.'',$param->{'db_user'},$param->{'db_pass'});
        
        # $imap
 
        my $imap = Net::IMAP::Client->new(
         
            server => $param->{'mail_server'},
            user   => $param->{'mail_user'},
            pass   => $param->{'mail_pass'},
            ssl    => 1,                              # (use SSL? default no)
            #ssl_verify_peer => 1,                     # (use ca to verify server, default yes)
            #ssl_ca_file => '/etc/ssl/certs/certa.pm', # (CA file used for verify server) or
            # ssl_ca_path => '/etc/ssl/certs/',         # (CA path used for SSL)
            port   => 993                             # (but defaults are sane)
         
        ) or die "Could not connect to IMAP server";
    
        print "DB Handle".Dumper($dbh)."
" if($param->{'debug'}); print "IMAP Handle".Dumper($imap)."
" if($param->{'debug'}); # get message $lv->{'uid'} = $dbh->selectrow_arrayref("SELECT entity_value FROM entity_key_value WHERE entity='$param->{mail_user}' AND entity_key='last_uid'"); print "DB Result".Dumper($lv->{'uid'})."<br>" if($param->{'debug'}); $lv->{'result'} = &imap_sent_alert({'dbh'=>$dbh, 'imap'=>$imap, 'uid'=>($lv->{'uid'}->[0]+0), 'debug'=>$param->{'debug'}, 'mail_from' => $param->{'mail_user'}, 'mail_to' => $param->{'mail_to'}, 'mail_cc' => $param->{'mail_cc'} }); # Logout $imap->logout; # message if ($lv->{'result'}->{'message_count'}){ return {'message'=> "$lv->{'result'}->{'message_count'} Message Updated." }; }else{ return {'message'=> "No new Messages Updated" }; } } # end # imap_sent_alert sub imap_sent_alert(){ my $lv; my $param = shift @_; print Dumper($param) if($param->{'debug'}); my $dbh = $param->{'dbh'}; my $imap = $param->{'imap'}; my $uid = $param->{'uid'}; # var $lv->{'content'} = ''; # everything's useless if you can't login $imap->login or die('Login failed: ' . $imap->last_error); # get folders my @folders = $imap->folders; print Dumper(\@folders) if($param->{'debug'}); # IMAP query my $query = build_search_string(UID => ''.$param->{'uid'}.':*'); # select sent folder $imap->select('INBOX.Sent'); # get messages $lv->{'messages'} = $imap->search($query); shift($lv->{'messages'}); print "Messages:".Dumper($lv->{'messages'}) if($param->{'debug'}); # fetch message summaries (actually, a lot more) $lv->{'summaries'} = $imap->get_summaries($lv->{'messages'},'References X-Original-To'); print Dumper($lv->{'summaries'}) if($param->{'debug'}); foreach my $summary (@{$lv->{'summaries'}}) { if ($summary->{'uid'}){ $lv->{'from'} = $summary->{'from'}->[0]; # cc & bcc $lv->{'to'} = []; $lv->{'cc'} = []; $lv->{'bcc'} = []; for my $send_type('to','cc','bcc'){ for(@{$summary->{$send_type}}){ push(@{$lv->{$send_type}},$_->mailbox.'@'.$_->host); } } # send type # mail $lv->{'from'}->{'name'} = $lv->{'from'}->{'name'} || ''; $lv->{'from_mail'} = "$lv->{'from'}->{'name'}<".$lv->{'from'}->{'mailbox'}.'@'.$lv->{'from'}->{'host'}.'>'; #$lv->{'to_mail'} = $lv->{'to'}->{'mailbox'}.'@'.$lv->{'to'}->{'host'}; # to $lv->{'to_address'}=$lv->{'new_subject'}='To: '.join(',',@{$lv->{'to'}}) if(@{$lv->{'to'}}); # new mail subject $lv->{'new_subject'}.=' | On '.$summary->date. ' | '.$summary->subject; # cc & bcc $lv->{'new_subject'}.=' | Cc: '.join(',',@{$lv->{'cc'}}) if( scalar(@{$lv->{'cc'}}) > 1); $lv->{'new_subject'}.=' | Bcc: '.join(',',@{$lv->{'bcc'}}) if(scalar(@{$lv->{'bcc'}}) > 1); # log content $lv->{'content'}.=''. '<dl><dt>From</dt><dd>'.$lv->{'from'}->{'name'}.'<'.$lv->{'from'}->{'mailbox'}.'@'.$lv->{'from'}->{'host'}.'></dd></dl>'. '<dl><dt>To</dt><dd>'.$lv->{'to_address'}.'</dd></dl>'. '<dl><dt>Date</dt><dd>'.$summary->date.'</dd></dl>'. '<dl><dt>Sub</dt><dd>'.$summary->subject.'</dd></dl><hr>'; # get message content $lv->{'mail_body_ref'} = $imap->get_rfc822_body($summary->{'uid'}); $lv->{'last_uid'} = $summary->{'uid'}; $lv->{'mail_body'} = ${$lv->{'mail_body_ref'}}; @{$lv->{'match_content_html'}} = $lv->{'mail_body'}=~m/(\<html\>)(.[^\*]+)(\<\/html\>)/ig; $lv->{'mail_content_html'} = join('', @{$lv->{'match_content_html'}}); # new mail $lv->{'mail'} = MIME::Lite->new( From =>$param->{'mail_from'}, # To =>'catchall@srinrd.in', # Cc => 'raja@webstarscg.com', To =>$param->{'mail_to'}, Cc => $param->{'mail_cc'}, Subject => $lv->{'new_subject'}, Data =>"$lv->{'mail_content_html'}", ); $lv->{'mail'}->attr("content-type" => "text/html"); $lv->{'mail'}->attr("content-type.charset" => "UTF-8"); # $lv->{'mail'}->send; $lv->{'mail'}->send(); } # end } # each message # update last id if ($lv->{'last_uid'}) { $dbh->do("UPDATE entity_key_value SET entity_value=$lv->{'last_uid'} WHERE entity='$param->{mail_from}' AND entity_key='last_uid'"); } #end # last run $dbh->do("INSERT entity_key_value (entity,entity_key,entity_value) VALUES ('$param->{mail_from}','last_run',".scalar(@{$lv->{'messages'}}).")"); # disconnect $dbh->disconnect; return{ 'content' => $lv->{'content'}, 'message_count' => scalar(@{$lv->{'messages'}}) }; } # alert sent 1;