A
A
Alexey2016-01-15 17:57:10
PHP
Alexey, 2016-01-15 17:57:10

Parsing letters about undelivered mail. Remove email and error codes?

The site often sends letters, but not all users receive letters. In order not to send letters to those whose mail no longer exists, I write the processing of undelivered mail.
Sending via SMTP, checking a dedicated mailbox with returned mail via IMAP.
Most servers insert the X-Failed-Recipients field into the message header: хххх@yandex.ru, but the php functions imap_headerinfo and similar cannot get this field.
The body of the letter also has a different format, so taking out EMail and the error code is not always possible.
Can anyone solve this in PHP? All found solutions are either in Perl, or mailer modules in C.
PS Most often, a letter of the format is returned

This message was created automatically by mail delivery software.

A message that you sent could not be delivered to one or more of its
recipients. This is a permanent error. The following address(es) failed:

  *****@balrt.ru
    SMTP error from remote mail server after RCPT TO:<******@balrt.ru>:
    host ns7.balrt.ru [88.147.158.86]: 550 5.1.1 User unknown

But the text is different and it is not possible to write a regular expression to extract the necessary information.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Dubrovin, 2016-01-16
@alex-saratov

According to the text of the letter, you will not always understand with which user there is a problem and there is no universal format. Instead, send each user a letter with a unique return address in an SMTP envelope like [email protected], process all letters sent to *@mailing.youdomain and identify the problem mailbox by the address they came to. If for some time more than a certain number of thrashings came - mark as inactive. Auto-replies can be distinguished from slaps by the Auto-Submitted: auto-replied header.

N
Nikita Tratorov, 2017-09-19
@NikitaTratorov

Use https://github.com/php-mime-mail-parser/php-mime-m...
and search for machine-readable headers will help you.
In the array of parts (multipart message), the 0th element is the general headers, the 1st is the human-readable text that you provided in the text, and the 2nd will be the machine-readable array that does not need to be parsed with regexps.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question