1

The authenticated users on my Drupal website have "Entry date" in their profile (= date when they registered). I want to send out notifications to me (the admin) when a user has been registered on my website since more than 5, 10 or 15 years. So each time a user surpasses for example 5 years of registration time, I get an email notification 1 month in advance.

How can I achieve this?

1
  • @Pierre.Vriens Its D7 under Windows
    – Vasmir
    Commented Feb 6, 2017 at 7:45

2 Answers 2

4

Use the Rules module together with Rules Scheduler (a sub-module of it), as explained in the steps below. These steps are based on a date field which is added to the user profile, so that for each user you can indicate an appropriate date.

Step 1: Create a field to store the reminder date

Similar to that "Entry date" field you already have, add another field to the "user" entity with machine name (say) field_next_reminder. Allow only 1 date (to store the date of the "next" reminder), I don't see any reason why you'd want to make it required (it doesn't matter in the context of this answer). And set its initial value to "5 years after the entry date", using the same procedure you have in place already for that "Entry date".

Step 2: Create a Rules "Component"

Here is the Rules Component to be created (in Rules export format, just import it in your own environment via copy-paste, which you can do using the "Import rule" link located at admin/config/workflow/rules):

{ "rules_send_email_notification" : {
    "LABEL" : "Send eMail notification",
    "PLUGIN" : "action set",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : { "user_to_be_reminded" : { "label" : "User to be reminded", "type" : "user" } },
    "ACTION SET" : [
      { "mail" : {
          "to" : "[site:mail]" ,
          "subject" : "Time flies ...",
          "message" : "This is a reminder about another 5th anniversary for user with eMail id [user-to-be-reminded:mail] on [user-to-be-reminded:field_next_reminder] ...",
          "language" : [ "" ]
        }
      }
    ]
  }
}

Obviously, the Subject and Content of the eMail may need review / tuning, while you might also want to use some other To eMail ID.

Step 3: Create a "Rule" using the Rules Component

Here is the Rule to be created, in Rules export format (just import it in your own environment via copy-paste, which you can do using the "Import rule" link located at admin/config/workflow/rules):

{ "rules_on_new_user_registration" : {
    "LABEL" : "On new user registration",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "rules_scheduler" ],
    "ON" : { "user_insert" : [], "user_update" : [] },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "account" ], "field" : "field_next_reminder" } }
    ],
    "DO" : [
      { "schedule_delete" : {
          "component" : "rules_send_email_notification",
          "task" : "[account:name]"
        }
      },
      { "schedule" : {
          "component" : "rules_send_email_notification",
          "date" : {
            "select" : "account:field-next-reminder",
            "date_offset" : { "value" : 2592000 }
          },
          "identifier" : "[account:name]",
          "param_user_to_be_reminded" : [ "account" ]
        }
      }
    ]
  }
}

Note that the above rule refers to the Rules Component from the previous step.

Also note the machine name of field_next_reminder in this rule, which is the field's machine name created in Step 1 (if in step 1 you used another machine name, then also replace the machine name field_next_reminder in this rule with the machine name of the field created in Step 1 (if you don't have such field yet, or the field names don't match, then the import of this rule might not work).

The schedule_delete part of this rule ensures that previously "scheduled" reminders are removed when updating the data field (so that it only triggers 1 reminder, and with the most recent date).

Step 4: Add 5 more years to the reminder date

With what's above you should receive such reminder eMails after 5 years. But you also want that to happen after 5 (or 10) years later on (for 10 years or 15 years).

Probably the easiest way to do so, is to have some mechanism in place so that around the time you actually perform some action (to process your reminder), you also add 5 more years to the field_next_reminder. In doing so, the above magic will get triggered again (via that very same rule).

You can apply such update manually (don't forget it though). Or (my preference) you could use the Flag module to implement a designated (global) flag to flag users. And combine this with an extra custom rule with these specs:

  • Rules Event: User gets flagged (with the designated flag).

  • Rules Actions:

    • Do whatever you want to happen to actually send congratulations (or anything like that?) to that user. E.g sending an appropriate eMail, grant user points, grant a special user badge (yearling?).
    • Update the field_next_reminder (using "set a data value") to add 5 more years for your next reminder.

True, this would be a minor extra effort to get it implemented (it may take up to 5 minutes, say 15 at most ...). But the ROI of doing so would be that authorized users would only have to flag those yearling-type of users (somewhere after they receive the reminder eMail ... in which you could even include a relevant hyperlink to make the magic happen). Even better: you could use this flagging facility to start nagging yourself to not forget to flag that user ... (logic: reminder date is below 30 days, and user is not yet flagged).

Step 5: Further tuning and enhancements

  • The date_offset (=2592000) in the above rule corresponds to "30 days in advance", which may need review / tuning (it's about 1 month, right?).

  • You might want to also use the Field Permissions module to set field-level permissions to edit, view and create fields on any entity. In your case maybe you only want admins to edit that reminder field.

  • 5 years is a long time ... remaining homework: what if the user account has been deleted.

  • Instead of just sending an eMail (as in the Rules Component), you could replace that by a more advanced option using the Message module (which has great integrations with the Rules module also). Here is an quote about that module (from its project page):

    ... enables logging and displaying system events in a number of different use cases. Events that are recorded over time are sometimes call activity streams. Exportable messages subtypes can be created for different use cases with custom fields and display (view) modes.

Step 6: User boarding

With the above solution in place, you have a solution for (a) all future new user accounts that will be created and (b) all existing accounts for which a date is entered (manually?) in that field_next_reminder field. However you could take advantage of that existing "Entry date" you mentioned, to also get a similar process in place for your existing user accounts, by performing an (one time only) appropriate mass update (bulk) operation. If you wonder how to also get that to work, then use this link.

Note: Using Rules to send an email X days after a user registers also contains quite some details that may help you.

3
  • Wow this is an amazing answer. I'm going to try this and will come back at you with suggestions and questions wether it worked or not.
    – Vasmir
    Commented Feb 13, 2017 at 9:24
  • @Vasmir : wow, that's a nice comment. I look forward to future feedback about it (and/or possible followup questions, posted as new questions to not change the original question here). BTW, there is another (rules based) solution which doesn't involve the Rules Scheduler at all ... If I make you curious about that, use this link. Commented Feb 13, 2017 at 9:37
  • @Vasmir any progress on this? Did you get it to work? Commented Mar 27, 2017 at 15:09
3

You can achieve your goal using both Rules and Scheduler module.

Check Using rules to send an email x days after a user registers.

If you are looking for custom coding, you can implement hook_cron, as explained in the answer to the question about "How to create a task which run at cron time and send emails to users?".

Some other links which may help you:

3
  • Would not it repeat the email sending step on each schedule.
    – sarathkm
    Commented Feb 3, 2017 at 11:49
  • Need to add some conditions, rule will execute based on that conditions.
    – DRUPWAY
    Commented Feb 3, 2017 at 11:51
  • Thanks. The link with the hook cron custom coding description seems to be fitting with what I am searching for. Where do I modify the code for hook cron?
    – Vasmir
    Commented Feb 3, 2017 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.