1

Is there a Magento 2 function like loadByCode for admin created Newsletter templates?

If not, how are you supposed to retrieve newsletter created in admin?

1 Answer 1

0

This method is not available. So try following way:

Inject using DI:

public function __construct(
    \Magento\Newsletter\Model\TemplateFactory $newsletterFactory
) {
    $this->newsletterFactory = $newsletterFactory;
}

Now use following way:

$template_code = 'abc';
$newsletter = $this->newsletterFactory->create()->load($template_code, 'template_code');
print_r($newsletter->getData());

Another Way

Inject using DI:

public function __construct(
    \Magento\Newsletter\Model\ResourceModel\Template\CollectionFactory $newsletterCollectionFactory
) {
    $this->newsletterCollectionFactory = $newsletterCollectionFactory;

}

Now use following way:

$template_code = 'abc';
$newsletterCollection = $this->newsletterCollectionFactory->create()->addFieldToFilter('template_code', $template_code);
foreach ($newsletterCollection as $newsletter) {
    print_r($newsletter->getData());
}

[Update]

how do you actually send the newsletter template after loading it?

$template_code = 'abc';
$newsletter = $this->newsletterFactory->create()->load($template_code, 'template_code');

$this->transportBuilder->setTemplateData(
    [
        'template_subject' => $newsletter->getTemplateSubject(),
        'template_text' => $this->getTemplateText(),
        'template_styles' => $this->getTemplateStyles(),
        'template_filter' => $this->templateFilter,
        'template_type' => 2, // 2 for html
    ]
);

/* Magento\Newsletter\Model\ResourceModel\Subscriber\CollectionFactory*/
$collection = $this->subscriberCollectionFactory->create()->useOnlyUnsent()->showCustomerInfo()->setPageSize(
    20
)->setCurPage(
    1
)->load();

/** @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' => 'sent from name', 'email' => '[email protected]']
    )->addTo(
        $item->getSubscriberEmail(),
        $item->getSubscriberFullName()
    )->getTransport();

    try {
        $transport->sendMessage();
    } catch (\Magento\Framework\Exception\MailException $e) {

    }
}

See sample code, how default code send email

6
  • How do you set the $newletter object to send the email? $transport = $this->_transportBuilder ->setTemplateIdentifier('sendfriend_email_template') ->setTemplateOptions(['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $this->_storeManager->getStore()->getId()]) //->setTemplateVars($emailTempVariables) ->setTemplateVars(['data' => $postObject]) ->setFrom($sender) ->addTo($email, $name) ->setReplyTo($senderEmail) ->getTransport(); $transport->sendMessage();
    – William
    Jan 27, 2017 at 15:58
  • Which email you set $newletter object?
    – Sohel Rana
    Jan 27, 2017 at 16:01
  • I meant after you get the $newsletter from the collection in your example, how do you send it?
    – William
    Jan 27, 2017 at 16:19
  • So you want to fetch all template and send to, right?
    – Sohel Rana
    Jan 27, 2017 at 16:43
  • Right, how do you actually send the newsletter template after loading it?
    – William
    Jan 27, 2017 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.