+7(960) 250-82-68 info@mirossa.ru


 mirossa        1С           C         PHP       JAVA  


Скрипты
 
 

imap. Запрос-ответ. (скрипт внизу)

Авторизуемся на сервере imap. выбираем для работы ящик "INBOX". Ищем непросмотренные письма с датой больше 11 февраля 2019 года. Получаем номера непрочитанных писем. Запрашиваем заголовочную часть письма с номером 15. Запрашиваем тело письма с номером 15.


Connecting to imap.majordomo.ru...
(answer).
* OK IMAP4 ready
(request) a1 LOGIN info@mirossa.ru password

(answer).
a1 OK [CAPABILITY IMAP4rev1 LITERAL+ SASL-IR LOGIN-REFERRALS ID ENABLE IDLE SORT SORT=DISPLAY THREAD=REFERENCES THREAD=REFS THREAD=ORDEREDSUBJECT MULTIAPPEND URL-PARTIAL CATENATE UNSELECT CHILDREN NAMESPACE UIDPLUS LIST-EXTENDED I18NLEVEL=1 CONDSTORE QRESYNC ESEARCH ESORT SEARCHRES WITHIN CONTEXT=SEARCH LIST-STATUS BINARY MOVE] Logged in


(request) a2 EXAMINE INBOX

(answer).
* FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
* OK [PERMANENTFLAGS ()] Read-only mailbox.
* 46 EXISTS
* 23 RECENT
* OK [UNSEEN 15] First unseen.
* OK [UIDVALIDITY 1455186964] UIDs valid
* OK [UIDNEXT 1272] Predicted next UID
* OK [HIGHESTMODSEQ 664] Highest
a2 OK [READ-ONLY] Examine completed (0.000 + 0.000 secs).


(request) a3 SEARCH UNSEEN SINCE 11-Feb-2019

(answer).
* SEARCH 15 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
a3 OK Search completed (0.001 + 0.000 secs).


(request) a4 FETCH 15 ENVELOPE

(answer).
* 15 FETCH (ENVELOPE ("Sun, 17 Feb 2019 17:22:30 +0300" "=?utf-8?B?0JDQutGC0YPQsNC70YzQvdCwINC70Lgg0LXRidC1INCy0LDRiNCwINCy0LDQutCw0L3RgdC40Y8g0LHRg9GF0LPQsNC70YLQtdGA0LA=?=" (("=?utf-8?B?0JXQutCw0YLQtdGA0LjQvdCwICjQsdGD0YUuINGD0YHQu9GD0LPQuCk=?=" NIL "opposite_153.907" "eveready-pharmacopoeia.tk")) (("=?utf-8?B?0JXQutCw0YLQtdGA0LjQvdCwICjQsdGD0YUuINGD0YHQu9GD0LPQuCk=?=" NIL "opposite_153.907" "eveready-pharmacopoeia.tk")) (("=?utf-8?B?0JXQutCw0YLQtdGA0LjQvdCwICjQsdGD0YUuINGD0YHQu9GD0LPQuCk=?=" NIL "opposite_153.907" "eveready-pharmacopoeia.tk")) ((NIL NIL "info" "mirossa.ru")) NIL NIL NIL "<1550413350.d1b9aa8b33a286e1f67ef4d7d9ecbc1c@mailsys>"))
a4 OK Fetch completed (0.001 + 0.000 secs).


