aucoeur wrote: I apologize but it appears the code it too long to paste here.
Part of the debugging process then is for you to try and cut out what is irrelevant - testing as you go to make sure you don't change the bug.
Once you have got it cut down to the minimum necessary to show the bug and you still can't see the problem, then you'll have something to post.
Edit: Have you also confirmed that the problem really is that the emails sometimes aren't sent out, and not that they sometimes aren't received (because of spam filters and the like)?