Answer the question
In order to leave comments, you need to log in
How to replace text in received email using regexp?
The information from the client in the e-mail contains several "garbage" text inserts added by various client mail programs or their CPMs. I would like to bring the letter to a more readable form and remove this data. It is known that they come from a specific address, i.e. you can create a filter on it.
But how to process it and send it to another address?
Answer the question
In order to leave comments, you need to log in
This task can be solved with a PHP script.
I won’t write all the code, but the algorithm is as follows:
1. Connect to the mailbox using one of the IMAP libraries: https://packagist.org/?query=imap (for example, php-imap/php-imap)
2 We read all letters from the inbox folder
3. For each letter we check the sender - if this is the same sender, we go further
4. We receive the text of the message as a string in a variable, let's say $body
5. We need to prepare in advance examples of "garbage text" that we want to cut out. For example, let them be written in PHP code as an array in the TO_BE_DETELED_TEXTS constant. We
remove these garbage fragments from the text of the letter:
$newBody =
str_replace(self::TO_BE_DETELED_TEXTS, '', $body);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question