This can be done using a local SMTP server providing such filtering capabilities or a filtering interface (invalid mails are rejected):
- Setup the SMTP server on a machine in your local network.
- Configure the server to forward all mails using one or more accounts of your mail provider(s).
- Configure all mail clients to use your SMTP server for outgoing mail.
- Configure the filters of the SMTP server.
- Optional configure your router or firewall to deny outgoing connections to SMTP port 25 from all local addresses except those of your SMTP server to avoid bypassing your server.
Sample code would implement a simple forwarding only SMTP server. I would look for a ready to use one with filtering capabilities.
If you decide to do so, read about SMTP and test it locally before connecting to the net. You may be in trouble with wrong setups that establish a mail loop.