Problem/Motivation

It is required to send some emails to a specific user before his role expires. For example:

  • sending one warning e-mail 1 month before the expiration
  • one warning e-mail 10 days before the expiration
  • e-mail with the notification that the role has expired.

Proposed resolution

Create a new submodule to implement the new feature. Use the "user.data" service to avoid sending repeated notifications to users.

Remaining tasks

  • Fix all pipeline errors: cspell, phpcs and phpstan
  • Compatibility with Drupal 11
  • Make the new submodule's configuration form translatable
  • Fix configuration language handling
  • Complete the renaming from points to methods
  • Add default config file
  • Add info of the new submodule into the main README
  • Fix error: notification emails are not send in user's preferred language, just in site's default language
  • More tests
  • Review the MR

User interface changes

The new submodule will have a new UI that will allow to configure the notifications.

API changes

None.

Data model changes

None.

Original report by Skin

Hello, I`d like to know if there is a way to send some emails to a specific user before his role expires: for example I`d like to send one warning e-mail 1 month before the expiration, one warning e-mail 10 days before the expiration and a final e-mail with the notification that the role has expired.

Before starting to study Rules module ( I never used it ) i`d like to ask if it is possible to do this with the Rules 8.x module.

May be someone can poin`t me in the right direction?

thanks.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Skin created an issue. See original summary.

rcodina’s picture

Status: Active » Postponed

