function AddAttachment($arFields) { global $DB; $strSql = "SELECT ATTACHMENTS FROM b_mail_message WHERE ID=" . IntVal($arFields["MESSAGE_ID"]); $dbr = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); if (!($dbr_arr = $dbr->Fetch())) { return false; } $n = IntVal($dbr_arr["ATTACHMENTS"]) + 1; if (strlen($arFields["FILE_NAME"]) <= 0) { $arFields["FILE_NAME"] = $n . "."; if (strpos($arFields["CONTENT_TYPE"], "message/") === 0) { $arFields["FILE_NAME"] .= "msg"; } else { $arFields["FILE_NAME"] .= "tmp"; } } if (is_set($arFields, "CONTENT_TYPE")) { $arFields["CONTENT_TYPE"] = strtolower($arFields["CONTENT_TYPE"]); } if (strpos($arFields["CONTENT_TYPE"], "image/") === 0 && (!is_set($arFields, "IMAGE_WIDTH") || !is_set($arFields, "IMAGE_HEIGHT")) && is_set($arFields, "FILE_DATA")) { $filename = CTempFile::GetFileName(md5(uniqid("")) . '.tmp'); CheckDirPath($filename); if (file_put_contents($filename, $arFields["FILE_DATA"]) !== false) { $img_arr = CFile::GetImageSize($filename); $arFields["IMAGE_WIDTH"] = $img_arr ? $img_arr[0] : 0; $arFields["IMAGE_HEIGHT"] = $img_arr ? $img_arr[1] : 0; } } if (is_set($arFields, "FILE_DATA") && !is_set($arFields, "FILE_SIZE")) { $arFields["FILE_SIZE"] = CUtil::BinStrlen($arFields["FILE_DATA"]); } if (!CMailUtil::IsSizeAllowed(strlen($DB->ForSQL($arFields["FILE_DATA"])) + 100)) { return false; } $ID = $DB->Add("b_mail_msg_attachment", $arFields, array("FILE_DATA")); if ($ID > 0) { $strSql = "UPDATE b_mail_message SET ATTACHMENTS=" . $n . " WHERE ID=" . IntVal($arFields["MESSAGE_ID"]); $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); } return $ID; /* $arFile = Array( "name"=>$filename, "size"=>strlen($part["BODY"]), "type"=>$part["CONTENT-TYPE"], "content"=>$part["BODY"], "MODULE_ID"=>"mail" ); $file_id = CFile::SaveFile($arFile, "mail"); */ }
function AddMessage($mailbox_id, $message, $charset) { global $DB; list($obHeader, $message_body_html, $message_body, $arMessageParts) = CMailMessage::parseMessage($message, $charset); $arFields = array( "MAILBOX_ID" => $mailbox_id, "HEADER" => $obHeader->strHeader, "FIELD_DATE_ORIGINAL" => $obHeader->GetHeader("DATE"), "NEW_MESSAGE" => "Y", "FIELD_FROM" => $obHeader->GetHeader("FROM"), "FIELD_REPLY_TO" => $obHeader->GetHeader("REPLY-TO"), "FIELD_TO" => $obHeader->GetHeader("TO"), "FIELD_CC" => $obHeader->GetHeader("CC"), "FIELD_BCC" => ($obHeader->GetHeader('X-Original-Rcpt-to')!=''?$obHeader->GetHeader('X-Original-Rcpt-to').($obHeader->GetHeader("BCC")!=''?', ':''):'').$obHeader->GetHeader("BCC"), "MSG_ID" => trim($obHeader->GetHeader("MESSAGE-ID"), " <>"), "IN_REPLY_TO" => trim($obHeader->GetHeader("IN-REPLY-TO"), " <>"), "FIELD_PRIORITY" => IntVal($obHeader->GetHeader("X-PRIORITY")), "MESSAGE_SIZE" => strlen($message), "SUBJECT" => $obHeader->GetHeader("SUBJECT"), "BODY" => rtrim($message_body) ); if(COption::GetOptionString("mail", "save_src", B_MAIL_SAVE_SRC)=="Y") $arFields["FULL_TEXT"] = $message; if($message_body_html!==false) $arFields["FOR_SPAM_TEST"] = $obHeader->strHeader." ".$message_body_html; else $arFields["FOR_SPAM_TEST"] = $obHeader->strHeader." ".$message_body; $arFields["SPAM"] = "?"; if(COption::GetOptionString("mail", "spam_check", B_MAIL_CHECK_SPAM)=="Y") { $arSpam = CMailFilter::GetSpamRating($arFields["FOR_SPAM_TEST"]); $arFields["SPAM_RATING"] = $arSpam["RATING"]; $arFields["SPAM_WORDS"] = $arSpam["WORDS"]; $arFields["SPAM_LAST_RESULT"] = "Y"; } if(CMailUtil::IsSizeAllowed(strlen(implode(",", $arFields)))) { $MESSAGE_ID = CMailMessage::Add($arFields); CMailLog::AddMessage( Array( "MAILBOX_ID"=>$mailbox_id, "MESSAGE_ID"=>$MESSAGE_ID, "STATUS_GOOD"=>"Y", "LOG_TYPE"=>"NEW_MESSAGE", "MESSAGE"=>$arFields["SUBJECT"]." (".$arFields["MESSAGE_SIZE"].") ". (COption::GetOptionString("mail", "spam_check", B_MAIL_CHECK_SPAM)=="Y"? "[".Round($arFields["SPAM_RATING"], 3)."]" : "" ) ) ); if(COption::GetOptionString("mail", "save_attachments", B_MAIL_SAVE_ATTACHMENTS)=="Y") { $n=0; foreach($arMessageParts as $part) { $arField = Array( "MESSAGE_ID" => $MESSAGE_ID, "FILE_NAME" => $part["FILENAME"], "CONTENT_TYPE" => $part["CONTENT-TYPE"], "FILE_DATA" => $part["BODY"], "CONTENT_ID" => $part["CONTENT-ID"] ); CMailMessage::AddAttachment($arField); } // foreach($arMessageParts as $part) } $arFields['ID'] = $MESSAGE_ID; CMailFilter::Filter($arFields, "R"); return $MESSAGE_ID; } else { CMailLog::AddMessage(array( "MAILBOX_ID"=>$mailbox_id, "STATUS_GOOD"=>"N", "LOG_TYPE"=>"NEW_MESSAGE", "MESSAGE"=>"Big message size, check mysql max_allow_packet." )); } }
function AddMessage($mailbox_id, $message, $charset) { global $DB; // replace all /n -> /r/n //$message = str_replace("\r\n", "\n", $message); //$message = str_replace("\n", "\r\n", $message); $header_pos = strpos($message, "\r\n\r\n"); // spliting message to header and body $message_header = substr($message, 0, $header_pos); $message_body = substr($message, $header_pos + 4); $message_body_html = false; // parsing header and convert $arMessageParts = array(); $obHeader = CMailMessage::ParseHeader($message_header, $charset); if ($obHeader->IsMultipart()) { //echo 'obHeader="<pre>';print_r($obHeader);echo '/obHeader</pre>'; $arMessagePartsTmp = preg_split("/" . preg_quote("--" . $obHeader->GetBoundary(), "/") . "(--|\r\n)/s", $message_body); $message_body = ""; for ($i = 0; $i < count($arMessagePartsTmp); $i++) { if (strlen($arMessagePartsTmp[$i]) <= 0) { continue; } $message_part = $arMessagePartsTmp[$i]; if (substr($message_part, 0, 2) == "\r\n") { $message_part = "\r\n" . $message_part; } $p = strpos($message_part, "\r\n\r\n"); $message_part_header = substr($message_part, 0, $p); if (strlen(trim($message_part_header)) <= 0 && count($arMessagePartsTmp) > 1) { continue; } $message_part_body = substr($message_part, $p + 4); if (strlen(trim($message_part_body)) <= 0) { continue; } $obMPHeader = CMailMessage::ParseHeader($message_part_header, $charset); if (strtolower($obHeader->MultipartType()) == "alternative" && $obMPHeader->IsMultipart()) { continue; } if (strtolower($obMPHeader->MultipartType()) == "alternative" && $obMPHeader->IsMultipart()) { $arMessagePartsRelTmp = preg_split("/" . preg_quote("--" . $obMPHeader->GetBoundary(), "/") . "(--|\r\n)/s", $message_part_body); $bFound = false; $obMPHeaderHTML = false; for ($j = 0; $j < count($arMessagePartsRelTmp); $j++) { if (strlen(trim($arMessagePartsRelTmp[$j])) <= 0) { continue; } $p = strpos($arMessagePartsRelTmp[$j], "\r\n\r\n"); $message_part_rel_header = substr($arMessagePartsRelTmp[$j], 0, $p); $message_part_rel_body = substr($arMessagePartsRelTmp[$j], $p + 4); $obMPRelHeader = CMailMessage::ParseHeader($message_part_rel_header, $charset); $rel_content_type = strtolower($obMPRelHeader->content_type); if ($rel_content_type == "text/plain") { $bFound = true; $obMPHeader = $obMPRelHeader; $message_part_body = $message_part_rel_body; break; } elseif ($rel_content_type == "text/html" && !is_object($obMPHeaderHTML)) { $obMPHeaderHTML = $obMPRelHeader; $message_part_body_html = $message_part_rel_body; } } // for($j=0; $j<count($arMessagePartsRelTmp); $j++) if (!$bFound && is_object($obMPHeaderHTML)) { $obMPHeader = $obMPHeaderHTML; $message_part_body = HTMLToTxt($message_part_body_html); $bFound = true; } } // if(strtolower($obMPHeader->MultipartType() ... $content_type = strtolower($obMPHeader->content_type); //print_r($obMPHeader); $encoding = strtolower($obMPHeader->GetHeader("CONTENT-TRANSFER-ENCODING")); if ($encoding == "base64") { $message_part_body = base64_decode($message_part_body); } elseif ($encoding == "quoted-printable") { $message_part_body = quoted_printable_decode($message_part_body); } elseif ($encoding == "x-uue") { $message_part_body = CMailUtil::uue_decode($message_part_body); } //? $msg_charset = $obMPHeader->charset; $filename = $obMPHeader->filename; if ((strpos($content_type, "plain") !== false || strpos($content_type, "html") !== false || strpos($content_type, "text") !== false) && strpos($content_type, "x-vcard") === false) { $message_part_body_converted = CMailUtil::ConvertCharset($message_part_body, $msg_charset, $charset); } else { $message_part_body_converted = $message_part_body; } $arMessageParts[] = array("CONTENT-TYPE" => $content_type, "CONTENT-ID" => $obMPHeader->content_id, "CHARSET" => $charset, "BODY" => $message_part_body_converted, "FILENAME" => $filename); } //for($i=0; $i<count($arMessagePartsTmp); $i++) for ($i = 0; $i < count($arMessageParts); $i++) { if (strlen($arMessageParts[$i]["FILENAME"]) <= 0 && strpos(strtolower($arMessageParts[$i]["CONTENT-TYPE"]), "text/") === 0) { if (strtolower($arMessageParts[$i]["CONTENT-TYPE"]) == "text/html") { $message_body_html = $arMessageParts[$i]["BODY"]; $message_body = HTMLToTxt($arMessageParts[$i]["BODY"]); } else { $message_body = $arMessageParts[$i]["BODY"]; } unset($arMessageParts[$i]); break; } } //print_r($arMessageParts); } else { $encoding = strtolower($obHeader->GetHeader("CONTENT-TRANSFER-ENCODING")); if ($encoding == "base64") { $message_body = base64_decode($message_body); } elseif ($encoding == "quoted-printable") { $message_body = quoted_printable_decode($message_body); } elseif ($encoding == "x-uue") { $message_body = CMailUtil::uue_decode($message_body); } //? $message_body = CMailUtil::ConvertCharset($message_body, $obHeader->charset, $charset); if (strtolower($obHeader->content_type) == "text/html") { $message_body_html = $message_body; $message_body = HTMLToTxt($message_body); } } //if($obHeader->IsMultipart()) $arFields = array("MAILBOX_ID" => $mailbox_id, "HEADER" => $obHeader->strHeader, "FIELD_DATE_ORIGINAL" => $obHeader->GetHeader("DATE"), "NEW_MESSAGE" => "Y", "FIELD_FROM" => $obHeader->GetHeader("FROM"), "FIELD_REPLY_TO" => $obHeader->GetHeader("REPLY-TO"), "FIELD_TO" => $obHeader->GetHeader("TO"), "FIELD_CC" => $obHeader->GetHeader("CC"), "FIELD_BCC" => ($obHeader->GetHeader('X-Original-Rcpt-to') != '' ? $obHeader->GetHeader('X-Original-Rcpt-to') . ($obHeader->GetHeader("BCC") != '' ? ', ' : '') : '') . $obHeader->GetHeader("BCC"), "MSG_ID" => trim($obHeader->GetHeader("MESSAGE-ID"), " <>"), "IN_REPLY_TO" => trim($obHeader->GetHeader("IN-REPLY-TO"), " <>"), "FIELD_PRIORITY" => IntVal($obHeader->GetHeader("X-PRIORITY")), "MESSAGE_SIZE" => strlen($message), "SUBJECT" => $obHeader->GetHeader("SUBJECT"), "BODY" => RTrim($message_body)); //print_r($arMessageParts); print_r($arFields); die(); if (COption::GetOptionString("mail", "save_src", B_MAIL_SAVE_SRC) == "Y") { $arFields["FULL_TEXT"] = $message; } if ($message_body_html !== false) { $arFields["FOR_SPAM_TEST"] = $obHeader->strHeader . " " . $message_body_html; } else { $arFields["FOR_SPAM_TEST"] = $obHeader->strHeader . " " . $message_body; } $arFields["SPAM"] = "?"; if (COption::GetOptionString("mail", "spam_check", B_MAIL_CHECK_SPAM) == "Y") { $arSpam = CMailFilter::GetSpamRating($arFields["FOR_SPAM_TEST"]); $arFields["SPAM_RATING"] = $arSpam["RATING"]; $arFields["SPAM_WORDS"] = $arSpam["WORDS"]; $arFields["SPAM_LAST_RESULT"] = "Y"; } if (CMailUtil::IsSizeAllowed(strlen(implode(",", $arFields)))) { $MESSAGE_ID = CMailMessage::Add($arFields); CMailLog::AddMessage(array("MAILBOX_ID" => $mailbox_id, "MESSAGE_ID" => $MESSAGE_ID, "STATUS_GOOD" => "Y", "LOG_TYPE" => "NEW_MESSAGE", "MESSAGE" => $arFields["SUBJECT"] . " (" . $arFields["MESSAGE_SIZE"] . ") " . (COption::GetOptionString("mail", "spam_check", B_MAIL_CHECK_SPAM) == "Y" ? "[" . Round($arFields["SPAM_RATING"], 3) . "]" : ""))); if (COption::GetOptionString("mail", "save_attachments", B_MAIL_SAVE_ATTACHMENTS) == "Y") { $n = 0; foreach ($arMessageParts as $part) { $arField = array("MESSAGE_ID" => $MESSAGE_ID, "FILE_NAME" => $part["FILENAME"], "CONTENT_TYPE" => $part["CONTENT-TYPE"], "FILE_DATA" => $part["BODY"], "CONTENT_ID" => $part["CONTENT-ID"]); CMailMessage::AddAttachment($arField); } // foreach($arMessageParts as $part) } CMailFilter::FilterMessage($MESSAGE_ID, "R"); return $MESSAGE_ID; } else { CMailLog::AddMessage(array("MAILBOX_ID" => $mailbox_id, "STATUS_GOOD" => "N", "LOG_TYPE" => "NEW_MESSAGE", "MESSAGE" => "Big message size, check mysql max_allow_packet.")); } }