Forum Moderators: phranque
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?
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