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

Hi Fellow monks

I was abruptly referred to How (Not) To Ask A Question. last week, deservedly so.

Thank You.

After reading the document I appreciate much more of what is going on in the forum. I'll do my best in staying with the rules.

First of all I'm working on windows. I was given the task of retrieving a email from a specific email address and saving as a file in any outlook compattible format, for example as a (.msg) or (.eml) file. I can download the email and save it as a text file. The attachments or refferred to in the MIME tools as 'slang for any part of a multipart message' is still encoded.

I'm a bit clueless on the usage of the MIME tools although I've read through most of the MIME documentation. I'm not to sure if it is just lack in experience or a lack of undestanding of the english language. (but all excuses aside my code will explain better).

I'm also not sure if MIME is the way to go to get the whole message in a outlook compattible format. If you could please advise.

#!c:\perl\bin use Net::POP3; use MIME::Parser ; my $parser = new MIME::Parser; my $pophost = myhost'; # Name or IP address of POP3 server my $popuser = myusr; # Account name on POP3 server my $poppass = mypass; # Password for account my ($item,$message,$line); my $pop = Net::POP3->new($pophost) or die "$!\n"; my $message_list = $pop->list; if ($pop->login($popuser, $poppass) > 0) { my $msgnums = $pop->list; # hashref of msgnum => size foreach my $msgnum (keys %$msgnums) { my $msg = $pop->get($msgnum); ##Comment 1 #$fname = $msgnum.".txt"; #open(MAIL,">$fname"); #print MAIL @$msg; #close MAIL; ##Comment2 #$parser->output_dir("/tmp"); #$parser->output_prefix("msg"); #$parser->output_to_core(1); my $entity = $parser->parse(@$msg); ##Comment3 #$pop->delete($msgnum); } } $pop->quit;

Comment1: you will see at this point I am able to receive the emails and with the commented code will see how I saved the file as a text file

Comment2: here you will see I played with some of the MIME functions but do not have any clue of the whole structure, how the functions work together. The un commented piece creates a file but does not put anything into the file

Comment3: I'm not deleting the email so I do not to have to send it the whole time

Please if you could point me in the right direction

Replies are listed 'Best First'.
Re: downloading email saving as outlook compattible file WIN32
by roboticus (Chancellor) on May 29, 2006 at 13:37 UTC
    PugSA:

    If I understand you correctly, you just want to retrieve the EMail and store it in a format that Outlook can read. If that's the case, you don't need the MIME tools. If you save the body with the 'txt' extension, Outlook doesn't bother to decode the MIME stuff inside. If you save the body of the EMail with the appropriate extension (.eml), I believe that Outlook will be able to read it and decipher the MIME content properly.

    If, on the other hand, you want to manipulate the contents, then you'll definitely want to use the MIME tools on CPAN. I've never used them to disassemble an EMail message, but I used them to create FAXes. (We have a FAX server that will take RTF or HTML (among other formats) MIME-encoded EMails and fax them to the specified addressee.

    I'm no MIME expert, but I tend to think of MIME-encoded EMails as a manilla folder, with simple text on the front. That text is the text-only part of the EMail, including the headers. Then, inside the folder is a collection of named pages, typically (but not always) encoded. The top line of the page tells you the name of the page, as well as a description of what you'll see when it's decoded, and the encoding scheme used.

    It's not too difficult to figure out--but I found the documentation a bit obtuse, so I experimented a lot by taking apart some EMails to see what was in them, and manipulating them in an editor.

    One hint: Outlook tries to be too clever, so in most cases, it makes it impossible to get the unmodified body of the EMail received. So you might want to consider a different EMail client for your experiments. (I used Netscape's EMail client, which doesn't change your EMail contents when saving them to a text file for you.) Once you get to the point where you can take the EMails apart and put 'em back together, then you can switch back to Outlook to verify your work.

    If you're interested, /msg me and tomorrow when I get back to work, I'll post my fax code to the Snippets section. Of course, it's me *creating* an EMail to be sent, and not taking one apart, so you may not find it interesting...

    --roboticus

Re: downloading email saving as outlook compattible file WIN32
by jdtoronto (Prior) on May 29, 2006 at 13:59 UTC
    PugSA

    On Lincoln Stein's book "Network Programming with Perl" he gives code for a simple module called PopParser.pm

    package PopParser; use strict; use Net::POP3; use MIME::Parser; use vars '@ISA'; @ISA = qw(Net::POP3); # over-ride Net::POP3 new() method sub new { my $pack = shift; return unless my $self = $pack->SUPER::new(@_); my $parser = MIME::Parser->new(); $parser->output_dir( $ENV{TEMP} || '/tmp'); $self->parser($parser); $self; } # accessor for parser sub parser { my $self = shift; ${*$self}{'pp_parser'} = shift if @_; return ${*$self}{'pp_parser'} } # over-ride get() sub get { my $self = shift; my $msgnum = shift; my $fh = $self->getfh($msgnum) or die "Can't get message: ",$self->m +essage,"\n"; return $self->parser->parse($fh); } 1;
    which I have found very useful along with the example of its use given in his pop_fetch.pl programme ( see p210 or it can be found here but you need the book to get the commentary!). One of the beauties of his code is that he explains everything on a near line by line basis.

    As for Outlook? Sorry, I can't help, although we work in Windows predominantly - Outlook is outlawed here.

    jdtoronto

Re: downloading email saving as outlook compattible file WIN32
by roboticus (Chancellor) on May 31, 2006 at 16:17 UTC
    PugSA:

    I dug up the code, and noticed that it's a bit too trivial to put in the Snippets section, so I thought I'd post it here as another reply to your original post...

    #!/usr/bin/perl #--------------------------------------------------------------------- # faxer.pl <recipient> <file> # # Send file <file> to <recipient>. Note that the recipient field is # a specially formatted field that contains recipient name, fax, # and email address of the fax server, like so: # # "/name=Lonnie/fax=2136998/<rfax@faxserver.domain.com>" # # 20050111 MCMason - Created from script LPhillips gave me #--------------------------------------------------------------------- use MIME::Lite; $fax_dest = @ARGV[0]; $fax_file = @ARGV[1]; $msg = MIME::Lite->new( From => 'Roboticus <roboticus@domain.com>', To => "$fax_dest", Subject => 'Aging Report', Type => 'TEXT', Data => '<FROMPHONE:(XXX)315-2177>' .'<CHANNEL:1><FINE><PRIORITY:LOW>' ) or die "Error creating MIME body: $!\n"; $msg->attach( Type => 'text/plain', Path => $fax_file, Filename => 'Aging Report' ) or die "Error attaching file: $!\n"; $msg->send;
    --roboticus