A
A
Alexey Crossov2020-12-13 13:31:43
PHP
Alexey Crossov, 2020-12-13 13:31:43

What to do with imap_body message body encoding?

Hello!

It is required to receive letters from Yandex Mail and add them to the database on your website. The problem is that the received letters are encoded and decoded and it does not work.

There is a code:

//Открываю поток IMAP к почтовому ящику
$imap = imap_open("{imap.yandex.ru:993/imap/ssl}", "login", "password");
//Получаю сообщения, удовлетворяющие заданным критериям
$mails_id = imap_search($imap, 'ON "12-Dec-2020"', OP_READONLY);
//Получаю и вывожу тело выбранных сообщений
foreach ($mails_id as $num) {
  $body = imap_body($imap, $num);
  echo '<pre>';
  print_r($body);
  echo '</pre>';
}


As a result, I get something like:
--000000000000bc6bc505b640126c
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: base64
0JAg0YHRgtC+0LjRgiDQu9C4INC80L3QtSDQt9Cw0L ... //ЗАКОДИРОВАННЫЙ ТЕКСТ СООБЩЕНИЯ

--000000000000bc6bc505b640126c
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: base64
PGRpdiBkaXI9ImF1dG8iPtCQINGB0YLQvtC40YIg0LvQuCDQvNC90LUg0LfQsNC/ ... //ЗАКОДИРОВАННЫЙ ТЕКСТ СООБЩЕНИЯ


Tried to decode with imap_base64 - returns empty. With the help of base64_decode - returns krakozyabry "�M4�M4�M4mΛs�9o�4�n� ��z{Sʗ�{�V���Z�ǭQ1|". I tried to decode them - nothing happened. Tried a variety of options with iconv - nothing happened.

The only thing I have come to so far: if you decode ONLY the code above with the comment //ENCODED MESSAGE TEXT using imap_base64 or base64_decode, then everything is OK.

If this text, along with the rest of the response (such as --000000000000bc6bc505b640126c Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: base64 0JAg0YHRgtC+0LjRgiDQu9C4INC80...), then it's empty or gibberish.

That is, in theory, I can clean the entire response from unnecessary and decode only the body of the message, but maybe there is some more universal option? Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Crossov, 2020-12-14
@iAlexCross

It turned out to be solved through imap. I don't know if this will work for everyone, but for my task OK.

$imap = imap_open("{imap.yandex.ru:993/imap/ssl}", "login", "password");

    $mails = imap_search($imap, 'ON "12-Dec-2020"', OP_READONLY); // ON "12-Dec-2020 - условие

    foreach($mails as $mail) {
        $structure = imap_fetchstructure($imap, $mail);
        if (isset($structure->parts[1])) {
            $part = $structure->parts[1];
            $message = imap_fetchbody($imap,$mail,1);
            if(strpos($message,"<html") !== false) {
                $message = trim(utf8_encode(quoted_printable_decode($message)));
            }
            else if ($part->encoding == 3) {
                $message = imap_base64($message);
            }
            else if($part->encoding == 2) {
                $message = imap_binary($message);
            }
            else if($part->encoding == 1) {
                $message = imap_8bit($message);
            }
            else {
                $message = trim(utf8_encode(quoted_printable_decode(imap_qprint($message))));
            }
        }
        echo '<pre>';
        print_r($message);
        echo '</pre>';
    }

F
FanatPHP, 2020-12-13
@FanatPHP

Smoking multipart/form-data
Well, it immediately smokes https://github.com/php-mime-mail-parser/php-mime-m...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question