Пример #1
0
 /**
  * Change status of recipients and mailing chain for resending mails to recipients who have error sending
  *
  * @param $id
  * @return void
  */
 public static function prepareReSendErrorRecipients($id)
 {
     if (!static::canReSendErrorRecipients($id)) {
         return;
     }
     $mailingChain = static::getRowById(array('ID' => $id));
     $updateSql = 'UPDATE ' . PostingRecipientTable::getTableName() . " SET STATUS='" . PostingRecipientTable::SEND_RESULT_NONE . "'" . " WHERE POSTING_ID=" . intval($mailingChain['POSTING_ID']) . " AND STATUS='" . PostingRecipientTable::SEND_RESULT_ERROR . "'";
     \Bitrix\Main\Application::getConnection()->query($updateSql);
     PostingTable::update(array('ID' => $mailingChain['POSTING_ID']), array('STATUS' => PostingTable::STATUS_PART));
     static::update(array('ID' => $id), array('STATUS' => static::STATUS_SEND));
 }
Пример #2
0
 /**
  * Handler of after add event
  *
  * @param Entity\Event $event
  * @return Entity\EventResult
  */
 public static function onAfterAdd(Entity\Event $event)
 {
     $result = new Entity\EventResult();
     $data = $event->getParameters();
     $data = $data['fields'];
     // update unsub flag of recipient
     PostingRecipientTable::update(array('ID' => $data['RECIPIENT_ID']), array('IS_UNSUB' => 'Y'));
     // update unsub counter of posting
     $resultDb = static::getList(array('filter' => array('RECIPIENT_ID' => $data['RECIPIENT_ID'])));
     if ($resultDb->getSelectedRowsCount() == 1) {
         PostingTable::update(array('ID' => $data['POSTING_ID']), array('COUNT_UNSUB' => new \Bitrix\Main\DB\SqlExpression('?# + 1', 'COUNT_UNSUB')));
     }
     return $result;
 }
Пример #3
0
 /**
  * @param array $chain
  * @param array $rpnt
  * @param bool $setGoal
  * @return void
  */
 protected static function stop($chain, $rpnt, $setGoal)
 {
     $rpnt['EMAIL'] = strtolower($rpnt['EMAIL']);
     // if mailing continue, then stop it
     $recipientDb = PostingRecipientTable::getList(array('select' => array('ID', 'ROOT_ID', 'POSTING_ID', 'STATUS', 'POSTING_STATUS' => 'POSTING.STATUS'), 'filter' => array('=EMAIL' => $rpnt['EMAIL'], '=POSTING.MAILING_ID' => $chain['MAILING_ID'], '=STATUS' => array(PostingRecipientTable::SEND_RESULT_NONE, PostingRecipientTable::SEND_RESULT_WAIT)), 'limit' => 1));
     if ($recipient = $recipientDb->fetch()) {
         // if mailing continue, then stop it and the next was riched
         $updateFields['STATUS'] = PostingRecipientTable::SEND_RESULT_DENY;
         PostingRecipientTable::update(array('ID' => $recipient['ID']), $updateFields);
         // change status of posting if all emails sent
         if (!in_array($recipient['POSTING_STATUS'], array(PostingTable::STATUS_NEW, PostingTable::STATUS_PART))) {
             $recipientCountDb = PostingRecipientTable::getList(array('select' => array('POSTING_ID'), 'filter' => array('=POSTING_ID' => $recipient['POSTING_ID'], '=STATUS' => array(PostingRecipientTable::SEND_RESULT_NONE, PostingRecipientTable::SEND_RESULT_WAIT)), 'limit' => 1));
             if (!$recipientCountDb->fetch()) {
                 PostingTable::update(array('ID' => $recipient['POSTING_ID']), array('STATUS' => PostingTable::STATUS_SENT));
             }
         }
     }
     if (!$setGoal) {
         return;
     }
     // set flag of taking the goal to last success sending
     $recipientDb = PostingRecipientTable::getList(array('select' => array('ID', 'DATE_DENY'), 'filter' => array('=EMAIL' => $rpnt['EMAIL'], '=POSTING.MAILING_ID' => $chain['MAILING_ID'], '=STATUS' => array(PostingRecipientTable::SEND_RESULT_SUCCESS)), 'order' => array('DATE_SENT' => 'DESC', 'ID' => 'DESC'), 'limit' => 1));
     if ($recipient = $recipientDb->fetch()) {
         if (empty($recipient['DATE_DENY'])) {
             PostingRecipientTable::update(array('ID' => $recipient['ID']), array('DATE_DENY' => new DateTime()));
         }
     }
 }
