Here[
^] is a step-by-step guide. It should at least get you started in the right direction.
I know nothing about PHP (Except what it is), nothing about MIME (Except what it is), I do know a little about search engines though and that skill enabled me to find the link above.
I'm sure that if you practise, you will be able to do this sort of thing in time.