CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
How to check POP mail account with Perl?

 



Warren Bell
Deleted

Feb 6, 2000, 9:21 AM

Post #1 of 6 (2741 views)
How to check POP mail account with Perl? Can't Post

I want to write a small script that will check my pop mail accounts but I'm not sure what the syntax is when dealing with mail. I've written a couple small programs so I know a littleabout Perl.

What I want it to do is be called by the web browser and print the status in real time. Somthing like:

Checking account Foo...
Found 1 message on Foo

Checking account Bar...
Found 0 messages on Bar


That's all I want it to do for now. After I get that working I want to have it filter the mail through rules that you can specify. So the output might be somthing like:

Checking account Foo...
Found 3 message on Foo
1 message on Foo was in the junk list
Deleteing junk mail...
Done.

If anyone can tell me how to do this or give me some pointers I'd really appreciate it. Thanks.


Jasmine
Administrator

Feb 5, 2000, 10:11 PM

Post #2 of 6 (2741 views)
Re: How to check POP mail account with Perl? [In reply to] Can't Post

The easiest way to check a pop account is to use the Mail::POP3Client module, which offers a lot of options, including getting the count of messages, getting the total size of the mailbox, deleting messages, etc.

No need to reinvent the wheel here Smile

[This message has been edited by Jasmine (edited 02-06-2000).]


Warren Bell
Deleted

Feb 6, 2000, 1:40 PM

Post #3 of 6 (2741 views)
Re: How to check POP mail account with Perl? [In reply to] Can't Post

Perfect! Ok I got this code below working as a command line script but I need to figure out the way to detect if the "To:" field of the header contains the $user variable. Do you know how I can do that? I looked at the documentaion for the module but can't find it anywhere. I'll highlight the part I'm talking about:

------------------
#!/usr/bin/perl

use POP3Client;

@hosts = (
"first.mail.host",
"second.mail.host",
);

$user = 'username';
$pass = 'xxxxxx';

foreach $host (@hosts) {
$pop = new Mail::POP3Client(
USER => "$user",
PASSWORD => "$pass",
HOST => "$host",
);

print "\nChecking for mail at $host...\n\n";

$cnt = 0;
for ($i = 1; $i <= $pop->Count(); $i++) {
$cnt++;
foreach ($pop->Head($i)) {
/^(From):\s+/i and print $_, "\n";
#------if statement to detect username in To field of header------#
if ((To) !~ /* $user/) {
/^(From):\s+ - possible spammer\nDeleting message in 3 seconds../i and print $_, "\n";
sleep 3;
$pop->Delete;
#------end if statement------#
}
}
}

if ($cnt != 0){
print "\n";
}

print "Found $cnt message\(s\) at $host\n";
}
---------------


Jasmine
Administrator

Feb 6, 2000, 3:42 PM

Post #4 of 6 (2741 views)
Re: How to check POP mail account with Perl? [In reply to] Can't Post

Try the code below:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

for ($i = 1; $i <= $pop->Count(); $i++) {
foreach ( $pop->Head( $i ) ) {
/^(From|Subject):\s+/i and print $_, "\n";
if (($_ =~ /^To:\s+/i)&&($1 !~ $user)){
print "$_ - possible spam\n";
# Whatever else you wanted to do with spam.
}
}
}
</pre><HR></BLOCKQUOTE>

I've run a couple of tests on it and it seems to be working properly.

Good luck!


Warren Bell
Deleted

Feb 7, 2000, 10:09 AM

Post #5 of 6 (2741 views)
Re: How to check POP mail account with Perl? [In reply to] Can't Post

Thanks! I got it to work but had to make a couple adjustments.

One other thing, since I'm running it from the command line can I specify certain text to be a certain color? I think it's called ANSI colors. How whoul I write that in for a certain line of text?

Thanks again, great board.


Jasmine
Administrator

Feb 7, 2000, 12:20 PM

Post #6 of 6 (2741 views)
Re: How to check POP mail account with Perl? [In reply to] Can't Post

Always remember, CPAN is your friend Smile

Term::ANSIColor

Synopsis
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


use Term::ANSIColor;
print color 'bold blue';
print "This text is bold blue.\n";
print color 'reset';
print "This text is normal.\n";
print colored ("Yellow on magenta.\n", 'yellow on_magenta');
print "This text is normal.\n";

use Term::ANSIColor qw(:constants);
print BOLD, BLUE, "This text is in bold blue.\n", RESET;

use Term::ANSIColor qw(:constants);
$Term::ANSIColor::AUTORESET = 1;
print BOLD BLUE "This text is in bold blue.\n";
print "This text is normal.\n";
</pre><HR></BLOCKQUOTE>

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives