Can I script to send multiple emails?

Can I script to send multiple emails?

I would like to be able to input contacts, then when an "on-submit" alert happens somewhere else check them and send several of them emails according to certain conditions (for example - send an email to all contacts whose name is John).

Is there a "for-each" command or something similar?