Forum Moderators: phranque

Message Too Old, No Replies

Procmail Extract Text from Body of Email

         

Frank_Rizzo

9:01 pm on Jun 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First up: Why is there no support for procmail? It's been around for ages but there is little support. There are zillions of sites duplicating the man pages, and about half a dozen really geeky sites which are not that helpful.

Anyone here good with recipes?

Here's what I'm trying to do:

Extract parts of text from the body of an email and send a new mail with the extracted data in the subject.

********* incoming message
from: joe@widgets.com
to: frank@rizzowidgets.com
subject: Order despatched

Order no : 5551234
Stock quantity : 10
Description : Blue widgets with chrome pipes.
Customer : Frank Rizzo
Shipped to : 1 Evergreen Terrace, Springfield 12345 MA
**********

When this email is received I want to extract

5551234
10
Frank Rizzo

and send a new mail to

********* outgoing message
from: frank@rizzowidgets.com
to: despatch@rizzowidgets.com
subject: 5551234 10 Frank Rizzo
**********

Here's my meagre attempt:

[code]
:0
* ^Subject:.*Order\ despatched
{
:0c:
${DEFAULT}
:0
* ^Order No :*
ORDER_NO = $MATCH
:0
* ORDER_NO? [ ]*\/[^ ].*
{ ORDER_NO = $MATCH }
:0:proc.lock
¦ formail -X "" \
-I"Subject: ${ORDER_NO}" \
-i"Content-Type:" \
-i"Content-Length:" \
-I"To: despatch@rizzowidgets.com" \
¦ ${SENDMAIL} -t
}
[code]

Any ideas?

trillianjedi

4:01 pm on Jun 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



*bump*

Anyone?

Frank_Rizzo

9:10 am on Jun 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a solution for this now.

It was suggested that it would be easier to pipe out to php and run the parsing script in that.

It certainly was easier.

Here's the recipe:


:0
* ^Subject:.*Order\ despatched
{
:0c:
${DEFAULT}
:0
¦/usr/bin/php -q /home/widgets/scripts/parse_email.php }

In the parse_email.php script I just read from stdin and parse each line


<?php

$email = $order_no = $qty = $name = "";

$fd = fopen("php://stdin", "r");

while(!feof($fd)) {
$email .= fread($fd, 1024);
}

fclose($fd);

$lines = explode("\n", $email);

// check lines for matches here