Send email when condition is met by using a preconfigured email template

Hi there! I´m very novice in PHP and I´m really stuck in this script. I appreciate if you could help me to know how to achieve this. Take this as a practice and feel fre to ask any doubt if the mission is not clear! Thanks in advance

This is the deal:

I have 5 SQL tables with data and I need to send an email using some variables of each table. These are the important data on each

DEPARTMENT
[table=“width: 500, class: grid, align: left”]
[tr]
[td]id[/td]
[td]name[/td]
[/tr]
[tr]
[td]1[/td]
[td]Secretary[/td]
[/tr]
[/table]

SUPERIOR

[table=“width: 500, class: grid, align: left”]
[tr]
[td]id[/td]
[td]name[/td]
[td]email[/td]
[td]department_id[/td]
[/tr]
[tr]
[td]1[/td]
[td]Ana[/td]
[td]ana@gmail.com[/td]
[td]1[/td]
[/tr]
[tr]
[td]2[/td]
[td]Peter[/td]
[td]peter@gmail.com[/td]
[td]1[/td]
[/tr]
[/table]

CLASSROOM

[table=“width: 500, class: grid, align: left”]
[tr]
[td]id[/td]
[td]name[/td]
[td]count[/td]
[td]alertcount[/td]
[td]superior_id[/td]
[/tr]
[tr]
[td]1[/td]
[td]biology[/td]
[td]10[/td]
[td]25[/td]
[td]1[/td]
[/tr]
[tr]
[td]2[/td]
[td]grammar[/td]
[td]48[/td]
[td]45[/td]
[td]2[/td]
[/tr]
[/table]

TEACHER

[table=“width: 500, class: grid, align: left”]
[tr]
[td]id[/td]
[td]name[/td]
[td]email[/td]
[/tr]
[tr]
[td]1[/td]
[td]Kat[/td]
[td]kat@gmail.com[/td]
[/tr]
[tr]
[td]2[/td]
[td]Joe[/td]
[td]joe@gmail.com[/td]
[/tr]
[/table]

LEVEL
[table=“width: 500, class: grid, align: left”]
[tr]
[td]id[/td]
[td]number[/td]
[td]classroom_id [/td]
[td]teacher_id[/td]
[/tr]
[tr]
[td]1[/td]
[td]A101[/td]
[td]1[/td]
[td]1[/td]
[/tr]
[tr]
[td]2[/td]
[td]A102[/td]
[td]2[/td]
[td]1[/td]
[/tr]
[tr]
[td]3[/td]
[td]K-12 [/td]
[td]1[/td]
[td]2[/td]
[/tr]
[/table]

I want to send one email (Bcc) to different TEACHERs when CLASSROOM.count = CLASSROOM.alert_count

The email should be structured like this:

[HR][/HR]

From, Reply to and To: SUPERIOR.email assigned to CLASSROOM.count = CLASSROOM.alert_count , SUPERIOR.name assigned to CLASSROOM.count = CLASSROOM.alert_count

Bcc: copy here all the -> TEACHER.email assigned to CLASSROOM.count = CLASSROOM.alert_count , TEACHER.name assigned to CLASSROOM.count = CLASSROOM.alert_count ;

Subject: Notice about CLASSROOM.name with CLASSROOM.count = CLASSROOM.alert_count

MESSAGE BODY

Hi TEACHER.name,

I´m SUPERIOR.name from ENTITY.name and I want to notify you that CLASSROOM.name CLASSROOM.description on LEVEL.number is ready to begging. Thanks
[HR][/HR]

Just to split in to parts the email body, each TEACHER.name will receive a similar email. Similar because CLASSROOM.name CLASSROOM.description will be the same for all the teachers, it is the CLASSROOM whose count is equal to alert_count, but it will be different at the same time because LEVEL.number is different for each teacher, but it is not whatever LEVEL.number, it is the one associated with the CLASSROOM whose count is equal to alert_count.

Again, thank you in advance on what code do I need to write in order to make this send mail script.
Thanks