@Yellowcakestand It is worth revisiting this situation with your son. This can be a 5 minute conversation.
It is important you tell your child you believe what he told you about the teacher saying, "no wonder your handwriting isn't neat, writing like that" and that you understand it wasn't the best learning comment for him.
Acknowledge his feelings - that he felt like crying and that it was an upsetting thing to hear.
You might say, "isn't it a pity that teacher wasn't able to say what you needed to hear to help you learn on that occasion".
Let your son know that you believe he was trying his best, and that it was unfortunate the teacher did not praise his effort this time. Then praise him for the effort he made.
Let him know his handwriting can and will improve, but it will take time and effort. Say something like, "you learned to walk/talk and that was tricky at first but he got the hang of it and now he can. All tricky things takes practice".
You may wish to ask the school about what help / support / aids they can provide to help him improve his handwriting and his confidence in it.
You can let them know that he will learn best from encouragement and promotion of a growth mindset (not comments like the one the teacher made). FYI this was not good teaching practice, the comment was mean and if said in front of others potentially humiliating.
Encourage your son toward a growth mindset in general.
Using the word yet helps...
It's tricky to do x, yet with practice you will be able to do it / do it better.
If he says he can't do something, you can say not quite yet, but with practice you will.
You can also make funny comments like "if your handwriting's not the neatest, perhaps you'll become a doctor. Lots of people say doctors have messy handwriting, or a creative? Lots of creative people have messy handwriting. Just try your best and we'll see where it takes you!".
It worked with my child. Handwriting still relatively messy but happy and successful :)