Подключили Яндекс Кассу по email протоколу, теперь нужно автоматизировать процесс, чтобы после оплаты в базе данных статус изменился на оплачено. Имеется рабочий скрипт, но письма Яндекса поступают закодированными S/MIME Cryptographic Signature. Может кто-нибудь сталкилвался с подобным, как прочитать такое письмо?
<?php
header("Content-Type: text/html; charset=utf-8");
error_reporting(-1);
function check_utf8($charset){
if(strtolower($charset) != "utf-8"){
return false;
}
return true;
}
function convert_to_utf8($in_charset, $str){
return iconv(strtolower($in_charset), "utf-8", $str);
}
function get_imap_title($str){
$mime = imap_mime_header_decode($str);
$title = "";
foreach($mime as $key => $m){
if(!check_utf8($m->charset)){
$title .= convert_to_utf8($m->charset, $m->text);
} else {
$title .= $m->text;
}
}
return $title;
}
function structure_encoding($encoding, $msg_body){
switch((int) $encoding){
case 4:
$body = imap_qprint($msg_body);
break;
case 3:
$body = imap_base64($msg_body);
break;
case 2:
$body = imap_binary($msg_body);
break;
case 1:
$body = imap_8bit($msg_body);
break;
case 0:
$body = $msg_body;
break;
default:
$body = "";
break;
}
return $body;
}
function recursive_search($structure){
$encoding = "";
if($structure->subtype == "HTML" || $structure->type == 0) {
if($structure->parameters[0]->attribute == "charset"){
$charset = $structure->parameters[0]->value;
}
return array(
"encoding" => $structure->encoding,
"charset" => strtolower($charset),
"subtype" => $structure->subtype
);
} else {
if(isset($structure->parts[0])) {
return recursive_search($structure->parts[0]);
} else {
if($structure->parameters[0]->attribute == "charset"){
$charset = $structure->parameters[0]->value;
}
return array(
"encoding" => $structure->encoding,
"charset" => strtolower($charset),
"subtype" => $structure->subtype
);
}
}
}
$mail_login = "***@yandex.ru";
$mail_password = "***";
$mail_imap = "{imap.yandex.ru:993/imap/ssl}";
$mail_filetypes = array(
"MSWORD"
);
$connection = imap_open($mail_imap, $mail_login, $mail_password);
if(!$connection) {
echo "Ошибка соединения с почтой - ".$mail_login;
exit;
} else {
$msg_num = imap_num_msg($connection);
$mails_data = array();
for($i = 1; $i <= $msg_num; $i++) {
$msg_header = imap_header($connection, $i);
$mails_data[$i]["time"] = time($msg_header->MailDate);
$mails_data[$i]["date"] = $msg_header->MailDate;
foreach($msg_header->to as $data){
$mails_data[$i]["to"] = $data->mailbox."@".$data->host;
}
foreach($msg_header->from as $data){
$mails_data[$i]["from"] = $data->mailbox."@".$data->host;
}
$mails_data[$i]["title"] = get_imap_title($msg_header->subject);
$msg_structure = imap_fetchstructure($connection, $i);
$msg_body = imap_fetchbody($connection, $i, 1);
$body = "";
$recursive_data = recursive_search($msg_structure);
if($recursive_data["encoding"] == 0 ||
$recursive_data["encoding"] == 1){
$body = $msg_body;
}
if($recursive_data["encoding"] == 4){
$body = structure_encoding($recursive_data["encoding"], $msg_body);
}
if($recursive_data["encoding"] == 3){
$body = structure_encoding($recursive_data["encoding"], $msg_body);
}
if($recursive_data["encoding"] == 2){
$body = structure_encoding($recursive_data["encoding"], $msg_body);
}
if(!check_utf8($recursive_data["charset"])){
$body = convert_to_utf8($recursive_data["charset"], $msg_body);
}
echo htmlspecialchars($body);
}
}
imap_close($connection);
?>