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

Hello Monks, I am parsing emails with my script just fine. However I need to output some of the headers to an xml document. I will give you the example XML and my pop script below. I need to write the to, from, cc, and subject fields into the xmlk format you see below. what is the best way to do this and can you give an example Thanks
- <Index> - <Fields> - <Field> <Name>From</Name> <Value>"Sam man" <sam.man@sam.com></Value> </Field> - <Field> <Name>To</Name> <Value><network@etp.us.blackberry.net></Value> </Field> - <Field> <Name>Date</Name> <Value>01/02/2005</Value> </Field> - <Field> <Name>Subject</Name> <Value>RIM_bca28a80-e9c0-11d1-87fe-00600811c6a2</Value> </Field> - <Field> <Name>CC</Name> <Value /> </Fields> </Index>

Here is the script

#CaptureMail Script use Net::POP3; use Mail::Header; # Configure Sever Login Info here # $postoffice='pop.mail.yahoo.com'; # fill in this with something like p +ostoffice.isp.com $user=''; # fill this in with your mail user name $password=''; # fill this in with your mail server password $verbose=1; # Call scan_header with the Mail::Header object. If it returns a value +, then # that value is the reason the mail should be Captured. # If it returns undef, the mail is not Captured; # # NOTE: Edit the Email addresses below to specify who's mail to Captur +e' sub scan_header { my $head, @rec, $r, @tags, $t, $msgsize; ($head, $msgsize) = @_; @tags = $head->tags(); $DefinedUser = 0; foreach $t (@tags) { if ($t=~/^X-Advertisement/i) { return "Found X-Advertisement header"; } if ($DefinedUser == 0) { if (($t=~/From/i) || ($t=~/To/i) || ($t=~/Cc/i)) { @rec = $head->get($t); foreach $r (@rec) { if ($r=~/extra\@ediets\.com/i) { return "Found User in $t header"; } if ($r=~/savetrees\.com/i) { return "Found User in $t header"; } if ($r=~/earthlinking\.net/i) { return "Found User in $t header"; } if ($r=~/\@shoppingplanet\.com/i) { return "Found User in $t header"; $DefinedUser = 1; } } } } } if ($DefinedUser == 1) { return "Processing First Header Pass"; } return undef; } # Call Get_First_Header_Pass with postoffice, user, password to be sca +nned. # sub Get_First_Header_Pass { my $postoffice, $user, $password, $pop, $msgcount, $i, $head, $reas +on, $subj, $from, $CaptureCount; ($postoffice, $user, $password) = @_; $pop = Net::POP3->new($postoffice) ; if (! defined($pop)) { die "Net::POP3::new failed for postoffice $postoffice\n"; } $msgcount = $pop->login($user, $password); if (! defined($msgcount)) { die "Cannot login to mailbox at $postoffice\n"; } ($msgcount, $msgsize) = $pop->popstat(); $CaptureCount = 0; for ($i = 1; $i <= $msgcount; ++$i) { $msgsize = $pop->list($i); $head = new Mail::Header $pop->top($i, 0); if ($reason = &scan_header($head, $msgsize)) { if ($verbose) { $subj = $head->get('Subject'); $from = $head->get('From'); if (! defined($subj)) { $subj = "<no subject>\n"; } if (! defined($from)) { $from = "<no from address>\n"; } print "Capturing mail:\n"; print " From: $from"; print " Subject: $subj"; print " Reason: $reason\n"; } #$pop->delete($i); ++$CaptureCount; } } $pop->quit(); if ($verbose && ($msgcount > 0)) { print "Looked at $msgcount message(s), Captured $CaptureCount\n" +; } } # Actually do everything. # &Get_First_Header_Pass($postoffice, $user, $password);

Replies are listed 'Best First'.
Re: parse email output to a formatted XML document
by holli (Abbot) on Feb 14, 2005 at 21:51 UTC
    /me suggests using print;


    holli, /regexed monk/