The code looks ok on the page to me. If you are getting a good result now it would seem that the page is ok. What you have described with the order going through but no email being sent is possible, however most of the time server issues like this are not sporadic. Have you tested this with different email addresses? There could be problems regarding certain email addresses that are causing this problem.
So long as you can see that the order has gone through and is recorded in the db there should not be any problem. If you are testing this out and go from the checkout success page directly to the site and test it again you might possibly update an existing order rather than create a new one. If you find this happening to you then you can stop it by adding this code to the bottom of the checkout success page:
<?php
if(!session_id()) session_start();
session_regenerate_id();
unset($_SESSION);
@session_start();
?>