Answer the question
In order to leave comments, you need to log in
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>';
}
--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/ ... //ЗАКОДИРОВАННЫЙ ТЕКСТ СООБЩЕНИЯ
Answer the question
In order to leave comments, you need to log in
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>';
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question