I'm going to build my own contact form for my Wordpress site, and it'll be on the Contact page.
I have a page template for this page to use, but I want to know where is the best place to have the wp_mail()
function, in the theme's functions.php file, or in the page template file?
I would normally place all form related PHP stuff in the file the form appears in like so:
<?php if(isset($_POST['submit'])) {
// Form PHP stuff
} ?>
<form>
// Form fields
</form>
it doesn't matter where you place this function, it depends on the requirement such as if you want when any user submit the form email should send. so you decide where to place either on the same file or another file.
let suppose you have ajax based form submission, there are several ways to make it work.
Custom:
WP AJAX functionality:
see here how WP ajax works WP theme.
in above linked article where it used php default mail function you can replace it wih wp_mail.