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  

Listen to Your Email
Listen to your email as an MP3 audio stream—just because you can
The Code
[Discuss (1) | Link to this hack]

The Code

Save the following code to a file named mail2mp3.pl somewhere on your PC hard drive, and follow the configuration instructions from :


use strict;
use Cwd;
use File::Copy;
use File::Path;
use File::Spec;
use Mail::POP3Client;
use Mail::Internet;
use LWP::Simple;
use Win32;
use Win32::OLE;
use Win32::OLE::Const qq{Microsoft Speech Object Library};

use constant DESTINATION  => q{D:\MP3\TiVo};
use constant PREVIEW_LINES => 30;

my @accounts = (
 DESC   => q{thoellri@foobar.com},
 USER   => "thoellri",
 PASSWORD => "password",
 HOST   => "pop3.foobar.com"
 {DESC   => q{tobias@somewhere.com},
 USER   => "tobias",
 PASSWORD => "password",
 HOST   => "mail.somewhere.com"

for my $account (@accounts) {
 # erase existing messages
  rmtree([ File::Spec->catfile(DESTINATION, qq{Email}, $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, qq{Email}, $account->{DESC}) ],↵
    0, 0711);
  for my $num (1..$count) {
    my @preview=$pop->HeadAndBody($num,100);
    my $mail=Mail::Internet->new(\@preview);
    my $mp3file=mail2mp3($mail);
    next unless defined($mp3file);
    my $out=File::Spec->catfile(DESTINATION, qq{Email}, $account->{DESC},

sub mail2mp3 {
  my $header=$m->head(  );
  my $type=Win32::OLE->new('SAPI.SpAudioFormat');
  my $stream=Win32::OLE->new('SAPI.SpFileStream');
  my $speech=Win32::OLE->new('SAPI.SpVoice');
  $speech->Speak(qq{From: }.$header->get('From'),SVSFDefault);
  $speech->Speak(qq{Subject: }.$header->get('Subject'),SVSFDefault);
  foreach my $line (@{$m->body(  )}) {
    $speech->Speak($line." ",SVSFDefault);
    last if($lines++>= PREVIEW_LINES);
  $stream->Close(  );
  return undef unless (-f "output$$.wav");
 # here we call out to the command line mp3 encode
  system(qq{bladeenc -quiet -nocfg -quit output$$.wav output$$.mp3});
  return undef unless (-f "output$$.mp3");
  return File::Spec->catfile(getcwd,qq{output$$.mp3});

See also:

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.