Пример #4
0
 /**
  * @param $id
  * @param int $timeout
  * @param int $maxMailCount
  * @return bool|string
  * @throws \Bitrix\Main\ArgumentException
  * @throws \Bitrix\Main\DB\Exception
  * @throws \Bitrix\Main\Db\SqlQueryException
  * @throws \Exception
  */
 public static function send($id, $timeout = 0, $maxMailCount = 0)
 {
     $start_time = getmicrotime();
     @set_time_limit(0);
     static::$emailSentPerIteration = 0;
     $postingDb = PostingTable::getList(array('select' => array('ID', 'STATUS', 'MAILING_ID', 'MAILING_CHAIN_ID', 'MAILING_CHAIN_REITERATE' => 'MAILING_CHAIN.REITERATE', 'MAILING_CHAIN_IS_TRIGGER' => 'MAILING_CHAIN.IS_TRIGGER'), 'filter' => array('ID' => $id, 'MAILING.ACTIVE' => 'Y', 'MAILING_CHAIN.STATUS' => MailingChainTable::STATUS_SEND)));
     $postingData = $postingDb->fetch();
     // posting not found
     if (!$postingData) {
         return static::SEND_RESULT_ERROR;
     }
     // if posting in new status, then import recipients from groups and set right status for sending
     $isInitGroupRecipients = false;
     $isChangeStatusToPart = false;
     if ($postingData["STATUS"] == PostingTable::STATUS_NEW) {
         $isInitGroupRecipients = true;
         $isChangeStatusToPart = true;
     }
     if ($postingData["STATUS"] != PostingTable::STATUS_PART && $postingData["MAILING_CHAIN_IS_TRIGGER"] == 'Y') {
         $isInitGroupRecipients = false;
         $isChangeStatusToPart = true;
     }
     if ($isInitGroupRecipients) {
         PostingTable::initGroupRecipients($postingData['ID']);
     }
     if ($isChangeStatusToPart) {
         PostingTable::update(array('ID' => $postingData['ID']), array('STATUS' => PostingTable::STATUS_PART));
         $postingData["STATUS"] = PostingTable::STATUS_PART;
     }
     // posting not in right status
     if ($postingData["STATUS"] != PostingTable::STATUS_PART) {
         return static::SEND_RESULT_ERROR;
     }
     // lock posting for exclude double parallel sending
     if (static::lockPosting($id) === false) {
         throw new \Bitrix\Main\DB\Exception(Loc::getMessage('SENDER_POSTING_MANAGER_ERR_LOCK'));
     }
     // select all recipients of posting, only not processed
     $recipientDataDb = PostingRecipientTable::getList(array('filter' => array('POSTING_ID' => $postingData['ID'], 'STATUS' => PostingRecipientTable::SEND_RESULT_NONE), 'limit' => $maxMailCount));
     while ($recipientData = $recipientDataDb->fetch()) {
         // create name from email
         $recipientEmail = $recipientData["EMAIL"];
         if (empty($recipientData["NAME"])) {
             $recipientEmailParts = explode('@', $recipientEmail);
             $recipientName = $recipientEmailParts[0];
         } else {
             $recipientName = $recipientData["NAME"];
         }
         // prepare params for send
         $sendParams = array('FIELDS' => array('EMAIL_TO' => $recipientEmail, 'NAME' => $recipientName, 'USER_ID' => $recipientData["USER_ID"], 'SENDER_CHAIN_CODE' => 'sender_chain_item_' . $postingData["MAILING_CHAIN_ID"], 'UNSUBSCRIBE_LINK' => Subscription::getLinkUnsub(array('MAILING_ID' => $postingData['MAILING_ID'], 'EMAIL' => $recipientEmail, 'RECIPIENT_ID' => $recipientData["ID"]))), 'TRACK_READ' => array('MODULE_ID' => "sender", 'FIELDS' => array('RECIPIENT_ID' => $recipientData["ID"])), 'TRACK_CLICK' => array('MODULE_ID' => "sender", 'FIELDS' => array('RECIPIENT_ID' => $recipientData["ID"]), 'URL_PARAMS' => array('bx_sender_conversion_id' => $recipientData["ID"])));
         if (is_array($recipientData['FIELDS']) && count($recipientData) > 0) {
             $sendParams['FIELDS'] = $sendParams['FIELDS'] + $recipientData['FIELDS'];
         }
         // set sending result to recipient
         $mailSendResult = static::sendInternal($postingData['MAILING_CHAIN_ID'], $sendParams);
         PostingRecipientTable::update(array('ID' => $recipientData["ID"]), array('STATUS' => $mailSendResult, 'DATE_SENT' => new Type\DateTime()));
         // send event
         $eventData = array('SEND_RESULT' => $mailSendResult == PostingRecipientTable::SEND_RESULT_SUCCESS, 'RECIPIENT' => $recipientData, 'POSTING' => $postingData);
         $event = new Event('sender', 'OnAfterPostingSendRecipient', array($eventData));
         $event->send();
         // limit executing script by time
         if ($timeout > 0 && getmicrotime() - $start_time >= $timeout) {
             break;
         }
         // increment sending statistic
         static::$emailSentPerIteration++;
     }
     //set status and delivered and error emails
     $statusList = PostingTable::getRecipientCountByStatus($id);
     if (!array_key_exists(PostingRecipientTable::SEND_RESULT_NONE, $statusList)) {
         if (array_key_exists(PostingRecipientTable::SEND_RESULT_ERROR, $statusList)) {
             $STATUS = PostingTable::STATUS_SENT_WITH_ERRORS;
         } else {
             $STATUS = PostingTable::STATUS_SENT;
         }
         $DATE = new Type\DateTime();
     } else {
         $STATUS = PostingTable::STATUS_PART;
         $DATE = null;
     }
     // unlock posting for exclude double parallel sending
     static::unlockPosting($id);
     // update status of posting
     PostingTable::update(array('ID' => $id), array('STATUS' => $STATUS, 'DATE_SENT' => $DATE));
     // return status to continue or end of sending
     if ($STATUS == PostingTable::STATUS_PART) {
         return static::SEND_RESULT_CONTINUE;
     } else {
         return static::SEND_RESULT_SENT;
     }
 }
Пример #5
0
 public static function updatePostingReadCounters($type)
 {
     $dataDb = \Bitrix\Sender\PostingRecipientTable::getList(array('select' => array('POSTING_ID', 'CNT'), 'filter' => array('=UPDATE_POSTING.COUNT_' . $type => 0, '>CNT' => 0, '=IS_' . $type => 'Y'), 'runtime' => array(new \Bitrix\Main\Entity\ReferenceField('UPDATE_POSTING', 'Bitrix\\Sender\\PostingTable', array('=this.POSTING_ID' => 'ref.ID'), array('join_type' => 'INNER')), new \Bitrix\Main\Entity\ExpressionField('CNT', 'COUNT(%s)', 'ID')), 'order' => array('CNT' => 'DESC', 'POSTING_ID' => 'ASC')));
     while ($item = $dataDb->fetch()) {
         if (self::isTimeUp()) {
             return true;
         }
         \Bitrix\Sender\PostingTable::update(array('ID' => $item['POSTING_ID']), array('COUNT_' . $type => $item['CNT']));
     }
     return false;
 }