/** * @param $mailingChainId * @param array $params * @return string * @throws \Bitrix\Main\ArgumentException * @throws \Bitrix\Main\DB\Exception */ protected static function sendInternal($mailingChainId, array $params) { if (static::$currentMailingChainFields !== null) { if (static::$currentMailingChainFields['ID'] != $mailingChainId) { static::$currentMailingChainFields = null; } } if (static::$currentMailingChainFields === null) { $mailingChainDb = MailingChainTable::getList(array('select' => array('*', 'SITE_ID' => 'MAILING.SITE_ID'), 'filter' => array('ID' => $mailingChainId))); if (!($mailingChain = $mailingChainDb->fetch())) { return PostingRecipientTable::SEND_RESULT_ERROR; } $charset = false; $siteDb = \Bitrix\Main\SiteTable::getList(array('select' => array('SERVER_NAME', 'NAME', 'CULTURE_CHARSET' => 'CULTURE.CHARSET'), 'filter' => array('LID' => $mailingChain['SITE_ID']))); if ($site = $siteDb->fetch()) { $charset = $site['CULTURE_CHARSET']; $serverName = $site['SERVER_NAME']; } else { throw new \Bitrix\Main\DB\Exception(Loc::getMessage('SENDER_POSTING_MANAGER_ERR_SITE', array('#SITE_ID#' => $mailingChain['SITE_ID']))); } if (!$charset) { throw new \Bitrix\Main\DB\Exception(Loc::getMessage('SENDER_POSTING_MANAGER_ERR_CHARSET', array('#SITE_ID#' => "[" . $mailingChain['SITE_ID'] . "]" . $site['NAME']))); } $attachmentList = array(); $attachmentDb = \Bitrix\Sender\MailingAttachmentTable::getList(array('select' => array('FILE_ID'), 'filter' => array('CHAIN_ID' => $mailingChainId))); while ($attachment = $attachmentDb->fetch()) { $attachmentList[] = $attachment['FILE_ID']; } static::$currentMailingChainFields = array(); static::$currentMailingChainFields['EVENT'] = array('FILE' => $attachmentList); static::$currentMailingChainFields['ID'] = $mailingChain['ID']; static::$currentMailingChainFields['MESSAGE'] = array('BODY_TYPE' => 'html', 'EMAIL_FROM' => $mailingChain['EMAIL_FROM'], 'EMAIL_TO' => '#EMAIL_TO#', 'SUBJECT' => $mailingChain['SUBJECT'], 'MESSAGE' => $mailingChain['MESSAGE'], 'MESSAGE_PHP' => \Bitrix\Main\Mail\Internal\EventMessageTable::replaceTemplateToPhp($mailingChain['MESSAGE'])); static::$currentMailingChainFields['SITE'] = array($mailingChain['SITE_ID']); static::$currentMailingChainFields['CHARSET'] = $charset; static::$currentMailingChainFields['SERVER_NAME'] = $serverName; static::$currentMailingChainFields['LINK_PROTOCOL'] = \Bitrix\Main\Config\Option::get("sender", "link_protocol", 'http'); } $messageParams = array('EVENT' => static::$currentMailingChainFields['EVENT'], 'FIELDS' => $params['FIELDS'], 'MESSAGE' => static::$currentMailingChainFields['MESSAGE'], 'SITE' => static::$currentMailingChainFields['SITE'], 'CHARSET' => static::$currentMailingChainFields['CHARSET']); if (!empty($params['FIELDS']['UNSUBSCRIBE_LINK'])) { if (substr($params['FIELDS']['UNSUBSCRIBE_LINK'], 0, 4) !== 'http') { if (!empty(static::$currentMailingChainFields['SERVER_NAME'])) { $serverName = static::$currentMailingChainFields['SERVER_NAME']; } else { $serverName = \Bitrix\Main\Config\Option::get("main", "server_name", $GLOBALS["SERVER_NAME"]); } $linkProtocol = static::$currentMailingChainFields['LINK_PROTOCOL']; $params['FIELDS']['UNSUBSCRIBE_LINK'] = $linkProtocol . '://' . $serverName . $params['FIELDS']['UNSUBSCRIBE_LINK']; } } $message = Mail\EventMessageCompiler::createInstance($messageParams); $message->compile(); $mailHeaders = $message->getMailHeaders(); if (!empty($params['FIELDS']['UNSUBSCRIBE_LINK'])) { $unsubUrl = $params['FIELDS']['UNSUBSCRIBE_LINK']; $mailHeaders['List-Unsubscribe'] = '<' . $unsubUrl . '>'; } // send mail $result = Mail\Mail::send(array('TO' => $message->getMailTo(), 'SUBJECT' => $message->getMailSubject(), 'BODY' => $message->getMailBody(), 'HEADER' => $mailHeaders, 'CHARSET' => $message->getMailCharset(), 'CONTENT_TYPE' => $message->getMailContentType(), 'MESSAGE_ID' => '', 'ATTACHMENT' => $message->getMailAttachment(), 'LINK_PROTOCOL' => static::$currentMailingChainFields['LINK_PROTOCOL'], 'LINK_DOMAIN' => static::$currentMailingChainFields['SERVER_NAME'], 'TRACK_READ' => isset($params['TRACK_READ']) ? $params['TRACK_READ'] : null, 'TRACK_CLICK' => isset($params['TRACK_CLICK']) ? $params['TRACK_CLICK'] : null)); if ($result) { return PostingRecipientTable::SEND_RESULT_SUCCESS; } else { return PostingRecipientTable::SEND_RESULT_ERROR; } }
/** * @param $mailingChainId * @param array $arParams * @return string * @throws \Bitrix\Main\ArgumentException * @throws \Bitrix\Main\DB\Exception */ protected static function sendInternal($mailingChainId, array $arParams) { if (static::$currentMailingChainFields !== null) { if (static::$currentMailingChainFields['ID'] != $mailingChainId) { static::$currentMailingChainFields = null; } } if (static::$currentMailingChainFields === null) { $mailingChainDb = MailingChainTable::getList(array('select' => array('*', 'SITE_ID' => 'MAILING.SITE_ID'), 'filter' => array('ID' => $mailingChainId))); if (!($arMailingChain = $mailingChainDb->fetch())) { return PostingRecipientTable::SEND_RESULT_ERROR; } $charset = false; $siteDb = \Bitrix\Main\SiteTable::getList(array('select' => array('NAME', 'CULTURE_CHARSET' => 'CULTURE.CHARSET'), 'filter' => array('LID' => $arMailingChain['SITE_ID']))); if ($arSiteDb = $siteDb->fetch()) { $charset = $arSiteDb['CULTURE_CHARSET']; } else { throw new \Bitrix\Main\DB\Exception(Loc::getMessage('SENDER_POSTING_MANAGER_ERR_SITE', array('#SITE_ID#' => $arMailingChain['SITE_ID']))); } if (!$charset) { throw new \Bitrix\Main\DB\Exception(Loc::getMessage('SENDER_POSTING_MANAGER_ERR_CHARSET', array('#SITE_ID#' => "[" . $arMailingChain['SITE_ID'] . "]" . $arSiteDb['NAME']))); } static::$currentMailingChainFields = array(); static::$currentMailingChainFields['ID'] = $arMailingChain['ID']; static::$currentMailingChainFields['MESSAGE'] = array('BODY_TYPE' => 'html', 'EMAIL_FROM' => $arMailingChain['EMAIL_FROM'], 'EMAIL_TO' => '#EMAIL_TO#', 'SUBJECT' => $arMailingChain['SUBJECT'], 'MESSAGE' => $arMailingChain['MESSAGE'], 'MESSAGE_PHP' => \Bitrix\Main\Mail\Internal\EventMessageTable::replaceTemplateToPhp($arMailingChain['MESSAGE'])); static::$currentMailingChainFields['SITE'] = array($arMailingChain['SITE_ID']); static::$currentMailingChainFields['CHARSET'] = $charset; } $arMessageParams = array('FIELDS' => $arParams['FIELDS'], 'MESSAGE' => static::$currentMailingChainFields['MESSAGE'], 'SITE' => static::$currentMailingChainFields['SITE'], 'CHARSET' => static::$currentMailingChainFields['CHARSET']); $message = Mail\EventMessageCompiler::createInstance($arMessageParams); $message->compile(); // send mail $result = Mail\Mail::send(array('TO' => $message->getMailTo(), 'SUBJECT' => $message->getMailSubject(), 'BODY' => $message->getMailBody(), 'HEADER' => $message->getMailHeaders(), 'CHARSET' => $message->getMailCharset(), 'CONTENT_TYPE' => $message->getMailContentType(), 'MESSAGE_ID' => '', 'ATTACHMENT' => $message->getMailAttachment(), 'TRACK_READ' => isset($arParams['TRACK_READ']) ? $arParams['TRACK_READ'] : null, 'TRACK_CLICK' => isset($arParams['TRACK_CLICK']) ? $arParams['TRACK_CLICK'] : null)); if ($result) { return PostingRecipientTable::SEND_RESULT_SUCCESS; } else { return PostingRecipientTable::SEND_RESULT_ERROR; } }
/** * @return string */ public static function handleEvent(array $arEvent) { if (!isset($arEvent['FIELDS']) && isset($arEvent['C_FIELDS'])) { $arEvent['FIELDS'] = $arEvent['C_FIELDS']; } if (!is_array($arEvent['FIELDS'])) { throw new \Bitrix\Main\ArgumentTypeException("FIELDS"); } $flag = static::SEND_RESULT_TEMPLATE_NOT_FOUND; // no templates $arResult = array("Success" => false, "Fail" => false, "Was" => false); $trackRead = null; $trackClick = null; if (array_key_exists('TRACK_READ', $arEvent)) { $trackRead = $arEvent['TRACK_READ']; } if (array_key_exists('TRACK_CLICK', $arEvent)) { $trackClick = $arEvent['TRACK_CLICK']; } $arSites = explode(",", $arEvent["LID"]); if (count($arSites) <= 0) { return $flag; } // get charset and server name for languages of event $charset = false; $serverName = null; $siteDb = Main\SiteTable::getList(array('select' => array('SERVER_NAME', 'CULTURE_CHARSET' => 'CULTURE.CHARSET'), 'filter' => array('LID' => $arSites))); if ($arSiteDb = $siteDb->fetch()) { $charset = $arSiteDb['CULTURE_CHARSET']; $serverName = $arSiteDb['SERVER_NAME']; } if (!$charset) { return $flag; } // get filter for list of message templates $arEventMessageFilter = array(); $MESSAGE_ID = intval($arEvent["MESSAGE_ID"]); if ($MESSAGE_ID > 0) { $eventMessageDb = MailInternal\EventMessageTable::getById($MESSAGE_ID); if ($eventMessageDb->Fetch()) { $arEventMessageFilter['ID'] = $MESSAGE_ID; $arEventMessageFilter['ACTIVE'] = 'Y'; } } if (count($arEventMessageFilter) == 0) { $arEventMessageFilter = array('ACTIVE' => 'Y', 'EVENT_NAME' => $arEvent["EVENT_NAME"], 'EVENT_MESSAGE_SITE.SITE_ID' => $arSites); } // get list of message templates of event $messageDb = MailInternal\EventMessageTable::getList(array('select' => array('ID'), 'filter' => $arEventMessageFilter, 'group' => array('ID'))); while ($arMessage = $messageDb->fetch()) { $eventMessage = MailInternal\EventMessageTable::getRowById($arMessage['ID']); $eventMessage['FILES'] = array(); $attachmentDb = MailInternal\EventMessageAttachmentTable::getList(array('select' => array('FILE_ID'), 'filter' => array('EVENT_MESSAGE_ID' => $arMessage['ID']))); while ($arAttachmentDb = $attachmentDb->fetch()) { $eventMessage['FILE'][] = $arAttachmentDb['FILE_ID']; } $arFields = $arEvent['FIELDS']; foreach (GetModuleEvents("main", "OnBeforeEventSend", true) as $event) { if (ExecuteModuleEventEx($event, array(&$arFields, &$eventMessage)) === false) { continue 2; } } // get message object for send mail $arMessageParams = array('EVENT' => $arEvent, 'FIELDS' => $arFields, 'MESSAGE' => $eventMessage, 'SITE' => $arSites, 'CHARSET' => $charset); $message = EventMessageCompiler::createInstance($arMessageParams); $message->compile(); // send mail $result = Main\Mail\Mail::send(array('TO' => $message->getMailTo(), 'SUBJECT' => $message->getMailSubject(), 'BODY' => $message->getMailBody(), 'HEADER' => $message->getMailHeaders(), 'CHARSET' => $message->getMailCharset(), 'CONTENT_TYPE' => $message->getMailContentType(), 'MESSAGE_ID' => $message->getMailId(), 'ATTACHMENT' => $message->getMailAttachment(), 'TRACK_READ' => $trackRead, 'TRACK_CLICK' => $trackClick, 'LINK_PROTOCOL' => \Bitrix\Main\Config\Option::get("main", "mail_link_protocol", ''), 'LINK_DOMAIN' => $serverName)); if ($result) { $arResult["Success"] = true; } else { $arResult["Fail"] = true; } $arResult["Was"] = true; } if ($arResult["Was"]) { if ($arResult["Success"]) { if ($arResult["Fail"]) { $flag = static::SEND_RESULT_PARTLY; } else { $flag = static::SEND_RESULT_SUCCESS; } // all sent } else { if ($arResult["Fail"]) { $flag = static::SEND_RESULT_ERROR; } // all templates failed } } return $flag; }
/** * * @param $mailingChainId * @param array $params * @return string * @throws \Bitrix\Main\ArgumentException * @throws \Bitrix\Main\DB\Exception */ protected static function sendInternal($mailingChainId, array $params) { // event before sending $eventSendParams = $params; $eventSendParams['MAILING_CHAIN_ID'] = $mailingChainId; $event = new Event('sender', 'OnBeforePostingSendRecipient', array($eventSendParams)); $event->send(); foreach ($event->getResults() as $eventResult) { if ($eventResult->getType() == \Bitrix\Main\EventResult::ERROR) { return PostingRecipientTable::SEND_RESULT_ERROR; } if (is_array($eventResult->getParameters())) { $eventSendParams = array_merge($eventSendParams, $eventResult->getParameters()); } } unset($eventSendParams['MAILING_CHAIN_ID']); $params = $eventSendParams; // prepare common params if (static::$currentMailingChainFields !== null) { if (static::$currentMailingChainFields['ID'] != $mailingChainId) { static::$currentMailingChainFields = null; } } if (static::$currentMailingChainFields === null) { $mailingChainDb = MailingChainTable::getList(array('select' => array('*', 'SITE_ID' => 'MAILING.SITE_ID'), 'filter' => array('=ID' => $mailingChainId))); if (!($mailingChain = $mailingChainDb->fetch())) { return PostingRecipientTable::SEND_RESULT_ERROR; } $charset = false; $siteDb = \Bitrix\Main\SiteTable::getList(array('select' => array('SERVER_NAME', 'NAME', 'CULTURE_CHARSET' => 'CULTURE.CHARSET'), 'filter' => array('=LID' => $mailingChain['SITE_ID']))); if ($site = $siteDb->fetch()) { $charset = $site['CULTURE_CHARSET']; $serverName = $site['SERVER_NAME']; } else { throw new \Bitrix\Main\DB\Exception(Loc::getMessage('SENDER_POSTING_MANAGER_ERR_SITE', array('#SITE_ID#' => $mailingChain['SITE_ID']))); } if (!$charset) { throw new \Bitrix\Main\DB\Exception(Loc::getMessage('SENDER_POSTING_MANAGER_ERR_CHARSET', array('#SITE_ID#' => "[" . $mailingChain['SITE_ID'] . "]" . $site['NAME']))); } $attachmentList = array(); $attachmentDb = \Bitrix\Sender\MailingAttachmentTable::getList(array('select' => array('FILE_ID'), 'filter' => array('CHAIN_ID' => $mailingChainId))); while ($attachment = $attachmentDb->fetch()) { $attachmentList[] = $attachment['FILE_ID']; } static::$currentMailingChainFields = array(); static::$currentMailingChainFields['EVENT'] = array('FILE' => $attachmentList); static::$currentMailingChainFields['ID'] = $mailingChain['ID']; // create final mail-text due to filling template by blocks static::$currentMailingChainFields['IS_MESSAGE_WITH_TEMPLATE'] = false; if ($mailingChain['TEMPLATE_TYPE'] && $mailingChain['TEMPLATE_ID']) { $chainTemplate = \Bitrix\Sender\Preset\Template::getById($mailingChain['TEMPLATE_TYPE'], $mailingChain['TEMPLATE_ID']); if ($chainTemplate && $chainTemplate['HTML']) { $document = new \Bitrix\Main\Web\DOM\Document(); $document->loadHTML($chainTemplate['HTML']); \Bitrix\Main\Loader::includeModule('fileman'); if (\Bitrix\Fileman\Block\Editor::fillDocumentBySliceContent($document, $mailingChain['MESSAGE'])) { \Bitrix\Main\Web\DOM\StyleInliner::inlineDocument($document); $mailingChain['MESSAGE'] = $document->saveHTML(); static::$currentMailingChainFields['IS_MESSAGE_WITH_TEMPLATE'] = true; } else { unset($document); } } } static::$currentMailingChainFields['MESSAGE'] = array('BODY_TYPE' => 'html', 'EMAIL_FROM' => $mailingChain['EMAIL_FROM'], 'EMAIL_TO' => '#EMAIL_TO#', 'PRIORITY' => $mailingChain['PRIORITY'], 'SUBJECT' => $mailingChain['SUBJECT'], 'MESSAGE' => $mailingChain['MESSAGE'], 'MESSAGE_PHP' => \Bitrix\Main\Mail\Internal\EventMessageTable::replaceTemplateToPhp($mailingChain['MESSAGE'])); static::$currentMailingChainFields['SITE'] = array($mailingChain['SITE_ID']); static::$currentMailingChainFields['CHARSET'] = $charset; static::$currentMailingChainFields['SERVER_NAME'] = $serverName; static::$currentMailingChainFields['LINK_PROTOCOL'] = \Bitrix\Main\Config\Option::get("sender", "link_protocol", 'http'); static::$currentMailingChainFields['LINK_PARAMS'] = $mailingChain['LINK_PARAMS']; } $trackClick = isset($params['TRACK_CLICK']) ? $params['TRACK_CLICK'] : null; if ($trackClick && static::$currentMailingChainFields['LINK_PARAMS']) { if (!is_array($trackClick['URL_PARAMS'])) { $trackClick['URL_PARAMS'] = array(); } parse_str(static::$currentMailingChainFields['LINK_PARAMS'], $trackClickTmp); if (is_array($trackClickTmp)) { $trackClick['URL_PARAMS'] = array_merge($trackClick['URL_PARAMS'], $trackClickTmp); } } // prepare params for send email $messageParams = array('EVENT' => static::$currentMailingChainFields['EVENT'], 'FIELDS' => $params['FIELDS'], 'MESSAGE' => static::$currentMailingChainFields['MESSAGE'], 'SITE' => static::$currentMailingChainFields['SITE'], 'CHARSET' => static::$currentMailingChainFields['CHARSET']); if (!empty($params['FIELDS']['UNSUBSCRIBE_LINK'])) { if (substr($params['FIELDS']['UNSUBSCRIBE_LINK'], 0, 4) !== 'http') { if (!empty(static::$currentMailingChainFields['SERVER_NAME'])) { $serverName = static::$currentMailingChainFields['SERVER_NAME']; } else { $serverName = \Bitrix\Main\Config\Option::get("main", "server_name", $GLOBALS["SERVER_NAME"]); } $linkProtocol = static::$currentMailingChainFields['LINK_PROTOCOL']; $params['FIELDS']['UNSUBSCRIBE_LINK'] = $linkProtocol . '://' . $serverName . $params['FIELDS']['UNSUBSCRIBE_LINK']; } } // event on sending $eventMessageParams = $messageParams; $eventMessageParams['MAILING_CHAIN_ID'] = $mailingChainId; $eventMessageParams['IS_MESSAGE_WITH_TEMPLATE'] = static::$currentMailingChainFields['IS_MESSAGE_WITH_TEMPLATE']; $event = new Event('sender', 'OnPostingSendRecipient', array($eventMessageParams)); $event->send(); foreach ($event->getResults() as $eventResult) { if ($eventResult->getType() == \Bitrix\Main\EventResult::ERROR) { return PostingRecipientTable::SEND_RESULT_ERROR; } if (is_array($eventResult->getParameters())) { $eventMessageParams = array_merge($eventMessageParams, $eventResult->getParameters()); } } static::$currentMailingChainFields['IS_MESSAGE_WITH_TEMPLATE'] = $eventMessageParams['IS_MESSAGE_WITH_TEMPLATE']; unset($eventMessageParams['IS_MESSAGE_WITH_TEMPLATE']); unset($eventMessageParams['MAILING_CHAIN_ID']); $messageParams = $eventMessageParams; $message = Mail\EventMessageCompiler::createInstance($messageParams); $message->compile(); // add unsubscribe info to header $mailHeaders = $message->getMailHeaders(); if (!empty($params['FIELDS']['UNSUBSCRIBE_LINK'])) { $unsubUrl = $params['FIELDS']['UNSUBSCRIBE_LINK']; $mailHeaders['List-Unsubscribe'] = '<' . $unsubUrl . '>'; } $mailBody = null; if (static::$currentMailingChainFields['IS_MESSAGE_WITH_TEMPLATE'] && Option::get('sender', 'use_inliner_for_each_template_mail', 'N') == 'Y') { // inline styles $mailBody = \Bitrix\Main\Web\DOM\StyleInliner::inlineHtml($message->getMailBody()); } if (!$mailBody) { $mailBody = $message->getMailBody(); } // set email params $mailParams = array('TO' => $message->getMailTo(), 'SUBJECT' => $message->getMailSubject(), 'BODY' => $mailBody, 'HEADER' => $mailHeaders, 'CHARSET' => $message->getMailCharset(), 'CONTENT_TYPE' => $message->getMailContentType(), 'MESSAGE_ID' => '', 'ATTACHMENT' => $message->getMailAttachment(), 'LINK_PROTOCOL' => static::$currentMailingChainFields['LINK_PROTOCOL'], 'LINK_DOMAIN' => static::$currentMailingChainFields['SERVER_NAME'], 'TRACK_READ' => isset($params['TRACK_READ']) ? $params['TRACK_READ'] : null, 'TRACK_CLICK' => $trackClick); // event on sending email $eventMailParams = $mailParams; $eventMailParams['MAILING_CHAIN_ID'] = $mailingChainId; $event = new Event('sender', 'OnPostingSendRecipientEmail', array($eventMailParams)); $event->send(); foreach ($event->getResults() as $eventResult) { if ($eventResult->getType() == \Bitrix\Main\EventResult::ERROR) { return PostingRecipientTable::SEND_RESULT_ERROR; } if (is_array($eventResult->getParameters())) { $eventMailParams = array_merge($eventMailParams, $eventResult->getParameters()); } } unset($eventMailParams['MAILING_CHAIN_ID']); $mailParams = $eventMailParams; // send mail $result = Mail\Mail::send($mailParams); if ($result) { return PostingRecipientTable::SEND_RESULT_SUCCESS; } else { return PostingRecipientTable::SEND_RESULT_ERROR; } }