A Basic, Working, PHP Script That Uses PHP Mail From a Form Submission

Written by Christian Rumi 12 December 2022 1,934 views No Comment

Here is an example PHP script that can be used to accept 6 variables from a form and send them to an email address. It was tested today Monday, December 12, 2020. The reason this was written is because I was finding a lot of issues using the basic PHP Mail function. It was written with prompts to ChatGPT.

// Check if the form was submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Collect the form data
    $var1 = $_POST['var1'];
    $var2 = $_POST['var2'];
    $var3 = $_POST['var3'];
    $var4 = $_POST['var4'];
    $var5 = $_POST['var5'];
    $var6 = $_POST['var6'];

    // Set the recipient email address
    $to = 'your@email.com';

    // Build the email subject and body
    $subject = "Form submission: $var1";
    $body = "You have received a new form submission.\n\n"
           . "Var1: $var1\n"
           . "Var2: $var2\n"
           . "Var3: $var3\n"
           . "Var4: $var4\n"
           . "Var5: $var5\n"
           . "Var6: $var6\n";

    // Send the email
    mail($to, $subject, $body);
}

To use this script simply replace your@email.com with the email address you want to receive the form data to, and replace var1, var2, etc. with the names of the form fields where the data will be entered.

Add a page redirect function

When you’re done, add the following script to redirect the submission to a thank you page. You can add it after the mail function:

// Set the URL of the page to redirect to
$url = 'https://www.example.com/thank-you/';

// Redirect to the specified URL
header("Location: $url");
exit;

A self contained form in one HTML document

Here is a self contained version of the code that submits the form to itself:

<!doctype html>
<html>
<head>
    <title>Form Submission</title>
</head>
<body>
    <?php
    // Check if the form was submitted
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        // Collect the form data
        $var1 = $_POST['var1'];
        $var2 = $_POST['var2'];
        $var3 = $_POST['var3'];
        $var4 = $_POST['var4'];
        $var5 = $_POST['var5'];
        $var6 = $_POST['var6'];

        // Set the recipient email address
        $to = 'your@email.com';

        // Build the email subject and body
        $subject = "Form submission: $var1";
        $body = "You have received a new form submission.\n\n"
               . "Var1: $var1\n"
               . "Var2: $var2\n"
               . "Var3: $var3\n"
               . "Var4: $var4\n"
           . "Var5: $var5\n"
           . "Var6: $var6\n";

        // Send the email
        mail($to, $subject, $body);

        // Display a "thank you" message
        echo "<p>Thank you for your form submission!</p>";
    } else {
        // Display the form
        ?>
        <form method="post" action="">
            <label>Var1: <input type="text" name="var1"></label><br>
            <label>Var2: <input type="text" name="var2"></label><br>
            <label>Var3: <input type="text" name="var3"></label><br>
            <label>Var4: <input type="text" name="var4"></label><br>
            <label>Var5: <input type="text" name="var5"></label><br>
            <label>Var6: <input type="text" name="var6"></label><br>
            <input type="submit" value="Submit">
        </form>
        <?php
    }
    ?>
</body>
</html>

In the above script the PHP code checks if the form was submitted and if it was, it collects the form information, sends an email with the information, and displays the “thank you” message. If the form was not submitted, it simply displays the HTML form.

Written by

Comments are closed.