Tivo Hacks. 100 Industrial-strength Tips Tools 2003

   

Hack 61 Watching Your Email

Is a picture indeed worth a thousand words? That depends on how many words are in the picture. Render your email as PNG images, suitable for viewing via the TiVo HMO's photo sharing .

Series 2 users may not be able to check their email on their TiVo using TPOP [Hack #57], but this is nothing a little Perl scripting can't fix. Tobias Hoellrich comes through again with mailrender.pl (http://www.kahunaburger.com/blog/archives/000052.html). The script runs every so often, fetches any new mail from your POP3 mail server, and renders email as PNG image files for display via TiVo's Home Media Option (HMO) [Hack #63] on your TV.

In addition to Perl (http://www.perl.com) itself, you'll need the following Perl modules, all freely downloadable from the Comprehensive Perl Archive Network (CPAN) at http://www.cpan.org:

GD (http://search.cpan.org/author/LDS/)

An interface to the GD graphics library

Mail::Internet (http://search.cpan.org/author/MARKOV/)

For manipulating Internet mail messages

Mail::POP3Client (http://search.cpan.org/author/SDOWD/)

An interface for talking to POP3 mail servers

File:: Path (http://search.cpan.org/author/JHI/)

For manipulating directory trees

File::Spec (http://search.cpan.org/author/JHI/)

For fiddling with file names

Windows users will probably find it easiest to install these modules using PPM (http://aspn. activestate .com/ASPN/Reference/Products/ActivePerl/faq/ActivePerl-faq2.html), ActivePerl's package manager. Mac users, use the CPAN module (http://search.cpan.org/author/ANDK/CPAN-1.70/lib/CPAN.pm).

The Code

Save the following code to a file named mailrender.pl somewhere on your PC or Mac's hard drive:

#!c:\perl\bin\perl.exe use strict; use File::Path; use File::Spec; use GD; use Mail::POP3Client; use Mail::Internet; use constant DESTINATION => q{d:\DigitalPhotos\Tivo\EMail}; use constant PREVIEW_LINES => 100; use constant WIDTH => 640; use constant HEIGHT => 480; use constant HEADER_FONT => gdMediumBoldFont; use constant BODY_FONT => gdLargeFont; my @accounts = ( { DESC => q{ thoellri@foobar.com }, USER => " thoellri ", AUTH_MODE => "PASS", PASSWORD => " password ", HOST => " pop3.foobar.com " }, { DESC => q{ tobias@somewhere.com }, USER => "tobias", AUTH_MODE => "PASS", PASSWORD => " password ", HOST => " mail.somewhere.com " }, ); for my $account (@accounts) { # erase existing messages rmtree([ File::Spec->catfile(DESTINATION, $account->{DESC}) ], 0, 0); my $pop = new Mail::POP3Client (%$account); unless ($pop) { warn "Couldn't connect\n"; next; } my $count = $pop->Count; if ($count <0) { warn "Authorization failed"; next; } next if($count == 0); # no new messages # create new directory for messages mkpath([ File::Spec->catfile(DESTINATION, $account->{DESC}) ], 0, 0711); for my $num (1..$count) { my @preview=$pop->HeadAndBody($num,100); my $mail=Mail::Internet->new(\@preview); my $header=$mail->head; my $image=render($mail); my $out=File::Spec->catfile(DESTINATION, $account->{DESC},qq{message-}. sprintf("%02d",$num).qq{.png}); open(OUT, qq{>$out}); binmode OUT; print OUT $image->png; close(OUT); } $pop->Close; } sub render { my($m)=@_; my $header=$m->head( ); my $im = new GD::Image(WIDTH, HEIGHT); # allocate some colors my $white = $im->colorAllocate(255,255,255); my $black = $im->colorAllocate(0,0,0); my $gray = $im->colorAllocate(20,20,20); my $red = $im->colorAllocate(255,0,0); my $blue = $im->colorAllocate(0,0,255); my $y=2; $im->string(HEADER_FONT, 5,$y, "Date: ".$header->get('Date'), [RETURN] $black);$y+=10; $im->string(HEADER_FONT, 5,$y, "From: ".$header->get('From'), [RETURN] $black);$y+=10; $im->string(HEADER_FONT, 5,$y, "To: ".$header->get('To'), [RETURN] $black);$y+=10; $im->string(HEADER_FONT, 5,$y, "Subject: ".$header->get('Subject'), [RETURN] $blue);$y+=10; $im->string(HEADER_FONT, 5,$y, "-" x 80, $black);$y+=8; foreach my $line (@{$m->body( )}) { chomp($line); $im->string(BODY_FONT, 5, $y, $line, $gray); $y+=13; last if($y>=HEIGHT); } return $im; }

Mac OS X should alter the first line to point to the proper location of Perl:

#!/usr/bin/perl

If you don't have a copy of the Perl programming language on your Windows system, download and install a copy of ActivePerl from ActiveState (http://www.activestate.com/Products/ActivePerl/).

You'll want to set the DESTINATION variable (highlighted in bold in the previous script) to an appropriate directory into which the script should place those generated PNG images:

use constant DESTINATION => q{ d:\DigitalPhotos\Tivo\EMail };

For example, on my Mac, I might choose a space somewhere in my Pictures folder that I configured my HMO to read:

use constant DESTINATION => q{ /Users/raffi/Pictures/Hacks/Tivo/Email };

Next, configure the script for your email particulars by changing the default settings in the @accounts array (called out in bold in the previous script). For each account, set the DESC to a reasonable description of the email account, set USER and PASSWORD to your email username and password, and set HOST to the name of my POP3 mail server.

For example, here is my setup:

my @accounts = ( { DESC => q{ raffi@bitwaste.com }, USER => " raffi ", AUTH_MODE => "PASS", PASSWORD => " my_password ", HOST => " pop.bitwaste.com " }, );

Notice that I have only one account listed. To add more, simply paste in another copy of the account settingsstarting with { and ending with }and be sure to put a , between each.

You want to set the DESC to be a description of the email account, and setting it to your email address is just fine. USER and PASSWORD are self-explanatory, and HOST is the name of your POP3 server.

Running the Hack

Run the script from the DOS prompt (Start Run... command ) on your Windows PC or from the Terminal (Applications Utilities Terminal) under Mac OS X, like so:

perl mailrender.pl

The script will download (but not delete) your email and render a slew of PNG image files like the one shown in Figure 4-26.

Figure 4-26. An email message rendered as a PNG image by the mailrender.pl script

You can have mailrender.pl regularly render your email into PNG files automatically by using Scheduled Tasks on your Windows PC (Start Programs Accessories System Tools Scheduled Tasks) or cron on your Mac (see O'Reilly's Mac OS X Hacks ).

Pictures placed in your HMO-shared images directory will be snatched up by the HMO, ready to be displayed along with those family photos, as shown in Figure 4-27.

Figure 4-27. An email message in the TiVo HMO's gallery

Have fun readingor watching, to be preciseyour email.

Tobias Hoellrich


   
Top

Категории