This would be a new feature: new events should be created and dispatched on the right moment. Check out the current RoleExpiresEvent to find an example (it's dispatched inside role_expire_cron function). I also recommend you to check out Rules module source code.

skin’s picture

Thank you!

rcodina’s picture

Status: Postponed » Closed (works as designed)
chike’s picture

First thank you for your work, definitely this feature would be handy and is almost expected to be on this module. Warning messages like one month, 10 days, 5 days and on expiry day (days to be configurable) should be an integral feature of this module IMO. Use cases like in subscriptions, this will be a required functionality.

If I could I would have submitted a patch.

ilya.no’s picture

Version: 8.x-1.x-dev » 2.0.1
Component: Miscellaneous » Code
Status: Closed (works as designed) » Needs review
StatusFileSize
new21.99 KB

Hello all,
I need this feature and agree, that it can be placed here, so I've implemented it as separate sub-module. For main module I've only updated event class and cron function to react on expiration event. For notification event I've implemented similar, but separate event. Both events are processed by one subscriber, which sends actual emails. There is also cron function in new module to dispatch new type of events. Notifications are managed on `admin/config/people/role-expire/notification` page. Each notification has email body and subject, checkboxes for possible roles, assigned to this notification and field for number of days before expiration date when notification should be sent. In order to prevent sending same notification several times, I use `user.data` service and keep information there.

voviko’s picture

StatusFileSize
new22.03 KB

TypeError : Argument 1 passed to Egulias\EmailValidator\EmailValidator::isValid() must be of the type string, null given
change
$message['headers']['Sender'] = \Drupal::config('system.site')->get('mail');

voviko’s picture

StatusFileSize
new645 bytes
rcodina’s picture

Version: 2.0.1 » 2.x-dev
rcodina’s picture

Category: Support request » Feature request
vacho’s picture

StatusFileSize
new22.3 KB
new3.52 KB

The last patch have the issue that send the email notification always in default website language. This patch let to send the current language.

vacho’s picture

StatusFileSize
new22.3 KB

This patch fixes a problem to load the configuration.

vacho’s picture

StatusFileSize
new22.42 KB

Sorry, forget the patch 12.

vacho’s picture

StatusFileSize
new1.45 KB
heddn’s picture

Status: Needs review » Needs work

Actual functionality here works very well. No concerns with that. My nits are mainly about making this a better solution. The main one being around naming, which I recognize is a hard thing. Bumping back to NW.

  1. +++ b/modules/role_expire_notification/config/schema/role_expire_notification.schema.yml
    @@ -0,0 +1,32 @@
    +    role_expire_notification_points:
    

    Calling this a notification point doesn't make as much sense as calling it a notification method. Let's rename this here and elsewhere.

  2. +++ b/modules/role_expire_notification/role_expire_notification.info.yml
    @@ -0,0 +1,8 @@
    +core_version_requirement: ^8.8 || ^9
    

    Let's change this to also include Drupal 10.

heddn’s picture

Status: Needs work » Needs review
StatusFileSize
new26.5 KB
new10.74 KB

Here's a fix for #15, along with a test of the new module. It also fixes several D10 incompatible issues. Mainly event dispatcher stuffs. It would be nice to have tests setup again as automated tests aren't executing right now.

rcodina’s picture

@heddn I enabled testing.

heddn’s picture

9.5 and 10.1.x both passed green.

heddn’s picture

StatusFileSize
new26.5 KB

Re-rolled for 2.x. It was a trivial re-roll and the diff stats are identical.

heddn’s picture

Version: 2.x-dev » 3.x-dev
StatusFileSize
new26.27 KB

And here's a 3.x patch.

chike’s picture

@heddn none of your patches applies on Drupal 9. I am using Drupal 9.5.10.

rcodina’s picture

@heddn @chike The patch cleanly applies to 3.x but this patch has to be split:

1. Test fixes: See #3378674: Fix failing tests
2. New feature without test fixes

Once ready, this feature will only be committed to 3.x branch.

rcodina’s picture

Status: Needs review » Needs work
rcodina’s picture

Status: Needs work » Needs review
StatusFileSize
new25.11 KB
new1.02 KB

Removed changes that overlap with #3378674: Fix failing tests

elvin - albania drupal developer’s picture

any movement on this? it would be great to have this feature enabled or this module integrated with ECA for more power handling

rcodina’s picture

Once drupal 11 version is released (new 4.x branch), a new MR should be created from current patch.

rcodina’s picture

Status: Needs review » Needs work
rcodina’s picture

Priority: Minor » Normal
rcodina’s picture

Version: 3.x-dev » 4.x-dev

rcodina’s picture

Issue summary: View changes

Added latest patch to a new issue fork and created a MR which targets 4.x branch. Please, don't upload more patches, just use the new issue fork.

rcodina’s picture

Issue summary: View changes
rcodina’s picture

Priority: Normal » Major
Issue summary: View changes
rcodina’s picture

There are only a few tasks left. Thanks for your contributions!

rcodina’s picture

Title: Send email before expiring date » Add a new configurable email notification system
rcodina’s picture

Issue summary: View changes
anybody’s picture

#3362875: Provide ECA integration would also be a good and more flexible alternative maybe?

rcodina’s picture

@anybody I think both can live together. This feature it's already implemented, we should not force users to install additional modules if not necessary.

elvin - albania drupal developer’s picture

any movement on this? Would really be helpful to have email notifications

rcodina’s picture

Issue summary: View changes

@elvin - albania drupal developer If you want to use it now you can patch the module via composer patching. It's fully working and it's multilingual. As you can see in the description and recent commits/messages, a lot of work has been done since your previous comment.

elvin - albania drupal developer’s picture

Thank you! Testing it right now and will report on anything i may find!

elvin - albania drupal developer’s picture

Thank you! Testing it right now and will report on anything i may find!

_tarik_’s picture

I’ve reviewed the changes in this task to better understand how the feature is intended to work and what adjustments might be beneficial. Based on this, I have several suggestions:

1. The module allows configuring an email notification for one or more roles, but it does not provide an option to set a default notification for all roles. This means administrators must always check whether every role is explicitly selected. To improve this, we could either introduce a setting that controls a default notification for all roles or add fallback behavior to the roles selection list—for example, if no role is selected, the rule would automatically apply to all roles. What do you think?

2. The notification subforms are currently wrapped in a tag. If a site uses many notifications, this could make the page harder to navigate. Replacing the with might make the interface more manageable.

3. The notification subforms currently use auto-generated titles such as “Method #1.” It would be more intuitive to generate titles based on the roles to which the notification applies, or alternatively, provide a field that lets administrators define their own titles. This would make configurations easier to distinguish.

Please let me know what you think about the described changes, and I'll be ready to finish the issue.