29 return static::$error;
38 return Runtime\ReiteratedJob::getAgentName();
48 return Runtime\SenderJob::getAgentName($mailingChainId);
57 public static function actualizeAgent($mailingId =
null, $mailingChainId =
null)
60 ->withCampaignId($mailingId)
61 ->withLetterId($mailingChainId)
69 $event =
new Main\Event(
'sender',
'onBeforeChainSend', [
'LETTER_ID' => $letterId]);
71 foreach ($event->getResults() as $eventResult)
74 $eventResult->getType() === Main\EventResult::ERROR
75 || $eventResult->getParameters()
76 && isset($eventResult->getParameters()[
'ALLOW_SEND'])
77 && $eventResult->getParameters()[
'ALLOW_SEND'] ===
false
96 public static function chainSend($letterId)
98 static::$error =
null;
100 $letter = Model\LetterTable::getRowById($letterId);
101 if($letter && $letter[
'STATUS'] === Model\LetterTable::STATUS_PLAN)
103 $updateResult = Model\LetterTable::update($letterId, array(
'STATUS' => Model\LetterTable::STATUS_SEND));
104 if ($updateResult->isSuccess())
106 $letter = Model\LetterTable::getRowById($letterId);
109 if(!$letter || !in_array($letter[
'STATUS'], [
110 Model\LetterTable::STATUS_SEND
116 if(!static::checkOnBeforeChainSend($letterId))
118 return Runtime\SenderJob::getAgentName($letterId);
121 $postingSendStatus =
'';
122 if(!empty($letter[
'POSTING_ID']))
127 $letter[
'POSTING_ID'],
128 Runtime\Env::getJobExecutionTimeout(),
129 Runtime\Env::getJobExecutionItemLimit()
135 AddMessage2Log(
'Exception in mailing send. PostingId: ' . $letter[
'POSTING_ID'],
'sender');
144 return Runtime\SenderJob::getAgentName($letterId);
150 Model\LetterTable::update($letterId, array(
'STATUS' => Model\LetterTable::STATUS_WAIT));
160 return Runtime\SenderJob::getAgentName($letterId);
163 if ($letter[
'REITERATE'] !==
'Y')
165 Model\LetterTable::update($letterId, array(
'STATUS' => Model\LetterTable::STATUS_END));
169 $isNeedUpdate =
true;
170 if($letter[
'IS_TRIGGER'] ==
'Y')
172 $postingDb = PostingTable::getList(array(
173 'select' => array(
'ID',
'DATE_CREATE'),
176 'MAILING_CHAIN_ID' => $letter[
'ID']
178 'order' => array(
'DATE_CREATE' =>
'ASC'),
181 if($posting = $postingDb->fetch())
183 $dateCreate = $posting[
'DATE_CREATE'];
186 'STATUS' => Model\LetterTable::STATUS_SEND,
187 'AUTO_SEND_TIME' => $dateCreate ? $dateCreate->add($letter[
'TIME_SHIFT'].
' minutes') :
null,
188 'POSTING_ID' => $posting[
'ID']
190 Model\LetterTable::update($letterId, $updateFields);
191 $isNeedUpdate =
false;
197 $letterInstance =
new Entity\Letter();
198 $letterInstance->loadByArray($letter);
199 $letterInstance->wait();
203 'MAILING_CHAIN' => $letter
205 $event = new \Bitrix\Main\Event(
'sender',
'OnAfterMailingChainSend', array($eventData));
219 if(\COption::GetOptionString(
"sender",
"auto_method") !==
'cron')
222 $mailingChainDb = MailingChainTable::getList(array(
223 'select' => array(
'ID'),
229 '=MAILING.ACTIVE' =>
'Y',
234 while ($mailingChain = $mailingChainDb->fetch())
236 $event =
new Event(
'sender',
'onBeforeMailingChainSend', [
237 'chain' => $mailingChain,
240 foreach ($event->getResults() as $eventResult)
242 if ($eventResult->getType() === EventResult::ERROR)
248 static::chainSend($mailingChain[
'ID']);
261 $isAgentExecInSetting = !Runtime\Env::isReiteratedJobCron();
262 if(($isAgentExec && !$isAgentExecInSetting) || (!$isAgentExec && $isAgentExecInSetting))
267 $dateTodayPhp = new \DateTime();
268 $datetimeToday = Type\DateTime::createFromPhp(clone $dateTodayPhp);
269 $dateToday = clone $dateTodayPhp;
270 $dateToday = Type\Date::createFromPhp($dateToday->setTime(0,0,0));
271 $dateTomorrow = clone $dateTodayPhp;
272 $dateTomorrow = Type\Date::createFromPhp($dateTomorrow->setTime(0,0,0))->add(
'1 DAY');
273 $arDateFilter = array($dateToday, $dateTomorrow);
275 $chainDb = MailingChainTable::getList(array(
277 'ID',
'LAST_EXECUTED',
'POSTING_ID',
278 'MONTHS_OF_YEAR',
'DAYS_OF_MONTH',
'DAYS_OF_WEEK',
'TIMES_OF_DAY'
282 '=MAILING.ACTIVE' =>
'Y',
283 '=IS_TRIGGER' =>
'N',
288 while($arMailingChain = $chainDb->fetch())
290 $lastExecuted = $arMailingChain[
'LAST_EXECUTED'];
292 if($lastExecuted && $lastExecuted->getTimestamp() >= $dateToday->getTimestamp())
297 $event =
new Event(
'sender',
'onBeforeMailingPeriodChainSend', [
298 'chain' => $arMailingChain,
301 foreach ($event->getResults() as $eventResult)
303 if ($eventResult->getType() === EventResult::ERROR)
309 $timeOfExecute = static::getDateExecute(
311 $arMailingChain[
"DAYS_OF_MONTH"],
312 $arMailingChain[
"DAYS_OF_WEEK"],
313 $arMailingChain[
"TIMES_OF_DAY"],
314 $arMailingChain[
"MONTHS_OF_YEAR"]
319 $arUpdateMailChain = array(
'LAST_EXECUTED' => $datetimeToday);
321 $postingDb = PostingTable::getList(array(
322 'select' => array(
'ID'),
324 '=MAILING_CHAIN_ID' => $arMailingChain[
'ID'],
325 '><DATE_CREATE' => $arDateFilter
328 $arPosting = $postingDb->fetch();
335 $postingId = $arPosting[
'ID'];
336 $arUpdateMailChain[
'POSTING_ID'] = $postingId;
343 $arUpdateMailChain[
'AUTO_SEND_TIME'] = Type\DateTime::createFromPhp($timeOfExecute);
346 $result = Model\LetterTable::update($arMailingChain[
'ID'], $arUpdateMailChain);
347 if (!$result->isSuccess())
368 ?
string $daysOfMonth =
'',
369 ?
string $dayOfWeek =
'',
370 ?
string $timesOfDay =
'',
371 ?
string $monthsOfYear =
''
374 $timeOfExecute =
null;
382 $arTime = array(0,0);
384 $day = $date->
format(
'j');
385 $week = $date->
format(
'N');
386 $month = $date->
format(
'n');
388 if( (!$arDay || in_array($day, $arDay)) && (!$arWeek || in_array($week, $arWeek)) && (!$months || in_array($month, $months)) )
389 $timeOfExecute = $date->
setTime($arTime[0], $arTime[1]);
391 return $timeOfExecute;
setTime($hour, $minute, $second=0, $microseconds=0)
static send($id, $timeout=0, $maxMailCount=0)
const SEND_RESULT_CONTINUE
const SEND_RESULT_WAITING_RECIPIENT
static hasUnprocessed($postingId, $threadId=null)
static initGroupRecipients($postingId, $checkDuplicate=true)