Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Problems? Is your data what you think it is?
 
PerlMonks  

Mail check?

by odie (Sexton)
on May 23, 2001 at 23:21 UTC ( #82685=perlquestion: print w/ replies, xml ) Need Help??
odie has asked for the wisdom of the Perl Monks concerning the following question:

Ok. I would like to check whether I have mail or not. I suppose I could do some fancy parsing of /var/spool/mail/username, but as this can sometimes have size 0 and sometimes contain a "non-message" that apparently is a, quote "part of the internal format of your mail folder" it would prove a hassle. And maybe unnecessary.
Is there a fancy pm that can perform such a check on the mail spool or possibly a program that will do this that I can use from within perl?

Odie

--
I am a manual signature virus. Copy me please!

Comment on Mail check?
Re: Mail check?
by Beatnik (Parson) on May 23, 2001 at 23:50 UTC
    Check these fancy Mail::Box and Mail::Folder modules :)

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
Re: Mail check?
by tune (Curate) on May 23, 2001 at 23:52 UTC
    Mail::Util should be helpful for you. I never tried it, but looks good to parse the local mailbox.

    --
    tune

      Why not pipe mailspool to md5sum, and compare the checksums ever so often? -malloc
        Well, this will indeed show a change in the spool, but it will not quite tell me if there are messages in it. The md5 check will go off when new mail arrives, but also when mail is read and removed from the spool. =)

        Odie

        --
        I am a manual signature virus. Copy me please!
      Mail::Util does indeed have a handy read_mbox routine which reads a file and returns a list of references. Only problem with it is that it immediately reads all of the messages in the file into memory. I have several truly huge mailboxes, which cause my Perl process to crash and burn when I try to read them with read_mbox.

      So use read_mbox with caution.

      stephen

Re: Mail check?
by petdance (Parson) on May 24, 2001 at 00:14 UTC
    Here's what I wrote to check my POP3 box, mostly stolen from Lincoln's stellar Network Programming With Perl. I added some functionality to make it fetch a message if I specify -r n.
    #!/usr/bin/perl -w use constant VERSION => '1.03'; use strict; use Net::POP3; use Mail::Header; use Term::ReadPassword; use Getopt::Long; use lib "."; use Config::rc; my $filename = "~/.mcheckrc"; my $rcfile = new Config::rc( $filename ) or die $Config::rc::Error, "\ +n"; my $host = $rcfile->parm( "host" ) or die "Mail host not specified in +$filename\n"; my $user = $rcfile->parm( "user" ) or die "User name not specified in +$filename\n"; my $readno = 0; GetOptions( "r=i", \$readno ); my $password = read_password( "Password: " ) || exit 0; my $pop = Net::POP3->new( $host, Timeout=>30 ) or die "Can't connect t +o $host: $!\n"; my $messages = $pop->login( $user, $password ) or die "Can't log in: " +, $pop->message, "\n"; my $last = $pop->last; $messages += 0; # In case it's "OEO" (zero but true) print "mcheck v", VERSION, ": Inbox has $messages messages (", $messag +es-$last," new)\n"; if ( ($readno > 0) && ($readno <= $messages) ) { my $lines = $pop->get($readno); print @$lines, "\n"; } else { for my $msgnum (reverse(1..$messages)) { my $header = $pop->top( $msgnum ); my $parsedhead = Mail::Header->new($header); chomp( my $subject = $parsedhead->get('Subject')); chomp( my $from = $parsedhead->get('From')); $from = clean_from($from); printf "%4d %-25s %-50s\n", $msgnum, $from, $subject; } # for } # else $pop->quit; sub clean_from { local $_ = shift; /^"([^\"]+)" <\S+>/ && return $1; /^([^<>]+) <\S+>/ && return $1; /^\S+ \(([^\)]+)\)/ && return $1; return $_; }
    Oh yah, it uses a Config::rc module that I've been working on, but you can just hardcode your server and username if you want.

    xoxo,
    Andy

    %_=split/;/,".;;n;u;e;ot;t;her;c; ".   #   Andy Lester
    'Perl ;@; a;a;j;m;er;y;t;p;n;d;s;o;'.  #   http://petdance.com
    "hack";print map delete$_{$_},split//,q<   andy@petdance.com   >
    
      Nice hack, but I doubt it will help, unless I install a pop3 server on my localhost. Hardly seems worth the bother, don't you agree? ;)
      Odie
      --
      I am a manual signature virus. Copy me please!
        Nice hack, but I doubt it will help, unless I install a pop3 server on my localhost. Hardly seems worth the bother, don't you agree? ;)

        Hey, I just supply the code. The decision to use the code is an exercise best left to the reader. :-)

        xoxo,
        Andy

        %_=split/;/,".;;n;u;e;ot;t;her;c; ".   #   Andy Lester
        'Perl ;@; a;a;j;m;er;y;t;p;n;d;s;o;'.  #   http://petdance.com
        "hack";print map delete$_{$_},split//,q<   andy@petdance.com   >
        
Re: Mail check?
by Anonymous Monk on May 24, 2001 at 00:33 UTC
    You need to test the times of your mbox. Accessed after written = No Mail, written after accessed=NEW Mail.
      I was thinking of this also but you would set off a mail alert if you did something like delete a message.

      Ira.

      I am a manual signature virus. Copy me please!
      I am a manual signature virus. Copy me please!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://82685]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2014-04-21 15:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (495 votes), past polls