My work email tends to accumulate a lot of cruft, even after filtering away the noise that can be recognised automatically with regexes, so I wrote this script (used as a conky plugin) which polls my IMAP mailbox & returns the sender & subject of unread messages, suitably trimmed. Then I can see at a glance whether any need attention.
#!/usr/bin/perl #===================================================================== +========== # # FILE: unread_email.pl # # USAGE: ./unread_email.pl # # DESCRIPTION: Check for unread IMAP email, return sender & subject # # OPTIONS: none # REQUIREMENTS: Mail::IMAPTalk # BUGS: Only checks INBOX # NOTES: --- # AUTHOR: (Stephen Patterson), <steve@patter.mine.nu> # COMPANY: # VERSION: 1.0 # CREATED: 23/01/08 10:50:37 GMT # REVISION: --- #===================================================================== +========== use strict; use warnings; my $username = 'imap user'; my $password = 'imap password'; my $server = 'imap server'; use Mail::IMAPTalk; sub find_messages { my $IMAP = Mail::IMAPTalk->new( Server => $server, Username => $username, Password => $password, Uid => 1 ) or die "Couldn't connect"; $IMAP->select('inbox'); my @MsgIds = $IMAP->search('not', 'seen'); my @messages; foreach my $uid (@MsgIds) { my %info = fetch_message($IMAP, $uid); push @messages, \%info; } $IMAP->logout(); return \@messages; } sub fetch_message { # fetch a message ID, display some information my ($IMAP, $uid) = @_; my $Msg = $IMAP->fetch($uid, 'envelope')->{$uid}->{envelope}; # clean the sender $Msg->{From} =~ s/(")|(<.*>)|(\s{2,})|(\s+$)//g; # clean the subject $Msg->{Subject} =~ s/\[[[:alpha:]]+#\d{6}\]//g; $Msg->{Subject} =~ s/(\(.*\))|(\s{2,})|(\s+$)|(^\s+)//g; return ( From => $Msg->{From}, Subject => $Msg->{Subject}); } my $messages = find_messages(); if (scalar (@$messages) > 0) { foreach my $msg (@$messages) { print $msg->{From}, " - ", $msg->{Subject}, "\n"; } } else { print "No mail"; }

just another cpan module author

Replies are listed 'Best First'.
Re: Is my email worth reading?
by okram (Monk) on Feb 12, 2008 at 18:38 UTC
    A little change in order for it to work with GMail
    • $server should be servername:port (for gmail, 'imap.gmail.com:993')
    • use IO::Socket::SSL; # too
    • instead of the new() you use, do:
      my $sock = IO::Socket::SSL->new("$server") or die "Problem connecting via SSL to $server: ", IO::Socket::SSL::errs +tr(); my $ofh = select($sock); $| = 1; select ($ofh); my $IMAP = Mail::IMAPTalk->new( Socket => $sock, State => Mail::IMAPTalk::Authenticated, Username => $username, Password => $password, Uid => 0) or die "Could not query on existing socket. Reason: $@";
    Tested, works wonders with GMail :)
    Thanks for the tip! ++
Re: Is my email worth reading?
by telemachus (Friar) on Apr 02, 2008 at 01:41 UTC
    Thanks for the script: Playing with it on the command line rather than as a conky plugin, I added an option -a/--account that allows you to specify one of multiple accounts (or use a default which is preset, if you don't use the optional flag).
Re: Is my email worth reading?
by sv87 (Acolyte) on Aug 19, 2008 at 22:40 UTC
    Thanks a lot for the script, it's very handy indeed. I had the same problem here, of not being able to quickly assess what message is interesting or not, due to the huge amount of email. It works great as a command line tool as well, so I usually use it to scan the output for interesting tidbits about work projects or something. Thanks alot :-D