The code is too long to place with this answer. See the text file for the php code (the text file can be stored as php file).
I used this php code to send emais some time ago. Please modify the code below the comment:
I hope this helps.