if ($fid > 0) { $resultAddAttachFile = \Bitrix\Sender\MailingAttachmentTable::add(array('CHAIN_ID' => $ID, 'FILE_ID' => $fid)); $resultInsertAttachFile = $resultAddAttachFile->isSuccess(); } if (!$resultInsertAttachFile) { break; } } } if (isset($TEMPLATE_ACTION_SAVE) && $TEMPLATE_ACTION_SAVE == 'Y') { if (!empty($TEMPLATE_ACTION_SAVE_NAME) && !empty($MESSAGE)) { $CONTENT = $MESSAGE; $useBlockEditor = false; if ($TEMPLATE_TYPE && $TEMPLATE_ID) { \Bitrix\Main\Loader::includeModule('fileman'); $chainTemplate = \Bitrix\Sender\Preset\Template::getById($TEMPLATE_TYPE, $TEMPLATE_ID); if ($chainTemplate && $chainTemplate['HTML']) { $CONTENT = \Bitrix\Fileman\Block\Editor::fillTemplateBySliceContent($chainTemplate['HTML'], $CONTENT); if ($CONTENT) { $useBlockEditor = true; } } } $addResult = \Bitrix\Sender\TemplateTable::add(array('NAME' => $TEMPLATE_ACTION_SAVE_NAME, 'CONTENT' => $CONTENT)); if ($useBlockEditor && $addResult->isSuccess()) { \Bitrix\Sender\MailingChainTable::update(array('ID' => $ID), array('TEMPLATE_TYPE' => 'USER', 'TEMPLATE_ID' => $addResult->getId())); } } } } $step = 'chain_send_type';
} if ($message) { echo $message->Show(); } if (!isset($SEND_TYPE)) { if ($str_REITERATE == 'Y') { $SEND_TYPE = 'REITERATE'; } elseif (!empty($str_AUTO_SEND_TIME)) { $SEND_TYPE = 'TIME'; } elseif ($ID > 0) { $SEND_TYPE = 'MANUAL'; } } $templateListHtml = \Bitrix\Sender\Preset\Template::getTemplateListHtml('tabControl_layout'); $templateName = ''; $template = \Bitrix\Sender\Preset\Template::getById($str_TEMPLATE_TYPE, $str_TEMPLATE_ID); if ($template) { $templateName = $template['NAME']; } ?> <script> function SendTestMailing() { var data = { 'action': 'send_to_me', 'send_to_me_addr': BX('EMAIL_TO_ME').value }; var url = '/bitrix/admin/sender_mailing_chain_admin.php?MAILING_ID=<?php echo $MAILING_ID;
if (!\Bitrix\Main\Loader::includeModule("sender")) { ShowError(\Bitrix\Main\Localization\Loc::getMessage("MAIN_MODULE_NOT_INSTALLED")); } IncludeModuleLangFile(__FILE__); $POST_RIGHT = $APPLICATION->GetGroupRight("sender"); if ($POST_RIGHT == "D") { $APPLICATION->AuthForm(GetMessage("ACCESS_DENIED")); } $request = \Bitrix\Main\Context::getCurrent()->getRequest(); if ($request->get('action') == 'get_template') { require_once $_SERVER["DOCUMENT_ROOT"] . "/bitrix/modules/main/include/prolog_admin_js.php"; $templateType = $request->get('template_type'); $templateId = $request->get('template_id'); $templateCharset = $request->get('template_charset'); \Bitrix\Main\Loader::includeModule('fileman'); $template = \Bitrix\Sender\Preset\Template::getById($templateType, $templateId); if ($template) { echo \Bitrix\Fileman\Block\Editor::getHtmlForEditor($template['HTML'], $templateCharset); } require_once $_SERVER["DOCUMENT_ROOT"] . "/bitrix/modules/main/include/epilog_admin_js.php"; } $sTableID = "tbl_sender_template"; $ID = intval($_REQUEST["ID"]); $oSort = new CAdminSorting($sTableID, "ID", "desc"); $lAdmin = new CAdminList($sTableID, $oSort); function CheckFilter() { global $FilterArr, $lAdmin; foreach ($FilterArr as $f) { global ${$f}; }
/** * * @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; } }