Configure sendmail to accept emails only if “from”, “to” or specific field matches

you can exclude an email address or even a host that is spamming you using the access file located in your etc directory, the bad news is that it does not support regex or even wildcards.
What I needed to achieve here is disabling all outgoing mail unless the To: field matches some email…here’s how to do that:
edit you file in your /etc/mail dir

HHeader: $>Ruleset

HTo: $>CheckTo
D{Email}[email protected]

R${www}							[email protected] OK
R${root}							[email protected] OK
R$*		[email protected] OK
R${Email}		[email protected] OK
R$*	$#error $: 552 Ha2aw aw aw..... TOZ!

HCc: $>CheckTo

so any message going to another mail would be blocked and sent to postmaster with that error “552 Ha2aw aw aw….. TOZ!”
you can use regex as long as you follow sendmail manual.
the use of here is because of the fact I do not use sendmail to recieve any emails, however if you like to do the block from the outside world you can use to do the job.