Operating System - HP-UX
1820594 Members
1344 Online
109626 Solutions
New Discussion юеВ

Help to extract lines from email file in Perl

 
SOLVED
Go to solution
Paul D. Simpson
Frequent Advisor

Help to extract lines from email file in Perl

Hi all,
Thanks in advance for the help.

I have a Perl script that is grabbing headers from email messages & logging them to a file... Unfortunately, my sender switched the mechanism he uses to send these emails & now there seems to be a newline on some of the data I am trying to grab (Subject is the main culprit). I'd like to make my script grab everything from "Subject:" up to right before "To:", and "To:" up to just before "Cc:", "Cc:" up to just before "Message-ID"

Can anyone help please?

My script contains:
#!/opt/perl/bin/perl -w

while ($line=) {
my @now = localtime();
my $timestamp = sprintf("%04d-%02d-%02d",$now[5]+1900, $now[4]+1, $now[3]);
my $OUTPUT = "/home/share/acks/order_acks_$timestamp.txt";
open (OUTFIL, ">>$OUTPUT") || die "Cannot open $OUTPUT file";
if ($line =~ /^To:/){
chomp $line;
print OUTFIL "$line \n";
}
if ($line =~ /^[Cc]c:/){
chomp $line;
print OUTFIL "$line \n";
}
if ($line =~ /^Subject:/){
chomp $line;
print OUTFIL "$line \n\n";
}
}
close OUTFIL;
exit;
(Yes, I'm new to Perl)


The file I'm grabbing from looks like:
( I added ###<---->###### tages to highlight what I need)

From Sales.Order.Ack.Exceptions@company.com Wed Jun 10 15:56:21 2009
Return-Path:
Received: from smtp02.company.com (chn-sv-exft02.microchip.com [99.99.99.18])
by newton.company.com (8.9.3 (PHNE_35484)/8.9.3) with ESMTP id PAA03316;
Wed, 10 Jun 2009 15:56:21 -0700 (MST)
Received: from CHN-VM-STRMSRV.company.com ([99.99.99.126]) by smtp02.company.com with Microsoft SMTPSVC(6.0.3790.1830);
Wed, 10 Jun 2009 15:56:20 -0700
Date: Wed, 10 Jun 2009 15:56:20 -0700 (PST)
From: Your Order Acknowledgement
Reply-To: Sales.Order.Ack.Exceptions@company.com
####<-- SUBJECT GRAB START HERE-->#####
Subject: =?ISO-8859-1?Q?Customer:_110557,_Customer_PO:_123456789,_Order:_132456(?=
=?ISO-8859-1?Q?Changed_lines_41)__06-10?=
####<-- SUBJECT GRAB END HERE-->#####
####<-- TO GRAB START HERE-->#####
To: customer456@yahoo.com
####<-- TO GRAB END HERE-->#####
####<-- CC GRAB START HERE-->#####
####<-- CC GRAB END HERE-->#####
Message-ID: <7081244674580691@company.com>
MIME-Version: 1.0
Content-Type: MULTIPART/MIXED; BOUNDARY="6978513-22148-1244674580=:708"
X-OriginalArrivalTime: 10 Jun 2009 22:56:20.0044 (UTC) FILETIME=[AB49F8C0:01C9EA1E]

--6978513-22148-1244674580=:708
Content-Type: TEXT/PLAIN; CHARSET=ISO-8859-1
Content-Transfer-Encoding: QUOTED-PRINTABLE

We thank you for your order. An Order Acknowledgement is attached.=0A=0AIf=
you need further information...
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor
Solution

Re: Help to extract lines from email file in Perl

Hi Paul:

If the lines are truly multiple ones, we could do something like this:

# cat ./mymail
#!/usr/bin/perl
use strict;
use warnings;
my $line;
my @now = localtime();
my $timestamp
= sprintf( "%04d-%02d-%02d", $now[5] + 1900, $now[4] + 1, $now[3] );
my $OUTPUT = "home/share/acks/order_acks_$timestamp.txt";

#->open (OUTFIL, ">>$OUTPUT") || die "Cannot open $OUTPUT file";
{
local $/ = undef;

while ( $line = <> ) {
chomp $line;
if ( $line =~ /( ^To:.*? )( [Cc]c:|Message-ID )/xms ) {
print $1, "\n";
}
if ( $line =~ /( ^[Cc]c:.*? )( Message-ID )/xms ) {
print "$line \n";
}
if ( $line =~ /( ^Subject:.*? )( To: )/xms ) {
print $1, "\n";
}
}
}
close OUTFIL;
exit;

...

We slurp the whole file into one big record. Then we capture the pieces we want by matching the across any newline characters with '/s' regex modifier to allow '.' to match a newline.

Regards!

...JRF...
Paul D. Simpson
Frequent Advisor

Re: Help to extract lines from email file in Perl

Thanks James,

It is definitely sending the correct output to the screen, but does not seem to be writing to the output file.

FYI: This is being used as an alias redirect for sendmail, such that incoming mail for address "ack" is piped to this script. Not sure if that makes a difference.

Paul
James R. Ferguson
Acclaimed Contributor

Re: Help to extract lines from email file in Perl

Hi:

> It is definitely sending the correct output to the screen, but does not seem to be writing to the output file.

Yes, that's because for ease of testing I dropped your output filehandle leaving STDOUT as the default, selected one.

Instead of:

# print $1, "\n";

change to:

# print OUTFIL $1, "\n";

(or):

# print OUTFIL "$1\n";

Regards!

...JRF...




Paul D. Simpson
Frequent Advisor

Re: Help to extract lines from email file in Perl

James,
Thanks... I should have caught that... I had the same problem when I first wrote the script.
Thanks again
-Paul
Paul D. Simpson
Frequent Advisor

Re: Help to extract lines from email file in Perl

James nailed it