Blog Detail

23

Dec
 Adding Unsubscribe Links to Notifications with Laravel cover image

arrow_back Adding Unsubscribe Links to Notifications with Laravel

Subscribable-notifications is a package by YLS Ideas that has been designed to help you handle email unsubscribes with as little as 5 minutes of setup. After installing your notifications sent over email should now be delivered with unsubscribe links in the footer and as a mail header which email clients can present to the user for quicker unsubscribing. It can also handle resolving the unsubscribing of the user through a signed route/controller.

Installation

You can install the package via composer:

composer require ylsideas/subscribable-notifications

Optionally to make use of the built-in unsubscribing handler you can publish the application service provider. If you wish to implement your own unsubscribing process and only insert unsubscribe links into your notifications, you can forgo doing this.

php artisan vendor:publish --tag=subscriber-provider

This will create a \App\Providers\SubscriberServiceProvider class which you will need to register in config/app.php.

Usage

First, You have to implement the YlsIdeas\SubscribableNotifications\Contracts\CanUnsubscribe interface on your notifiable User model. You can also apply the YlsIdeas\SubscribableNotifications\MailSubscriber trait which will implement this for you to automatically provide signed URLs for the unsubscribe controller provided by this library.

use YlsIdeas\SubscribableNotifications\MailSubscriber;
use YlsIdeas\SubscribableNotifications\Contracts\CanUnsubscribe;

class User implements CanUnsubscribe
{
    use Notifiable, MailSubscriber;
}

Implementing your own unsubscribe links
If you wish to implement your own completely different unsubscribeLink() method you can.

use YlsIdeas\SubscribableNotifications\Contracts\CanUnsubscribe;

class User implements CanUnsubscribe
{
    use Notifiable;
    
    public function unsubscribeLink(?string $mailingList = ''): string
    {
        return URL::signedRoute(
            'sorry-to-see-you-go',
            ['subscriber' => $this, 'mailingList' => $mailingList],
            now()->addDays(1)
        );
    }
}

Implementing notifications as part of a mailing list

If you wish to apply specific mailing lists to notifications you need to implement the YlsIdeas\SubscribableNotifications\Contracts\AppliesToMailingList on those notifications. This will put two unsubscribe links into your emails generated from those notifications. One for all emails and one for only that type of email.

use YlsIdeas\SubscribableNotifications\Contracts\AppliesToMailingList;

class Welcome extends Notification implements AppliesToMailingList
{
    
    public function usesMailingList(): string
    {
        return 'weekly-updates';
    }
  
}

Using the full unsubscribing workflow

Using the App\Providers\SubscriberServiceProvider you can set up simple hooks to handle unsubscribing the user from all future emails. This package doesn’t determine how you should store that record of opting out of future emails. Instead, you provide functions in the provider which will be called. The following are just examples of what you can do.

Implementing an unsubscribe hook for a specific mailing list

This handler will be called if a user links a link through to unsubscribe from a specific mailing list.

public class SubscriberServiceProvider
{

    
    public function onUnsubscribeFromMailingList()
    {
        return function ($user, $mailingList) {
            $user->mailing_lists = $user->mailing_lists->put($mailingList, false);
            $user->save();
        };
    }
    
 
}

Implementing an unsubscribe hook for all emails

This handler will be called if the user has clicked through to the link to unsubscribe from all future emails.

public class SubscriberServiceProvider
{
    ...
    
    public function onUnsubscribeFromAllMailingLists()
    {
        return function ($user) {
            $user->unsubscribed_at = now();
            $user->save();
        };
    }
    
    ...
}

Implementing an unsubscribe response

The completion handler will be called after a user is unsubscribed, allowing you to customize where the user is redirected to or if you want to maybe show a further form even.

public class SubscriberServiceProvider
{
    
    public function onCompletion()
    {
        return function ($user, $mailingList) {
            return view('confirmation')
                ->with('alert', 'You\'re not unsubscribed');
        };
    }
    
} 

There is a lot more in this package that you should know, So if you want to dig more, You can visit its complete documentation on Github.

Published at : 23-12-2021

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project