It is just a phishing scam to get your information. AppleID 2FA codes are, by default, sent as encrypted iCloud notifications, not SMS nor iMessage. And they are only initiated when you try to use your AppleID to sign into an Apple online service.
So some scammer has your email (along no doubt with countless others) and is phishing in the hope somebody will do something with that message.
You can always change your AppleID password if you’re concerned. But I would just block the sender, delete the text and forget about it.
If it was an SMS, you can also forward the text to your cellular service provider to get them to block the sender at their end (forward the text to their spam short code, usually 7726 aka SPAM).