Or they could run a few algorithms client side on the text, return some sufficiently anonymized data and show ads based on that. So if you are emailing about organizing a birthday party you get balloon recommendations, but if you got a ransom letter with only an address, a photo and a a big number after the dollar sign, they might opt to not send data back to their ad servers.
If they think there is money to be made on this despite the hardness of the problem, they will come up with some okay-ish solution.
Or just make e2e 5 usd/month.
Though I never clicked on any ads in GMail, nor did I see anyone do it, so maybe GMail is not a great source of ad income.