There are a whole host of email handling packages on CPAN, and they really are the easiest way.
If you don't want to use modules, you're asking for a World of Pain, but the RFCs are there to help.
Look at RFCs 822 and more importantly 2822 for a detailed description of Internet Messages (i.e. email).
Once again, I really suggest you use the robust modules from CPAN, rather than try it yourself.
If the information in this post is inaccurate, or just plain wrong, don't just downvote - please post explaining what's wrong.
That way everyone learns.