O
O
oldTV2020-05-06 11:20:30
gmail
oldTV, 2020-05-06 11:20:30

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

1 answer(s)
P
PQR, 2020-05-06
@PQR

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);

6. Using the same library for working with mail (from step 1), we send a letter with a new text further to the target mailbox. Please note that the sender (the from field) will change, so when sending, I recommend adding information about the original author of the letter to the "reply to" field in the body of the new letter ($newBody) so that the reader knows who to reply to later.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question