1. Parsing:
Sun, 17 Feb 2019 17:22:30 +0300
"=?utf-8?B?0JDQutGC0YPQsNC70YzQvdCwINC70Lgg0LXRidC1INCy0LDRiNCwINCy0LDQutCw0L3RgdC40Y8g0LHRg9GF0LPQsNC70YLQtdGA0LA=?="
(("=?utf-8?B?0JXQutCw0YLQtdGA0LjQvdCwICjQsdGD0YUuINGD0YHQu9GD0LPQuCk=?="
opposite_153.907@eveready-pharmacopoeia.tk


2. Parsing:
Sun, 17 Feb 2019 17:22:30 +0300
Актуальна ли еще ваша вакансия бухгалтера
opposite_153.907@eveready-pharmacopoeia.tk (Екатерина (бух. услуги))


(request) a5 FETCH 15 BODY[TEXT]

(answer).
* 15 FETCH (BODY[TEXT] {2285}
----ae73ff89d57304fa7a519fb31b496c97
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: base64
0JfQtNGA0LDQstGB0YLQstGD0LnRgtC1IQoK0JzQtdC90Y8g0LfQvtCy0YPRgiDQldC60LDRgtC1
0YDQuNC90LAsINGPINCx0YPRhdCz0LDQu9GC0LXRgCDRgSDQsdC+0LvRjNGI0LjQvCDQvtC/0YvR
gtC+0Lwg0YDQsNCx0L7RgtGLLgoK0J/QvtC20LDQu9GD0LnRgdGC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CDQktGLINGA0LDRgdGB0LzQsNGC0YDQuNCy
0LDRgtGMINC80L7RjiDQutCw0L3QtNC40LTQsNGC0YPRgNGDINC60LDQuiAi0L/RgNC40YXQvtC0
0Y/RidC10LPQviIg0LHRg9GF0LPQsNC70YLQtdGA0LA/INCV0YHQu9C4INC00LAgLSDRgdC+0L7Q
sdGJ0LjRgtC1INC60LDQutC+0Lkg0L/RgNCw0LLQvtCy0L7QuSDRgdGC0LDRgtGD0YEg0L7RgNCz
0LDQvdC40LfQsNGG0LjQuCwg0LrQsNC60LDRjyDRgyDQktCw0YEg0YHQuNGB0YLQtdC80LAg0L3Q
sNC70L7Qs9C+0L7QsdC70L7QttC10L3QuNGPLCDQv9GA0LjQvNC10YDQvdC+0LUg0LrQvtC7LdCy
0L4g0L7Qv9C10YDQsNGG0LjQuSDQuCDQv9C10YDRgdC+0L3QsNC7LgrQryDRgdC80L7Qs9GDINGB
0LrQsNC30LDRgtGMINGB0YLQvtC40LzQvtGB0YLRjCDRg9GB0LvRg9CzINC60L7QvdC60YDQtdGC
0L3QviDQsiDQktCw0YjQtdC8INGB0LvRg9GH0LDQtS4KCtChINGD0LLQsNC20LXQvdC40LXQvCwg
0JXQutCw0YLQtdGA0LjQvdCwINCR0L7RgNC40YHQvtCy0LAu

----ae73ff89d57304fa7a519fb31b496c97
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: base64
)
a5 OK Fetch completed (0.001 + 0.000 secs).


(request) a9 logout

(answer).
* BYE Logging out
a9 OK Logout completed.

imap. скрипт

<?php
// function get_data_pop($pop_conn)
// {
// $data="";
// while (!feof($pop_conn)) {
// $buffer = chop(fgets($pop_conn,1024));
// $data .= "$buffer\r\n";
// if(trim($buffer) == ".") break;
// }
// return $data;
// }
//отправляем ключ и когда этот ключ вернулся от сервера, значит это последняя ответная строка.
function get_data_imap($imap_conn, $key)
{
$data="";
while (!feof($imap_conn)) {
$buffer = chop(fgets($imap_conn,1024));
$data .= "$buffer&lt;br/>"; //$data .= "$buffer\r\n";
if(0 == strncmp($buffer, $key, 2)) break;
}
return $data;
}

/////////////// IMAP ///////////////////////////////
// ftp://ftp.rfc-editor.org/in-notes/rfc2180.txt
// http://oophpy.blogspot.com/2015/02/read-email-using-imap-fsockopen.html
// http://opds.sut.ru/old/electronic_manuals/mail/5_IMAP.htm#5_4_10
// https://fastmail.blog/2016/07/13/whats-in-a-name-mailbox-names-via-imap/
//echo '&lt;br/>&lt;br/>';
print "Connecting to imap.majordomo.ru... &lt;br/>";
print "(answer).&lt;br/>";
$imap_conn = fsockopen("ssl://imap.majordomo.ru", 993, $errno, $errstr, 10);
if ($imap_conn == false){
echo $errno.':'.$errstr.'&lt;br/>';
return;
}
$code=fgets($imap_conn,1024);
echo $code."&lt;br/>";
$key = "a1";
$comand = "$key LOGIN info@mirossa.ru password";
print "(request) ".$comand."&lt;br/>&lt;br/>";
fputs($imap_conn,$comand."\r\n");
print "(answer).&lt;br/>";
print get_data_imap($imap_conn, $key)."&lt;br/>&lt;br/>&lt;br/>";
// fputs($imap_conn,"a2 STATUS INBOX (MESSAGES RECENT UIDNEXT)\r\n"); // INBOX - это как бы имя почтового ящика.
// $code=fgets($imap_conn,1024);
// echo $code."&lt;br/>";
// $code=fgets($imap_conn,1024);
// echo $code."&lt;br/>&lt;br/>&lt;br/>";
// пример ответа
// * STATUS INBOX (MESSAGES 7 RECENT 0 UIDNEXT 1222)
// a2 OK Status completed (0.000 + 0.000 secs).
// выбор конкретного ящика
$key = "a2";
$comand = "$key EXAMINE INBOX";
print "(request) ".$comand."&lt;br/>&lt;br/>";
fputs($imap_conn,"$key EXAMINE INBOX\r\n"); // INBOX - это как бы имя почтового ящика.
print "(answer).&lt;br/>";
print get_data_imap($imap_conn, $key)."&lt;br/>&lt;br/>&lt;br/>";
$key = "a3";
$comand = "$key SEARCH UNSEEN SINCE 11-Feb-2019";
print "(request) ".$comand."&lt;br/>&lt;br/>";
fputs($imap_conn,"$key SEARCH UNSEEN SINCE 11-Feb-2019\r\n"); // если удалить несколько сообщений, то индекс остальных сдвигается
print "(answer).&lt;br/>";
print get_data_imap($imap_conn, $key)."&lt;br/>&lt;br/>&lt;br/>";
// MIME 'B' and 'Q' header encoding https://www.mkssoftware.com/docs/perl/lib/Encode/MIME/Header.asp
// https://tools.ietf.org/html/rfc2047
// https://tools.ietf.org/html/rfc5321 https://tools.ietf.org/html/rfc5322
$key = "a4";
$comand = "$key FETCH 15 ENVELOPE";
print "(request) ".$comand."&lt;br/>&lt;br/>";
fputs($imap_conn,"$key FETCH 15 ENVELOPE\r\n"); //заголовочные поля сообщения
$data = get_data_imap($imap_conn, $key);
print "(answer).&lt;br/>";
print $data."&lt;br/>&lt;br/>1. Parsing:&lt;br/>"; // последовательность полей (через кавычки или двойные скобки) date, subject, from, sender, reply-to, to, cc, bcc, in-reply-to и message-id заголовка сообщения
$data = strstr($data, '"'); //отбрасывает начало строки до первой кавычки
$arr = preg_split('~(?:"[^"]*")?\K[/\s]+~', $data, 7, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
//print_r( $arr );
print str_replace('"', "", $arr[0])."&lt;br/>";
print $arr[1]."&lt;br/>"; // заголовок сообщения
print $arr[2]."&lt;br/>"; // представление имени отправителя
// print $arr[3]."&lt;br/>"; // = NIL
$email = str_replace('"', "", $arr[4])."@".str_replace(')', "", str_replace('"', "", $arr[5]));
print $email."&lt;br/>";
print "&lt;br/>&lt;br/>";
print "&lt;br/>&lt;br/>2. Parsing:&lt;br/>";
print str_replace('"', "", $arr[0])."&lt;br/>"; // Sun, 17 Feb 2019 17:22:30 +0300
$arr_subject = explode("?", $arr[1]);
$subject = "subject. error of parsing";
if (count($arr_subject) === 5)
// $arr_subject[1] // utf-8
if($arr_subject[2] === "B") // B or Q // base64 or quoted-printable
$subject = base64_decode($arr_subject[3]);
else
$subject = quoted_printable_decode($arr_subject[3]);
print $subject."&lt;br/>";
$arr_from = explode("?", $arr[2]);
$from = "from. error of parsing";
if (count($arr_from) === 5)
// $arr_from[1] // utf-8
if($arr_from[2] === "B") // B or Q // base64 or quoted-printable
$from = base64_decode($arr_from[3]);
else
$from = quoted_printable_decode($arr_from[3]);
print $email." (" . $from . ")"."&lt;br/>";
print "&lt;br/>&lt;br/>";
$key = "a5";
$comand = "$key FETCH 15 BODY[TEXT]";
print "(request) ".$comand."&lt;br/>&lt;br/>";
fputs($imap_conn,"$key FETCH 15 BODY[TEXT]\r\n"); // выводим секцию TEXT сообщения. https://rfc2.ru/2060.rfc/32
print "(answer).&lt;br/>";
print get_data_imap($imap_conn, $key)."&lt;br/>&lt;br/>&lt;br/>"; //тут может быть и текст, base64 и другое. нужно парсить параметры
$key = "a9";
$comand = "$key logout";
print "(request) ".$comand."&lt;br/>&lt;br/>";
fputs($imap_conn,"$key logout\r\n");
print "(answer).&lt;br/>";
print get_data_imap($imap_conn, $key)."&lt;br/>&lt;br/>&lt;br/>";
?>