Custom script email condition

is it possible to have a custom script function that returns true for the condition for the Recipients ? I want to send the email to a specific role based an employee’s location/branch/department, also the current doctype has an location identifier

I think you can query the database for that. I’m not very familiar with ERPNext and the different tables inside but I’m pretty sure you can get the list of users with a certain role doing :
frappe.db.sql(" SELECT email FROM 'tabUser' user, 'tabHas Role' hasRole WHERE = hasRole.parent AND hasRole.role = 'Stock User'; ")
This will list you all users who have Stock User Role.

i am trying to set the received by role to like supervisor that will send the email to all supervisor, but the each supervisor is in a different location and should only receive the email if the doctype location is the same as his location. i can query the database to do this in my app but i was wondering it was possible to write the script here or is there like a hook or python file that i can write the script and link it to the condition field. for example like the file Hooks that returns that has a function that returns a boolean.

When you create a custom Doctype a controller class file is created may be you can add some code on the after_insert method to add the task of sending emails to some queue.
Refer to these links :

thanks, i will try to use Utility Functions
along with the controller, but i wish trying to avoid this and use the normal email notification