In the digital landscape, a contact form is an indispensable component for any website, facilitating communication between users and website owners. While real-time messaging is an enticing option, the practicality and cost-effectiveness of the email method make it a prevalent choice.

In this article, we’ll delve into the process of creating a PHP contact form that sends emails, exploring each step in detail.

The Basis of Email Method Contact Form

The email method involves a straightforward approach. Users input their email and message in designated fields on a form. Upon submission, an email, including the user’s message and a “Reply-To” header with the user’s email, is sent to a specified address (e.g., [email protected]). 

This allows website owners to easily respond to user inquiries using their preferred email service provider.

Creating the HTML Form

Let’s begin by creating the HTML form. Employ the POST method and an empty action attribute, signaling the browser to submit the form to the same page. Below is a simplified HTML structure for the contact form.

<form method="POST" action=""> 
    <div class="input-wrap">
        <span class="label">Email:</span>
        <input type="text" name="email">
    </div>
    <div class="input-wrap">
        <span class="label">Message:</span>
        <textarea name="message"></textarea>
    </div>
    <div class="input-wrap">
        <input type="submit" name="submit" value="Submit" class="submit-button">
    </div>
</form>

Enhance your coding skills with the best tips for PHP structure and syntax

Sending the Email with PHP

To enable the form to send emails, we need to incorporate PHP code. This involves several steps, including checking for form submission, collecting and validating submitted data, creating the email, sending the email, and reporting errors. The following PHP snippets illustrate these steps:

Checking for Form Submission

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Your other code here
}
?>

Checking, Collecting, and Validating Submitted Data

<?php
if ($_SERVER['REQUEST_METHOD'] === "POST") {
    // Code for email validation
    // Code for message validation
}
?>

Creating the Email

<?php
if (empty($emailError) && empty($messageError)) {
    // Code to create a visually appealing email template
}
?>

Sending the Email

<?php
$to = '[email protected]';
$subject = 'Contact Form Submission';

if (mail($to, $subject, $emailBody, $headers)) {
    $sent = true;    
}
?>

Reporting Errors

<?php if (isset($emailError) || isset($messageError)) : ?> 
    <div id="error-message">
        <?php 
            echo isset($emailError) ? $emailError . '<br>' : ''; 
            echo isset($messageError) ? $messageError . '<br>' : ''; 
        ?>
    </div>
<?php endif; ?>

Styling the Form

Enhance user experience by styling the form. The following CSS code provides a basic styling example:

body {
    background-color: #fafafa;
}

form {
    width: 50%;
    margin: auto;
}

.input-wrap {
    padding: 20px 0;
}

.label {
    display: block;
    margin-bottom: 2px;
}

input, textarea {
    border: 1px solid #eee;
    padding: 6px;
    border-radius: 3px;
    width: 100%;
}

textarea {
    height: 200px;
    resize: none;
}

.submit-button {
    padding: 10px;
}

Conclusion

Crafting a PHP contact form that utilizes the email method empowers website owners to seamlessly connect with their audience.

From the HTML form creation to the integration of PHP code for form submission, data validation, and email generation, each aspect contributes to a robust and functional contact form. Styling considerations further enhance the user experience, making the interaction both practical and visually appealing.

As you deploy this email method contact form on your website, remember the importance of customization to align with your site’s design and user interface. Effective communication begins with a well-crafted contact form, setting the stage for meaningful interactions with your website visitors.