background has asked for the wisdom of the Perl Monks concerning the following question:

Dearest Monks, i have setup a gmail account to receive google alerts. i wrote the script below to dump the header and body of any email received by this account into a TXT file. my problem is this - when the body of the email is dumped into the TXT file i only get the resulting text and not the underlying HTML code. for example, what i want in the TXT file is:
<a href="http://www.blahblah.com/article.html">The Article Link</a>
Not:
The Article Link
my code is below. any thoughts? please note, the script below is only designed for capturing the header / body of a single email.
#!/usr/bin/perl -w use strict; use Mail::POP3Client; my $user = 'blahblahblah'; my $pass = 'blahblahblah'; my $pop = new Mail::POP3Client( USER => $user, PASSWORD => $pass, HOST => "pop.gmail.com", PORT => 995, USESSL => 'true', ); my $count = $pop->Count(); if ($count < 0) { print $pop->Message(); } elsif ($count == 0) { print "no messages\n"; } else { for my $i (1 .. $count) { open(STDOUT, ">my_txt_output.txt"); my $fh = new IO::Handle(); $fh->fdopen( fileno( STDOUT ), "w" ); $pop->HeadAndBodyToFile( $fh, $i ); close STDOUT; } } $pop->Close();
many thanks...

Replies are listed 'Best First'.
Re: How can I capture HTML code from my POP3 email body
by ikegami (Patriarch) on Oct 14, 2008 at 19:15 UTC

    I don't think Mail::POP3Client does any such translation. It dumps to the file exactly what it receives from the server.

    By the way, what's this crazyness?

    open(STDOUT, ">my_txt_output.txt"); my $fh = new IO::Handle(); $fh->fdopen( fileno( STDOUT ), "w" ); $pop->HeadAndBodyToFile( $fh, $i ); close STDOUT;

    You should use

    open(my $fh, '>', 'my_txt_output.txt') or die("Can't create my_txt_output.txt: $!\n"); $pop->HeadAndBodyToFile( $fh, $i );
      good stuff -- many thanks for the clean up. i'm new to perl and unfortunately have buckets of crazyness in my code...

      but back to my original problem. i did some searching and found modules for sending HTML mail (such as HTML::Mail), but didn't find anything for retrieving HTML mail. i'm not sure but i believe the gmail servers do preserve the 'google alerts' messages in HTML. any thoughts out there about how to retrieve the HTML mail using perl? thanks again.

Re: How can I capture HTML code from my POP3 email body
by perreal (Monk) on Oct 14, 2008 at 22:53 UTC
    I studied checkgmail code for a similar project and it worked for me. I think you can derive a solution from checkgmail.