Programmer's Corner - Perl Source Code Sample

Backup and Security Solutions 10% off all products with promo code: VISI-P1YR
Get the Programmer's Corner FireFox Search Plug-In

mail.pl - Perl

G Rowland

http://www.angelfire.com/ego/rowland/

growland84@hotmail.com

         

         

Use POP3 and MIME-tools to list emails waiting on server





#!/usr/bin/perl -w
# Use POP3 and MIME-tools to list emails waiting on server
# Pass email password on command line
# For some good coumentation of these classes, go to:
# http://search.cpan.org/dist/MIME-tools/

use Net::POP3;
use MIME::Parser;
use MIME::Entity;
use MIME::Head;

$popserver= '**********';
$username= '*******';
$password= $ARGV[0];

# Connect to the server...
$pop= Net::POP3->new($popserver)  or die "Can't connect to $popserver: $!\n"; # Net::POP3
$pop->login($username, $password)  or die "Can't authenticate $username: $!\n";

# Initialize the parser...
$parser= new MIME::Parser;
$parser->output_to_core(1); # simpler, but less scalable
$parser->ignore_errors(1); # more tolerant of weird input

sub getmsg {
    my ($msgid)= @_;
    my $msg= $pop->get($msgid);
    return $parser->parse_data($msg); # MIME::Entity
}

sub trimeol {
    my ($val)= @_;  $val=~ s|\n||ios;  return $val;
}

sub domsg {
    my ($msgid)= @_;
    my $ent= getmsg($msgid); # MIME::Entity
    my $head= $ent->head or return; # MIME::Head
    my $from= trimeol($head->get('From'));
    my $subject= trimeol($head->get('Subject'));;
    print "$msgid: $from  $subject\n";
}

# The main loop...
$msgs= $pop->list  or die "Can't list $username: $!\n";
foreach $msgid (keys %$msgs) {
    domsg($msgid);
}