Search your Gmail account for any string





4
Date Submitted Tue. Oct. 10th, 2006 12:39 PM
Revision 1 of 1
Beginner robert
Tags blurb | Email | Gmail | message | regex
Comments 1 comments
This perl subroutine uses the CPAN module Mail::Webmail::Gmail to iterate through a Gmail account searching for a particular string. If a match is found, the sender's name along with his/her email address, the subject, and the "blurb" is printed to standard out. Also, all messages found will be archived automatically within Gmail to clear the message from the inbox folder. The subroutine returns the number of messages found back to the calling environment.

Sample invocation: check_email(username, password);

By default, the subroutine will use encryption to connect to Gmail however, by setting encrypt_session to '0' will disable encryption.

use Mail::Webmail::Gmail;

sub check_email {
   my ($username, $password) = @_;
   my $msg_found = 0;
   my $gmail = Mail::Webmail::Gmail->new( username => $username,
                                          password => $password,
                                          encrypt_session => 1
                                        );
   my $messages = $gmail->get_messages( label => $Mail::Webmail::Gmail::FOLDERS{ 'INBOX' } );
   
   # iterate through each message
   foreach my $message( @{ $messages } ) {
      if ($message->{ 'blurb' } =~ /your-regex/gi || $message->{ 'subject' } =~ /your-regex/gi) { 
                
             ++$msg_found;
           
             print "<<Found email>>"  .
                   "\nSender Name: "  . $message->{'sender_name'}  .
                   "\nSender Email: " . $message->{'sender_email'} .
               "\nSubject: "      . $message->{'subject'     } .
               "\nBlurb: "        . $message->{'blurb'       } . "\n\n";
               
           
         $gmail->edit_archive( action => 'archive', 'msgid' => $message->{ 'id' } );
      }
   } 
   return $msg_found;
}
 

Robert Pena

www.penacentral.com

Comments

Comments patern as a parameter
Wed. Oct. 11th, 2006 8:20 AM    Scripter ctiggerf

Voting