_templateFilter = $templateFilter; $this->_date = $date; $this->_templateFactory = $templateFactory; $this->_problemFactory = $problemFactory; $this->_subscribersCollection = $subscriberCollectionFactory->create(); $this->_transportBuilder = $transportBuilder; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $this->timezone = $timezone ?: $objectManager->get(TimezoneInterface::class); $this->utcConverter = $utcConverter ?? $objectManager->get(LocalizedDateToUtcConverterInterface::class); } /** * Initialize resource model * * @return void */ protected function _construct() { parent::_construct(); $this->_init(\Magento\Newsletter\Model\ResourceModel\Queue::class); } /** * Return: is this queue newly created or not. * * @return boolean */ public function isNew() { return $this->getQueueStatus() === null; } /** * Set $_data['queue_start'] based on string from backend, which based on locale. * * @param string|null $startAt start date of the mailing queue * @return $this */ public function setQueueStartAtByString($startAt) { if ($startAt === null || $startAt == '') { $this->setQueueStartAt(null); } else { $this->setQueueStartAt($this->utcConverter->convertLocalizedDateToUtc($startAt)); } return $this; } /** * Send messages to subscribers for this queue * * @param int $count * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function sendPerSubscriber($count = 20) { if ($this->getQueueStatus() != self::STATUS_SENDING && ($this->getQueueStatus() != self::STATUS_NEVER && $this->getQueueStartAt()) ) { return $this; } if (!$this->_subscribersCollection->getQueueJoinedFlag()) { $this->_subscribersCollection->useQueue($this); } if ($this->_subscribersCollection->getSize() == 0) { $this->_finishQueue(); return $this; } $collection = $this->_subscribersCollection->useOnlyUnsent()->showCustomerInfo()->setPageSize( $count )->setCurPage( 1 )->load(); $this->_transportBuilder->setTemplateData( [ 'template_subject' => $this->getNewsletterSubject(), 'template_text' => $this->getNewsletterText(), 'template_styles' => $this->getNewsletterStyles(), 'template_filter' => $this->_templateFilter, 'template_type' => self::TYPE_HTML, ] ); /** @var \Magento\Newsletter\Model\Subscriber $item */ foreach ($collection->getItems() as $item) { $transport = $this->_transportBuilder->setTemplateOptions( ['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $item->getStoreId()] )->setTemplateVars( ['subscriber' => $item] )->setFrom( ['name' => $this->getNewsletterSenderName(), 'email' => $this->getNewsletterSenderEmail()] )->addTo( $item->getSubscriberEmail(), $item->getSubscriberFullName() )->getTransport(); try { $transport->sendMessage(); } catch (\Magento\Framework\Exception\MailException $e) { /** @var \Magento\Newsletter\Model\Problem $problem */ $problem = $this->_problemFactory->create(); $problem->addSubscriberData($item); $problem->addQueueData($this); $problem->addErrorData($e); $problem->save(); } $item->received($this); } if (count($collection->getItems()) < $count - 1 || count($collection->getItems()) == 0) { $this->_finishQueue(); } return $this; } /** * Finish queue: set status SENT and update finish date * * @return $this */ protected function _finishQueue() { $this->setQueueFinishAt($this->_date->gmtDate()); $this->setQueueStatus(self::STATUS_SENT); $this->save(); return $this; } /** * Getter data for saving * * @return array */ public function getDataForSave() { $data = []; $data['template_id'] = $this->getTemplateId(); $data['queue_status'] = $this->getQueueStatus(); $data['queue_start_at'] = $this->getQueueStartAt(); $data['queue_finish_at'] = $this->getQueueFinishAt(); return $data; } /** * Add subscribers to queue. * * @param array $subscriberIds * @return $this */ public function addSubscribersToQueue(array $subscriberIds) { $this->_getResource()->addSubscribersToQueue($this, $subscriberIds); return $this; } /** * Setter for save stores flag. * * @param boolean|integer|string $value * @return $this */ public function setSaveStoresFlag($value) { $this->_saveStoresFlag = (bool)$value; return $this; } /** * Getter for save stores flag. * * @return boolean * @SuppressWarnings(PHPMD.BooleanGetMethodName) */ public function getSaveStoresFlag() { return $this->_saveStoresFlag; } /** * Setter for stores of queue. * * @param array $storesIds * @return $this */ public function setStores(array $storesIds) { $this->setSaveStoresFlag(true); $this->_stores = $storesIds; return $this; } /** * Getter for stores of queue. * * @return array */ public function getStores() { if (!$this->_stores) { $this->_stores = $this->_getResource()->getStores($this); } return $this->_stores; } /** * Retrieve Newsletter Template object * * @return \Magento\Newsletter\Model\Template */ public function getTemplate() { if ($this->_template === null) { $this->_template = $this->_templateFactory->create()->load($this->getTemplateId()); } return $this->_template; } /** * Return true if template type eq text * * @return boolean */ public function isPlain() { return $this->getType() == self::TYPE_TEXT; } /** * Getter for template type * * @return int|string */ public function getType() { return $this->getNewsletterType(); } }