Solved

POP3 email, parse message body

Posted on 2004-08-08
5
270 Views
Last Modified: 2012-05-05
I need to analyse a messagebody in this form:

<Start of Bodytext>
SpamContent       123
LocalDeliver     456
RemoteDeliver    789

---
text following, not important
<End of Bodytext>

I need the first 3 lines to put into an array, based on the name, it's possible this gets more than three lines with other data.
After that i wan't to put this data along with the date of the email into a mySQL database (sits on the same linux box), how can I do this..?
Final result would be a webpage (PHP) with an overview of this data, but is no problemo.

So:
Each line into an array, based on line description, data ends with spaced line and next '---' line
Extra array zone with date of the email
Put this in a mySQL database (add record with this data), based on the date of the email (so if date already exists iverwrite record otherwise create new record.

Thanks
0
Comment
Question by:driesmans
  • 2
  • 2
5 Comments
 
LVL 6

Expert Comment

by:ren_b
ID: 11748834
> I need the first 3 lines to put into an array, based on the name, it's possible this gets more than three lines with other data.

is it possible that the 3 lines will be out of a set order? also, what is your sql query for putting it into mysql?
0
 

Author Comment

by:driesmans
ID: 11770190
Those data lines will always be in the same order, because the mail is automatically generated.

The SQL query will be an insert or update query . Didn't start on that one yet.
0
 
LVL 6

Accepted Solution

by:
ren_b earned 125 total points
ID: 11774761
ok, well this should be enough to get you going...

#!/usr/bin/perl
$readdata = 0;
while(<>){
  chomp;
  $readdata = 0 if $_ eq '';
  $data[++$#data] = (split(/\s+/,$_,2))[1] if $readdata;
  $readdata = 1 if $_ eq "<Start of Bodytext>";
  $date = $1 if $_ =~ /^date: (.*?)$/i;
}
$" = ", ";
$sqlquery = "insert into some_table values ( \"$date\", @data )";
print "$sqlquery\n";

if they'll always be in the same order, then they'll be read into the @data array as they come, and print them into the sql query. this should give you some ideas.
0
 

Author Comment

by:driesmans
ID: 11966060
Thanks Ren_b, with a few adjustments it did the job...

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now