PHP website form is not sending the form fields to email client-ThrowExceptions

Exception or error:

I have created PHP forms and used similar code in the process.php file but for some reason this one does not want to work for me.

I have the index.html form set up:

<form id="contactform" class="contact-form" method="post" action="process.php">

<button class="btn" type="submit" name="submit" value="Submit Message" onclick="submitForm()">Submit</button>

process.php file:

if(isset($_POST['submit'])) {

    $to = 'myEmail@myemail.com';
    $name = $_POST['name'];
    $subject = $_POST['subject'];
    $facility = $_POST['faciity'];
    $number = $_POST['number'];
    $contactname = $_POST['contactname'];
    $contacttitle = $_POST['contacttitle'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $city = $_POST['city'];
    $zipcode = $_POST['zipcode'];
    $message = $_POST['message'];
    $headers = "From: " .$email . "\r\n" .
    mail($to, $name, $subject, $faciity, $number, $contactname, $contacttitle, $email, $phone, $city, $zipcode, $message, $headers);

    header("Location: contacted.html");
}
?>

custom.js form:

function submitForm() {
   // Get the first form with the name
   // Usually the form name is not repeated
   // but duplicate names are possible in HTML
   // Therefore to work around the issue, enforce the correct index
   var frm = document.getElementsByName('contact-form')[0];
   frm.submit(); // Submit the form
   frm.reset();  // Reset all form data
   return false; // Prevent page refresh
}

I hit submit and it successfully redirects to my contacted.html page to let the client know the form was submitted.

but nothing actually gets redirected to my email client from the webserver.

Was advised that there is an issue with my process.php form. I am not a PHP developer and would appreciate any guidance to help fix the form. Thanks in advance!

here is the html code of the actual form:

<div class="row wow fadeInUp">
                    <div class="col-md-8 col-md-offset-2">
                        <form id="contactform" class="contact-form" method="post" action="process.php">
                            <div class="row">
                                <div class="col-md-6"><input id="name" type="text" class="form-control" name="name" placeholder="Company Name*" required>
                                </div>

                                <div class="col-md-6"><input id="facility" type="text" class="form-control" name="facility" placeholder="Facility Type*" required>
                                </div>

                                <div class="col-md-12"><br>
                                    <input type="number" id="locations" placeholder="Number of Locations*" class="form-control" name="locations" required><br>
                                </div>

                                <div class="col-md-6"><input id="contactname" type="text" class="form-control" name="contactname" placeholder="Contact Person Name*" required></div>
                                <br>
                                <div class="col-md-6"><input id="contacttitle" type="text" class="form-control" name="contacttitle" placeholder="Contact Person Title/Position*" required></div>
                                <div class="col-md-12"><br>
                                    <input type="email" id="email" placeholder="Email*" class="form-control" name="email" required>
                                </div>

                                <div class="col-md-6"><br><input id="phone" type="tel" class="form-control" name="phone" placeholder="Contact Phone Number*" required></div>
                                <div class="col-md-6"><br><input id="city" type="text" class="form-control" name="city" placeholder="City*" required></div>
                                <div class="col-md-12"><br>
                                    <input type="text" pattern="[0-9]{5}" id="zipcode" placeholder="Zip Code*" class="form-control" name="zipcode" required>
                                </div>

                                <div class="col-md-12"><br>
                                    <textarea name="name" id="subject" placeholder="Message" class="form-control" cols="30" rows="10" required></textarea>
                                </div>
                                <div class="col-md-12 text-center"><br><br>
                                    <button class="btn" type="submit" name="submit" value="Submit Message" onclick="submitForm()">Submit Message</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
How to solve:

You seem to have a typo here (the dot instead of “;” after the header definition):

$headers = "From: " .$email . "\r\n" .
mail($to, $name, $subject, $faciity, $number, $contactname, $contacttitle, $email, $phone, $city, $zipcode, $message, $headers);

As a result, mail() gets called without $headers being defined. Chances are that this interferes with the process.

However, on closer look, your call to mail() seems anomalous: you are specifying fields for facility (mis-spelled), contact title, ZIP code and other things that mail has no business knowing. Check the manual page for mail().

mail($to, $name, $subject, $faciity, $number, $contactname, $contacttitle, $email, $phone, $city, $zipcode, $message, $headers);

Be advised that there are several other issues in your code, like $_POST values being accepted with no checks at all. In some systems this might allow anyone to send email to anyone else from your server, even if you did specify your address in the $to.

update: sending information via mail()

The mail() function only worries about sending a mail body. What is in the mail body is your concern, not mail()’s.

So what you need to do is to create a suitable mail body, e.g.

$body = <<<BODY
    We received a POST form from the website.
    These are the data:

BODY; /* this line must start at the first column, no indent */

foreach ($_POST as $key => $value) {
    $body .= "{$key} = '{$value}'\n";
}
// Add a machine-useable part in case we parse this with some tool
// We use base64_encode to make sure it's harmless ASCII, and serialize
// instead of, say, json_encode since bad UTF8 strings can make js_encode
// croak and return false (or NULL).
$body .= "====CODE===\n" . base64_encode(serialize($_POST)) . "\n===CODE====\n";
$body .= "\n\nYours, Web server.";

// Now we send mail. Defaults, system mailer, nothing fancy.
mail('user@somewhere', 'Mail form from www.yoursite.it', $body);

// For more robust and flexible email sending, go for PHPMailer.

You will receive something like this:

From: www-data@yourserver.com
Subj: Mail form from www.yoursite.it

We received a POST form from the website.
These are the data:

name: John
surname: Smith
zipcode: 50134
...
...
====CODE===
YmxpbmRhIGxhIHNiaXJpZ3VkYSBjb21lIGZvc3NlIGFudGFuaSwgbGEgc3VwZXJjYXp6b2xhIGNv
biBzY2FwcGVsbGFtZW50bwo=
===CODE====

Yours, Web server.

You can now enter these data manually or decode and interpret the CODE section with another PHP script.

Leave a Reply

Your email address will not be published. Required fields are marked *