What can I say? Put the debugging back in and find out where the real problem is.
Put in *MORE* debugging.
After every statement in that problem block, if needed.
Ehhh...never mind. I just put in a test, first with a bogus email address and then with a good one. Only the bogus one cause the 500 error.
So clearly the bug is in the email test.
Let me fix mine. It's probably just a typo.
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.