O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  



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
The Code
[Discuss (1) | Link to this hack]

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'),↵
  $black);$y+=10;
  $im->string(HEADER_FONT, 5,$y, "From:  ".$header->get('From'),↵
  $black);$y+=10;
  $im->string(HEADER_FONT, 5,$y, "To:   ".$header->get('To'),↵
  $black);$y+=10;
  $im->string(HEADER_FONT, 5,$y, "Subject: ".$header->get('Subject'),↵
  $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


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.