From the sidelines, I'd think the answer is that your email platform should have that feature, or you should consider using entirely separate flows/tools for transactional emails and marketing emails. Not a lawyer, but AFAIK transactional emails are not subject to Spam rules & don't even need to have an "Unsubscribe" link. Mixing the two is just causing yourself needless pain.
That’s the tricky part that most “ew email is spam” folk forget. The definition of “all” can be super nuanced and most people don’t think about it.