There is no way to grep things directly out of the message body for further processing.
Sieve does not populate variables like ${1}
from body matches.
The RFC states clearly that this MUST NOT be possible.
There is however a possibility to solve this issue by feeding (filtering) the message threw a seperate application like as follows wich puts the desired informations into the header.
Imagine the original message was:
To: my@second.tld
Subject: Test
From: other@example.tld
Date: Wed, 25 Oct 2017 16:22:05 +0200
Hi guy, here starts the body
This mail contains a important dynamic address
From: important@match.tld
wich has to be matched und processed by sieve
Then Your sieve could look like this:
require ["copy","variables","vnd.dovecot.filter"];
if header :contains "Subject" "Returned mail" {
filter "bleed_from.py";
if header :matches "Bleeded-From" "*" {
redirect :copy "${1}";
}
}
The filter-script "bleed_from.py":
#!/usr/bin/python
import re
import email
# Read the mail from stdin
parser = email.FeedParser.FeedParser()
mail = None
for line in sys.stdin.readlines():
parser.feed(line)
mail = parser.close()
# Grep the From out of the body and add it to the header
ret = re.findall("From: (.*)", mail.get_payload())
mail.add_header("Bleeded-From", ret[0])
# Return the message
print(mail.as_string())
This is a very over simplyfied proove of concept and works only for non multipart messages without special characters. Otherwise this application will crash. Dealing with charsets would blow the margins of this example.