New feature on RainLab.User is really perfect, because before any kind of email is sent to user, it is check, if user has blocked specific type of template
known as mail template
. But, unfortunately, for now, there are only one checkbox in User profile, and, of course, only on backend.
Block all outgoing communication
This feature allows admins, to block every outgoing email to be sent, but, in real world, there may be something more precise, right? For example, different types of emails, like newsletters, system messages, hot topic alerts, or what came in your mind.
Not easy, but still possieble
For client of mine, last days i found out, how this can be implemented, because there are multiple choices and paths to achieve specific results. Small peek-a-boo from user profile settings for notifications.
As you can see, on this image are multiple “switches” belonging to specific type of newsletter. Because i prepared sending newsletters from custom made plugin, there are still only 5 different email templates, then i can block specific template.
Deep look into it
First switch is main “Block all outgoing communication” like on backend, but managable with user. This switch calls MailBlocker::blockAll(Auth::getUser())
. On background its create a wildcard block, then, each time, when is used Mail::send
… User plugin listenning for mailer.prepareSend
event. Then is checked MailBlocker::filterMessage($view, $message);
what means, there is sent and template code and whole message object as is.
To separate correct blocking
for specific types of newsletters, there must be specific template code. From default october funcionality, each email based on mail templates, is separated email, right, then you need to create for example 3 or 5 email templates. Mine was newsletter.empty
as default, for each occasion, then newsletter.newCourse
, newsletter.saleAlert
, 'newsletter.feedbackand
system`.
On plugin admin selects specific type of newsletter and fill out specific fields for selected kind of newsletter. For example, when is saleAlert, then admin can pick product to be promoted and template was prepared to build whole body. When admin uses empty
template, then whole newsletter body is managed by custom templates. On this i will be glad, when snippets will work, but, unfortunately, not.
Plugin in action can see https://youtu.be/WwGkYIvrNEY …
Final words
It will be perfect, if there is some kind of easy way, how to manage this little things, but, from perspective of octobercms mail templates and funcionality right now, if there will be a list based on email templates, this can be really big NO. When each newsletter
or mail is sent from mail templates, and some developers uses this without any other way… uff.
But, of course… Share some “love” and fire some comments and toughts.
Thanks