How do I automatically assign a role to new users based on the email address domain?

For instance if the email address is [email protected], and the condition is that all users from examplecompany.com should always have the role specialclient. How do I acheive this?

I am using Rules, but there are no specific email conditions to be found. Can I create my own Rules Condition? If so, I would be very grateful for any tips on how to accomplish this.

1 Answer 1


Yes you could create your own (custom) Rules Condition. But in this case I don't think that's needed. The clue to what you're asking for, is a Rules Condition which is a "text comparison".

In that comparison, you use a regular expression to indicate "the email address should end with "@examplecompany.com".

For an example rule which also uses such RegEx-expressions, refer to the answer to "How to use the Rules module to perform a simple anti-spam validation using specific keywords?".

Be aware: I'm not a RegEx expert ..., but here a few dozens of similar examples using a RegEx ...

However, I predict you'll have multiple domains/roles to be processed. So the real Rules fun starts when this (typical) question arises: "I need to add something like a Rules Condition inside the Rules Actions, how can I do that?"

To resolve this typical question, there are basically 2 approaches (that I am aware of):

  • Use the Conditional Rules module. For an example using the Conditional Rules module (for which there is not a lot of docu available), refer to my answer to the question "How to prevent a node being saved when using the Rules module?".
  • Rework the relevant part of your Rules Action(s) into performing a Rules Component. Because in a Rules Component, it is possible to also add Rules Conditions.
  • Thanks a lot for your answer, Very useful and upvote. I will try to do as you suggest.
    – TBJ
    Feb 13, 2017 at 10:15
  • @TBJ Merci for the feedback ... you probably also want to try the 2nd part I just added ... Feb 13, 2017 at 10:22

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.