Trying do build a contact form

One way to accomplish this is to setup two pages, with two page templates, one with your form code and the other with your mail code. The form action would point to the permalink of the page whose template hosts the mail code.

That said, I’d actually recommend using a plugin like Contact Form 7 instead. It’s highly modular and customizable and is a major time saver.