Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

oliverbusk's avatar

Rule Engine - Bind to specific model

I am currently trying to create a web application, that saves inbound emails. I have figured out how to save the incoming emails into my database, and I have all the email information available (headers, body etc.)

I want to be able to allow my users to set custom conditions for each of the email inboxes they have. So image user A have three inboxes. User A decides that for Inbox 1, below rule should apply:

Mockup

Meaning that the script, in this case, should:

  1. Check that the subject contains the word "Welcome!"
  2. Check that the From message does not contain the word "@hotmail.com"

Now I know I can just write a "simple" ifstatement, but this will not solve my problem, as I need it to be:

  1. Dynamic
  2. Specific for the given inbox

I am not sure how to go around this. I have found this project hoaproject/Ruler which seems to be suiting my needs in regards to writing "complex, dynamic" rules.

However, how would I go about implementing this dynamically?

As said, I want my users to be able to set rules that are specific for their own mailboxes.

Should these rules be saved in a database?

0 likes
1 reply
Sinnbeck's avatar

I have implemented something very similar for a project I am working on. The way I handled it was to save the conditions as a tree Eg. in your case there would be 1 root element with A and B (a column for each pointing to a new id) A points to the first rule and B point to the second. Now I start with setting the default return to false, and the check A and B. The result of each i run though

if(A && B) {
    return true
}

Now this is taking further by expanding the tree A can point to first rule, and B can point to a new branch which then points to a new A and B (thereby having 3 rules).

This might seem complex but after building a recursive function it works quite perfect :)

Please or to participate in this conversation.