2

I have already tried many given answers on the internet but none of them work such as I can't find below code in Subscriber.php, guess it was for Magento 1.x

$email->sendTransactional(
    Mage::getStoreConfig(self::XML_PATH_SUCCESS_EMAIL_TEMPLATE),
    Mage::getStoreConfig(self::XML_PATH_SUCCESS_EMAIL_IDENTITY),
    $this->getEmail(),
    $this->getName(),
    array('subscriber'=>$this)
);

I have also tried adding var unsubscribe or var subscriber.getUnsubscriptionLink() but none of them work.

in my footer of email I am trying to add unsubscribe url for the receivers, but how do i get in 2.4.2? Previously in 2.3.5-p1 it was working with subscribe_data.getUnsubscriptionLink() but with recent upgrade its not.

any help/suggestion please?

0

2 Answers 2

0

I had the same problem. I couldn't work out why exactly it was happening. The default {{var subscriber.getUnsubscriptionLink}} works if placed in a transactional email (eg subscriber success email) but doesn't work in a newsletter template sent by queue. Other variables work like {{var subscriber.getSubscriberEmail}}. Maybe someone with better knowledge can take a look at \Magento\Newsletter\Model\Queue to see why it's returning null.

In the end, I overrode this file in my custom module by copying the file over and adding a specific unsubscribe_link variable.

Your etc/di.xml file:

<config  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Newsletter\Model\Queue" type="VENDOR\MODULE\Model\Queue" />
</config>

In your new file VENDOR\MODULE\Model\Queue.php:

  • Update class to extend core file:
class Queue extends \Magento\Newsletter\Model\Queue implements TemplateTypesInterface
  • Update the parent construct so that all parent dependencies are declared:

      parent::__construct(
          $context,
          $registry,
          $templateFilter,
          $date,
          $templateFactory,
          $problemFactory,
          $subscriberCollectionFactory,
          $transportBuilder,
          $resource,
          $resourceCollection,
          $data
      );
    
  • Update setTemplateVars in public function sendPerSubscriber($count = 20)

          $transport = $this->_transportBuilder->setTemplateOptions(
              ['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $item->getStoreId()]
          )->setTemplateVars(
              ['subscriber' => $item, 'unsubscribe_link' => $item->getUnsubscriptionLink()]
          )->setFrom(
              ['name' => $this->getNewsletterSenderName(), 'email' => $this->getNewsletterSenderEmail()]
          )->addTo(
              $item->getSubscriberEmail(),
              $item->getSubscriberFullName()
          )->getTransport();
    

Full Queue preference file - https://pastebin.com/eW0A4GPV

Run bin/magento setup:di:compile

Update template file with new variable:

<a href="{{var unsubscribe_link}}">Unsubscribe now</a>

Previous result with {{var subscriber.getUnsubscriptionLink}}:

<a href="">Unsubscribe Now</a>

New result with {{var unsubscribe_link}}:

<a href="http://192.168.1.129/newsletter/subscriber/unsubscribe/id/524/code/ufidhevuti2vteqj2mn0xzox1fekcyl5/">Unsubscribe now</a>
10
  • can you please share the module?
    – nix
    Commented Apr 4, 2021 at 16:50
  • If you have custom module already you use you can just put it in there. Here is full Queue model override pastebin.com/eW0A4GPV - here is tutorial for creating your own basic module if you need devdocs.magento.com/videos/fundamentals/create-a-new-module
    – bernieu2
    Commented Apr 5, 2021 at 4:06
  • Doesn't work. Here is the module I created and then did s:d:c, also s:u, s:s:d, c:c && c:f. Url >> shorturl.at/emJL9
    – nix
    Commented Apr 5, 2021 at 18:58
  • Ok, so, in your di.xml you reference my copy/paste which has my folder structure still (Gem\Subscriber). It should be your module (NixDev\NewsletterUnsubscription). And in your module.xml, the name convention should be Vendor_Module so instead of NixDev/NewsletterUnsubscription it should be NixDev_NewsletterUnsubscription. After that, it should register properly. You can check modules enabled by using command bin/magento module:status and use bin/magento module:enable NixDev_NewsletterUnsubscription to enable it if it doesn't get enabled automatically
    – bernieu2
    Commented Apr 5, 2021 at 22:58
  • I have made the necessary changes now, but same issue even the module is enabled. If i send an email via Customer > Send email, or Subscribers > Email, it doesn't work. I am using {{var unsubscribe_link}} in the Footer template. but I just get text no URL to click. I have Magento 2.4.2
    – nix
    Commented Apr 6, 2021 at 9:47
0

Try This

app/code/VendoreName/ModuleName/etc

di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <preference for="Magento\Newsletter\Model\Queue" type="VendoreName\ModuleName\Model\Queue" />

    <preference for="Magento\Newsletter\Model\Template" type="VendoreName\ModuleName\Model\Template" />

</config>

app/code/VendoreName/ModuleName/Model

Queue.php

<?php

namespace VendoreName\ModuleName\Model;

class Queue extends \Magento\Newsletter\Model\Queue
{
    /**
     * 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,
                    'subscriber_data' => [
                        'unsubscription_link' => $item->getUnsubscriptionLink(),
                    ],
                ]
            )->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;
    }

}

app/code/VendoreName/ModuleName/Model

Template.php

<?php

namespace VendoreName\ModuleName\Model;

class Template extends \Magento\Newsletter\Model\Template
{

    /**
     * Retrieve template text wrapper
     *
     * @return string
     */
    public function getTemplateText()
    {
        if (!$this->getData('template_text') && !$this->getId()) {
            $this->setData(
                'template_text',
                __(
                    'Follow this link to unsubscribe <!-- This tag is for unsubscribe link  -->' .
                    '<a href="{{var subscriber_data.unsubscription_link}}">Unsubscribe' .
                    '</a>'
                )
            );
        }

        return $this->getData('template_text');
    }
}

Use in Newslatter Email Template : <a href="{{var subscriber_data.unsubscription_link}}">Unsubscribe</a>

Reference Link : https://github.com/magento/magento2/pull/33753

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