responsiv / campaign-plugin

[PREMIUM] Send professional campaign messages to your subscribers.
http://octobercms.com/plugin/responsiv-campaign
5 stars 2 forks source link

Exclude email addresses from a specific campaign #74

Closed chocolata closed 1 year ago

chocolata commented 2 years ago

Hi,

Loving the Campaign Plugin. I have a small suggestion though.

In prominent email service providers like MailChimp we have the option to exclude a list of email addresses for a specific campaign. This is very useful for example in the context of an upcoming event, when we only want to reach the recipients that haven't signed up for the event yet.

I've seen we can dynamically add a list of subscribers. It would be great if we could do the opposite... => exclude the recipients that appear in a specific external list (in my case those email addresses are stored in a separate plugin).

Is this a reasonable request?

Thanks in advance.

chocolata commented 1 year ago

Hi, just wanted to share how I solved this using recipient groups. I've created a custom plugin, and in the boot method of Plugin.php I did the following:

    Event::listen('responsiv.campaign.listRecipientGroups', function() {
        return [
            'event-all-registered' => 'All registered people of the event',
            'event-not-registered' => 'Not (yet) registered people of the event',
        ];
    });

    Event::listen('responsiv.campaign.getRecipientsData', function($type) {
        if ($type == 'event-all-registered') {

            $recipients = [];
            $guests = Guest::all();

            // Loop over all the guests in the system
            foreach ($guests as $guest) {
                if(!empty($guest->email)) {
                    $recipients[$guest->email] = ['first_name' => $guest->first_name, 'last_name' => $guest->last_name];
                }
            }
            return $recipients;

        }

        if ($type == 'event-not-registered') {

            $exclude = [];
            $recipients = [];

            // Create a list of excludable emails
            $guests = Guest::all();
            foreach ($guests as $guest) {
                if(!empty($guest->email)) {
                    array_push($exclude,$guest->email);
                }
            }

            // Find all lists that have "event" in its code field
            $subscriberlists = SubscriberList::where('code','LIKE','%event%')->get();
            foreach ($subscriberlists as $list) {
                foreach ($list->subscribers as $subscriber) {

                    // Exclude all emails that have already signed up for the event
                    if(!in_array($subscriber->email,$exclude) && is_null($subscriber->unsubscribed_at)) {
                        $recipients[$subscriber->email] = ['first_name' => $subscriber->first_name, 'last_name' => $subscriber->last_name];
                    }

                }

            }

            return $recipients;
        }
    });

The script above does two things:

  1. It gets all the people that are registered to the event (my custom model Guest) and puts them in a list. Great for things like sending out practical information before the event.
  2. It makes a list of all the people from all the lists that "event" in the list code, and it excluded the people that have already signed up for the event (my custom model Guest). Great for mails like "Have you signed up for our event yet?".

This solves the issue for me at the moment. It might still be useful to have a field in which we could paste emails to exclude, but I suppose this might be beyond the scope of the current plugin.

Hope this might be of some use.