How can I change unsubscribe url in the List-Unsubscribe header

kenxero25

New Member
Hello, I'm trying to convert all unsubscribe links to "one-click". I am aware of [DIRECT_UNSUBSCRIBE_URL] and already using it in the email template/content. It works great.

However I found out that the List-Unsubscribe url value is not the direct url. It might be [UNSUBSCRIBE_URL]. Is there a way to change that List-Unsubscribe header to use the direct link instead? I don't see a way using the backend/dashboard.

e.g.
[DIRECT_UNSUBSCRIBE_URL]: example.com/index.php/campaigns/am791ab0cd8ef/track-url/ml4516em3we1d/6118fb88e47d416fb6d395e788501f744a5fd4f0
List-Unsubscribe header url is showing (not the direct link above): example.com/lists/8ef928fpfv0a1/unsubscribe/ml4516em3we1d/6118fb88e47d416fb6d395e788501f744a5fd4f0?source=email-client-unsubscribe-button

Thank you very much for your time.
 
@kenxero25 - you got me on my way out for this week, but I wanted to try and help, even if, it might not work, sorry...
You can try to create a file called init-custom.php in the /apps/ directory and place this code in it:
PHP:
<?php
    
hooks()->addFilter('console_command_send_campaigns_before_send_to_subscriber', function($emailParams, $campaign, $subscriber, $customer, $server) {

    foreach ($emailParams['headers'] as $index => $header) {

        // not sure if this is the right move, but I assume it is needed with direct unsubscribe
        if ($header['name'] === 'List-Unsubscribe-Post') {
            unset($emailParams['headers'][$index]);
            continue;
        }

        // skip any other header except List-Unsubscribe
        if ($header['name'] !== 'List-Unsubscribe') {
            continue;
        }

        /** @var OptionUrl $optionUrl */
        $optionUrl = container()->get(OptionUrl::class);

        $listUnsubscribeHeaderValue = $optionUrl->getFrontendUrl('lists/' . $campaign->list->list_uid . '/unsubscribe/' . $subscriber->subscriber_uid . '/' . $campaign->campaign_uid . '/unsubscribe-direct?source=email-client-unsubscribe-button');
        $listUnsubscribeHeaderValue = '<' . $listUnsubscribeHeaderValue . '>';

        $emailParams['headers'][$index] = [
            'name' => 'List-Unsubscribe',     
            'value' => $listUnsubscribeHeaderValue
        ];

    }

    return $emailParams;
});
This code will hook in the send-campaigns process, and will replace the List-Unsubscribe header, which by default contains a url and an email address, with just a url, which is the direct unsubscribe url.

I don't know exactly if having a direct unsubscribe url is a good idea, at least not a good idea if spam filters will follow that link and trigger the unsubscribe process, but that's something you will find during your tests.

Please note that I haven't tested the code above yet, so in case you get any error, undo it and let me know, I'll have something working for you on Monday.

Best of luck ;)
 
Back
Top