#!/usr/local/bin/perl -w ####################################### # # New Mail Count Notification - IMAP # # Copyright (C) Justin Ribeiro # Project Page - http://www.j5studios.com/projects/notification/index.php # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 of # the License or (at your option) any later version. # # This program is distributed in the hope that it will be # useful, but WITHOUT ANY WARRANTY; without even the implied warranty # of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 # USA # # NOTES: # This little script was written because sometimes I'm not in front of a # computer, and would like to know when I receive new email. So I decided # I would send the number of new messages to my cell phone. # # This script is very basic; it doesn't tell you who the new email is from # or anything like that. Basically, this is my rough cut starting point for # more advanced ideas. # # You must set the variables below for your specific server / username. This # script only works with IMAP servers, mostly because I'm using IMAP. # # To make this script run automated, I suggest cron. # # Feel free to make changes and additions; I'm open to suggestions # and fixes. # # REQUIREMENTS: # You'll need the following CPAN modules installed: # -> Net::IMAP::Simple # -> Mail::Sendmail # ####################################### use Net::IMAP::Simple; use Mail::Sendmail; ###################################### # # Set the variables below # ###################################### my $mailbox = 'Inbox'; # name of the folder / mailbox my $hostname = 'somemail.server.com'; # mail server my $username = 'yourusername'; # mailbox username my $password = 'yourpassword'; # mailbox password my $subject = 'New Mail'; # subject line my $from = 'address@example.com'; # server from address my $recipient = 'Cell Phone '; # e-mail address to send to...aka the cell phone ###################################### # Editing below this line could be a hazard.... ###################################### # Connect to the Server my $server = Net::IMAP::Simple->new( $hostname ); $server->login( $username, $password ); # Open the Specified Folder my $number_of_messages = $server->select( $mailbox ); # Start the new mail counting loop my $count = 0; foreach my $msg ( 1 .. $number_of_messages ) { if ( $server->seen( $msg ) ) { # do nothing - Not a new message } else { $count++; }; } # Close the connection $server->quit(); if ( $count == 0 ) { # no need to send mail } else { # I'm being picky if ( $count == 1 ) { $message = "You have 1 new email waiting."; } else { $message = "You have $count new emails waiting." } # Setup the notify message %mail = ( To => $recipient, From => $from, Subject => $subject, Message => $message ); # Send the message sendmail(%mail) or die $Mail::Sendmail::error; }