On a community website, it’s a good idea to send some type of follow-up email a week or month after a user registers. This email can be used to ask for feedback or promote useful features.
This type of functionality can be built in a few ways. You could use a 3rd party email service like MailChimp or AWeber to send the follow-up as an auto-responder.
Another possibility, is to send the follow-up email directly from Drupal.
In this tutorial, I’ll show you how to use Rules Scheduler (part of Rules) to send a follow-up email one week after a user registers.
Now, every website is different and you may want to send this email after three days or one month. By using Rules, you can adjust this type of configuration directly from Drupal without writing any custom code.
We’ll create a basic rule that will need to be adjusted to suit your website. Use it as an example and remember don’t spam your users.
Simplenews is a powerful module that allows you to send newsletters directly from Drupal. If you need this type of functionality, then give it a try.
Getting Started
Head over to drupal.org and download Rules, Entity API, Views and Ctools. Once downloaded, enable Rules UI, Rules Scheduler and Views UI.
If you use Drush, run the following commands:
drush dl rules entity views ctools
drush en rules_admin rules_scheduler views_ui
How it Works
The functionality is split in two parts: a component and a rule.
We’ll create a rule called “Schedule follow-up email” that schedules the component to be executed one week from when a user registers.
The component, which we’ll call “Send follow-up email”, will be used to send the actual email. If you’ve never played around with Rules Scheduler, this concept is hard to understand until you see it in action.
Now, for Rules Scheduler to work you’ll need to make sure cron is running. If you’re having trouble with components not being executed, then check the “Recent log messages” and see if it’s an issue with cron.
For a great introduction into Rules Scheduler, I recommend that you watch the video: “Introducing Rules Scheduler“.
Create Component
1. Go to Configuration, Rules, Components and click on “Add new component”.
2. Select “Action set” from the “Component plugin” drop-down list and click on Continue.
3. Enter “Send follow-up email” into the Name field.
4. In the Variables section, select User from the “Data type” drop-down list, then enter User into Label and enter user
into Machine name.
Once you’ve filled out the form click on Continue.
At this point, we have an empty component with the User entity type passing through it as a variable. Now, all we need to do is create an action that sends the follow-up email.
5. Click on “Add action” and select “Send mail”.
6. In the To section, click on the “Switch to data selection” button so we can use a data selector instead of a token.
Then click on the selector and select user:mail
.
Go ahead and fill out the Subject and Message text area then click on Save.
All we have done so far is create a component called “Send follow-up email” with a single action that sends the email.
Create Rule
As mentioned earlier, the functionality is split up in two parts: a component and a rule.
We’ve already created the component that will send the email, now let’s create the rule that will schedule the component.
1. Go to Configuration, Rules and click on “Add new rule”.
2. Enter “Schedule follow-up email” into the Name field and select “After saving a new user account” from the “React on event” drop-down list. Then click on Save.
3. Click on “Add action” and select “Schedule component evaluation” from the “Select the action to add” drop-down list.
If you can’t see this action then make sure you’ve enabled the Rules Scheduler sub-module.
4. Select the “Send follow-up email” component from the Component field-set and click on Continue.
5. From the “Scheduled evaluation date” field-set, you can define which date the component will be evaluated on.
For our setup, just enter in +1 week
. This means it’ll run one week from today.
5. In the Identifier field-set, you need to add a string that uniquely identifies this task.
Enter in Follow-up email for [account:uid]
into the Value field.
6. And finally, in the User field-set select account
from the Data selector.
Once you’ve selected an account, click on Save.
Test Follow-Up Email
Now that we have everything setup, let’s test and make sure it’s working.
Go ahead and register a new user account on your Drupal site. Once registered, go to Configuration, Rules and click on the Schedule tab.
You should see a scheduled task with the identifier Follow-up email for X
. X
being the User ID.
Once the scheduled date has passed, the component will execute and a follow-up email will be sent.
If you see a message that says “To display scheduled tasks you have to install the Views module.”, then make sure you enable the Views module. The Schedule page uses Views to display tasks.
FAQ
Here’s a FAQ list on a few possible problems and how to investigate them.
Q: Emails are not being sent.
First make sure your Drupal site has been set up to send emails. This can be achieved by setting up a mail server or by using a 3rd party SMTP provider.
To see if Rules has sent an email, go to Reports and the “Recent log messages” page and look for a message: “Successfully sent email to email@example.com”. If you can see this message, then Rules sent the email, but you still need to make sure you’ve configured your server to send emails.
Lastly, check your spam folder. Often emails that are sent from test servers end up in the spam folder.
Q: I can’t see any tasks on the Schedule page.
First of all make sure you’ve installed Views. The Schedule page uses the module to display all tasks.
Second, make sure you’ve added the “Schedule component evaluation” action to schedule the component. Without this action tasks will not be created.
Q: How do I send HTML emails?
The “Send mail” action used in the component only sends plain text emails. If you want to send HTML emails, then look at using Mime Mail. This module comes with an action called “Send HTML e-mail” which can be used to send emails as HTML.
Conclusion
I hope you now have a better understanding about Rules Scheduler and how to use it. Not only can it be used to fire off emails, you can use it to remove roles or remove comment access.
If you need to automate and schedule the execution of a component, then Rules Scheduler is the right module for the job.
Good day Ivan! Please, tell how I can run schedule like in your tutorial – unlimited loop
e.g. for example I want create NODE with title CURRENT DATE – every minute?
Good question. I’ve never set anything up like this using Rules.
Rules scheduler does not offer any type of recurring functionality.
For your example, I would implement it using custom code. Then run the code in hook_cron.
I might have spent hours wrestling with Rules if I hadn’t come across these excellent instructions first. I followed them to the letter and everything worked perfectly. Thank you.
Many thanks for the great tutorial. On the “Add a new action”, I have to enter a key – didn’t see that mentioned in your instructions. What would you suggest entering there?
John
The key can be anything, it’s used as an “ID” for the message. Just enter something unique.
Thanks!! for the tutorials it really help me to understand about rules, only that I want to schedule an action (sending an email) on an anniversary date, the date is in the database with a year different than the actual. Is there a way i can calculated these?
To calculate a custom “Scheduled evaluation date” you’ll have to create an entity property with custom code. (https://drupal.org/node/1021466)
Dear Ivan, Could you please tell me how to send a daily birthday wishing e-mail to users who are registered and filled text box with their birthdays. Many thanks for your tutorial.
This will require a whole tutorial so I won’t write it as a comment. But just create a “birthday” field using the date module on the user entity and use Rule scheduler to send the email.
Any guide on how to do it with Drupal 6? The Rules interface seems very different and this rules scheduler thing is the most illogical thing I have seen. It’s so difficult to follow.
I don’t know if you can do this in Drupal 6. Ask in the Rules issue queue.
yep! I was able to get it working with D6 Rules. The interface was quite different, I will hopefully make a video tutorial soon to share on Youtube.
I have followed your rule whatever you have described above.i have created a component and rule it is sending mail only once after user has been created .i want to send every 3 months a mail to user after created an account.
Thanks dk, good question.
To send an email every three months, you’ll need to use the “Schedule component evaluation” action when an email is sent.
Let me explain a bit more.
1. When a user is created, schedule an email to be sent in 3 months.
2. In 3 months, the action to send the email will be executed. In that action(which sends the mail), add “Schedule component evaluation”, to resend in another 3 months.
When an email is sent, the next is scheduled in at the same time.
Hope this helps.
Once the scheduled rules are created, what is a good method to run a rule to delete the old scheduled tasks once they have been executed?
(ie: Scheduled task is created by a rule to send an e-mail 10 days after user registration. Another rule deletes the executed task once it has run.)
Trying to parse this one out in how to clean up scheduled rules but having difficulty. Any thoughts would be awesome.
Hi Shane,
The scheduled rules should automatically be deleted once they’ve been executed. Just to make sure, you are referring to the rules on “admin/config/workflow/rules/schedule”?
I’ve never needed functionality which deletes an executed scheduled rule. After it’s been executed it’s gone.
Look in your logs and see if any PHP errors are appearing.
Cheers,
Ivan
Hi Ivan,
many thanks for the great tutorial – it really helped.
I have one more question, how can I apply this only for a particular role?
I tried with adding a condition, selected a role. But then the whole thing didn’t work anymore.
Any idea?
Many thanks again!
Bests
Selim
Hi Selim,
To run a rule on a user role I would do the following:
– Add the condition called “User has role(s)”.
– Select a role.
You may have already done the same thing. However, this is how I’ve done it in the past.
Cheers,
Ivan
… this is exactly what I did.
What I wonder is, what has to be indicated in the User-section
at User identifier / Data selector ?
In my case it says “account”. Is that what you also have?
Thanks again!
Bests
Selim
It depends how the rule is being fired off. It should be something like `user` or `account`.
Hi Ivan,
now I found the reason for the condition didn’t work:
Unfortunately the module “Multiple registration” forms didn’t take the condition.
Since I tried https://www.drupal.org/project/profile2_regpath and setup the role that should be registered with, it worked well with the condition “if certain role than rule scheduler”.
Many thanks
Bests
Selim
Thanks for the update.
One more question: as mentioned in my previous comment, I use https://www.drupal.org/project/profile2_regpath in order to put custom registration fields in /user/registration.
Now, in /admin/config/people/accounts/fields I can indicate what sort of fields shall appear in the regsitration procedure. With https://www.drupal.org/project/profile2_regpath I can create a separate registration-from – it actually works.
But now there is a problem. In my standard registration I ask users to indicate username and password only – all other fields are not displayed. For example: for “field_firstname” the “Display on user registration form.”-option is unchecked, which is also true for other address-fields.
But at my other registration-form, I use a different registration-profile which I created with the above module (profile2_regpath) at /admin/structure/profiles/manage/profile_alternative_register/fields.
When I now add an existing field, such as “field_firstname”, I thought it should fill the record so that the content appears also in user/[uid]/edit – but it doesn’t.
Instead in the user-settings I can see a link named after the profile-name “Profile Alternative Register” (at user/[uid]/edit/profile_alternative_register), which contains the indicated record given by the newly registered user.
But – big but – how can I access these fields in my rule?
I tried with [account:field-firstname], for example, but nothing seems to happen.
How can I achieve that the standard records for the user, accessible and changable for him at user/[uid]/edit are filled with the contents given at the registration?
Any experience with this?
Many many thanks in advance!
Bests
Selim
To get the actual fields you’ll need to use the “Fetch entity by property” condition.
This Drupal answers question explains how to do it.
http://drupal.stackexchange.com/q/108763
Hi Ivan,
great help! Many thanks.
Selim
Hi Ivan,
great tutorial!
Now that I have it going I thought it would make alot of sense, if the user could unsibscribe from the Follow-Up-Emails.
One the one hand I can create a rule-action as “Delete scheduled tasks”, but then all scheduled tasks of a certain component will be deleted. Or is there a way I can indicate a particular User-ID in the actions “TASK-IDENTIFIER” field?
Many thanks!
Best wishes
Selim
Creating an unsubscribe link is possible but will require custom code and it’s way beyond the scope of this tutorial.
If you want to handle auto responders and unsubscribes, I’d look at the simplenews module (https://www.drupal.org/project/simplenews).
Hi Ivan,
Great tutorial. I need to send a mail on every thursdays to all active users . So in the field ‘SCHEDULED EVALUATION DATE’, what should be the value? Also if I choose the event as ‘When the user account is activated’, mail would only be send for the first time when the user account is activated, right? In my scenario, what should be the event?
Thanks in advance!
Hi Bincy,
You can’t select specific days. Instead you set the next scheduled evaluation “+ 7 days” when the current one runs.
For example, if today is Thursday, run the rule and then schedule next week’s one as an action at the end.
Yes the activated user condition will only run once. What constitutes an “active user”, logged in the last 7 days or 30 days?
Once you know that, there should be a condition which checks when the user last logged in.
Good luck.
Cheers,
Ivan
Hello – great tutorial. How do I extend this to ONLY send an email if the user hasn’t LOGGED IN yet? It seems that the email component is scheduled as USER CREATION, but then the component doesn’t seem to have any further conditions such as is checking if the LAST LOGGED IN DATE is null.
How would I implement this?
Hi Stevo,
You’d have to change the condition in the Rule that fires the component.
The component itself is basic, it just defines an action. You’ll have to create a rule where you define the condition to check if they have logged in.
Cheers,
Ivan