Exemple #1
0
 /**
  * @param Entity\Event $event
  * @return Entity\EventResult
  */
 public static function onDelete(Entity\Event $event)
 {
     $result = new Entity\EventResult();
     $data = $event->getParameters();
     $deleteIdList = array();
     if (!empty($data['primary'])) {
         $itemDb = static::getList(array('select' => array('ID'), 'filter' => $data['primary']));
         while ($item = $itemDb->fetch()) {
             $deleteIdList[] = $item['ID'];
         }
     }
     foreach ($deleteIdList as $chainId) {
         MailingAttachmentTable::delete(array('CHAIN_ID' => $chainId));
         MailingTriggerTable::delete(array('MAILING_CHAIN_ID' => $chainId));
         PostingTable::delete(array('MAILING_CHAIN_ID' => $chainId));
     }
     return $result;
 }
 /**
  * @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;
     }
 }
                    if (!empty($TEMPLATE_ACTION_SAVE_NAME) && !empty($MESSAGE)) {
                        \Bitrix\Sender\TemplateTable::add(array('NAME' => $TEMPLATE_ACTION_SAVE_NAME, 'CONTENT' => $MESSAGE));
                    }
                }
            }
            $step = 'chain_send_type';
            $isPostedFormProcessed = true;
            LocalRedirect('sender_mailing_wizard.php?step=' . $step . '&MAILING_ID=' . $MAILING_ID . "&MAILING_CHAIN_ID=" . $MAILING_CHAIN_ID . "&lang=" . LANGUAGE_ID);
        } else {
            $DB->InitTableVarsForEdit("b_sender_mailing_chain", "", "str_");
        }
    } else {
    }
    $arMailngChainAttachment = array();
    if ($ID > 0) {
        $attachmentFileDb = \Bitrix\Sender\MailingAttachmentTable::getList(array('select' => array('FILE_ID'), 'filter' => array('CHAIN_ID' => $ID)));
        while ($ar = $attachmentFileDb->fetch()) {
            if ($arFileFetch = CFile::GetFileArray($ar['FILE_ID'])) {
                $arMailngChainAttachment[] = $arFileFetch;
            }
        }
    }
    $templateListHtml = \Bitrix\Sender\Preset\Template::getTemplateListHtml();
}
if ($step == 'chain_send_type') {
    $ID = $MAILING_CHAIN_ID;
    IncludeModuleLangFile(dirname(__FILE__) . "/mailing_chain_edit.php");
    $DAYS_OF_WEEK = empty($DAYS_OF_WEEK) ? '' : implode(',', $DAYS_OF_WEEK);
    if ($REQUEST_METHOD == "POST" && !$isPostedFormProcessed && check_bitrix_sessid()) {
        $arFields = array("REITERATE" => "N", "AUTO_SEND_TIME" => "", "DAYS_OF_WEEK" => "", "DAYS_OF_MONTH" => "", "TIMES_OF_DAY" => "");
        switch ($SEND_TYPE) {
Exemple #4
0
 /**
  *
  * @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;
     }
 }