0

I wanted to add a unsubscribe link to the bottom of the newsletter. the magento default

{{var subscriber.getUnsubscriptionLink()}}">{{var subscriber.getUnsubscriptionLink()}}

It generates the link

https://example.com/nl/newsletter/subscriber/unsubscribe/

Which redirects to

https://example.com/nl/newsletter/subscribed#.WR3C02grL4Y

And the user is not unsubscribed from the newsletter.

5 Answers 5

1

I never use Magento to send newsletter, you can add this at the bottom of your email template and it should work

<a href="{{var subscriber.getUnsubscriptionLink()}}">
{{var subscriber.getUnsubscriptionLink()}}
</a>
4
0

I had this problem too with Magento 2.4.2. This solution fixed it here: https://github.com/magento/magento2/pull/33753/commits/dfe2a283b227f20730ecef7a605c8cfd3f1e1897

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

0

Try this code [Magento 2.4.5-p1]

Only update your template code like the below:

<p><a href="{{var subscriber_data.confirmation_link}}">{{var subscriber_data.confirmation_link}}</a></p>

Please clear the cache after updating the template.

0

This is a Default Magento Issue in some of the versions.

{{var subscriber.getUnsubscriptionLink()}}">{{var subscriber.getUnsubscriptionLink()}} doesn't return any unsubscribe link.

In Order to get our way around, passing the unsubscribe link in a explicit way. By Overriding the Vendor/Magento/Module-Newsletter/Model/Queue.php and passing the unsubscribe link to the Collection Item of the sendPerSubscriber() method.

$item->setData('yourCustomVariable', $item->getUnsubscriptionLink());

Create Newsletter template in the Admin with {{var subscriber.yourCustomVariable}}

Your Answer

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

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