Php html email form template

Here is a basic PHP HTML email form template that you can use as a starting point:

<?php
// Define the email address to send the form data to
$to_email = '[email protected]';

// Define the subject of the email
$subject = 'Contact Form Submission';

// Define the form fields
$fields = array(
    'name' => 'Name',
    'email' => 'Email',
    'message' => 'Message'
);

// Check if the form has been submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Loop through the form fields and validate the data
    foreach ($fields as $field => $label) {
        if (!isset($_POST[$field]) || empty($_POST[$field])) {
            $error = true;
            break;
        }
    }

    // If the form data is valid, send the email
    if (!$error) {
        $message = '';
        foreach ($fields as $field => $label) {
            $message.= "$label: ". $_POST[$field]. "\n";
        }
        $headers = 'From: '. $_POST['email']. "\r\n".
                   'Reply-To: '. $_POST['email']. "\r\n".
                   'MIME-Version: 1.0'. "\r\n".
                   'Content-Type: text/plain; charset=UTF-8';
        mail($to_email, $subject, $message, $headers);
        echo 'Thank you for submitting the form!';
    } else {
        echo 'Error: Please fill out all required fields.';
    }
} else {
    // Display the form
   ?>
    <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
        <table>
            <tr>
                <td><?php echo $fields['name'];?></td>
                <td><input type="text" name="<?php echo $fields['name'];?>" /></td>
            </tr>
            <tr>
                <td><?php echo $fields['email'];?></td>
                <td><input type="email" name="<?php echo $fields['email'];?>" /></td>
            </tr>
            <tr>
                <td><?php echo $fields['message'];?></td>
                <td><textarea name="<?php echo $fields['message'];?>"></textarea></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="Send" /></td>
            </tr>
        </table>
    </form>
    <?php
}
?>

This template uses the mail() function to send the form data to the specified email address. The form fields are defined in an array, and the form data is validated using a simple loop. If the form data is valid, the email is sent using the mail() function. If the form data is invalid, an error message is displayed.

You can customize this template to fit your specific needs by modifying the form fields, email address, and subject. You can also add additional validation and error handling as needed.

Note that this is a basic example, and you may want to consider using a more robust email library or service to handle email sending, especially if you plan to send large volumes of email or need more advanced features.