Home > Software design >  mail.php can't get it to email the correct data
mail.php can't get it to email the correct data

Time:10-21

I've no experience with php, due to an old formtomail cgi script no longer working on a host server I decided to switch to a simple html to php mail for a contact page on a website. I took a template online and set up the html page but having difficulty editing the mail.php file so that all the data requests on my html form get emailed over to me. The template I use just had email and message. My html contact page has different requests i.e contact name not name, and asks for more information. so I need the php form to email all this information and need help on how to edit the php file to include whats requested on the html contact page.

I have a contact form HTML page with the following:

<form action="mail.php" method="POST">
<p >
Contact Name:<br /><input type="text" name="contact name" size="50" /><br />
Firm Name:<br /><input type="test" name="firm name" /><br /><br />
E-mail: (Please enter the email address you would like the document is to be sent to)<br /> 
<input type="text" name="email" size="50" /><br />
Job Number:<br /><input type="test" name="job number" /><br /><br />
Document you require:<br /><form action=""><select name="Document you require">
<option value="Data Sheet">Data Sheet</option>
</select><br/ ><br />
Discount code:<br /><input type="text" name="Discount code" size="20" /><br /><br />
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>

I then have a mail.php with this (MY EMAIL is my actual email):

<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "MY EMAIL";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>

Would also like it to bring up an existing thank you.html page rather than just a thankyou word on a white page so also need help adding in how I link this.

Any help would be appreciated

CodePudding user response:

Formatting your code will help immensely to start debugging this. I've reformatted your current HTML with some indenting and line breaks to help make it clear:

<form action="mail.php" method="POST"> <!-- missing closing tag -->
    <p > <!-- missing closing tag -->
        Contact Name:<br />
        <input type="text" name="contact name" size="50" /><br />

        Firm Name:<br />
        <input type="test" name="firm name" /><br /><br />

        E-mail: (Please enter the email address you would like the document to be sent to)<br /> 
        <input type="text" name="email" size="50" /><br />

        Job Number:<br />
        <input type="test" name="job number" /><br /><br />

        Document you require:<br />
        <form action=""> <!-- nested form in a form will break things -->
            <select name="Document you require">
                <option value="Data Sheet">Data Sheet</option>
            </select><br/ ><br />

            Discount code:<br />
            <input type="text" name="Discount code" size="20" /><br /><br />
            
            <input type="submit" value="Send">
            <input type="reset" value="Clear">
        </form>

There are a few things wrong with this:

  1. Opening <p> with no </p> (closing tag) anywhere
  2. <form action=""> nested inside of an existing form isn't allowed and will cause submission issues (see "Permitted content" at https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form)
  3. The closing </form> that exists matches the <form action="">, but you're missing a </form>
  4. You don't have a message field anywhere

Here's the updated HTML with my suggested fixes:

<form action="mail.php" method="POST">
    <p >
        Contact Name:<br />
        <input type="text" name="contact_name" size="50" /><br />

        Firm Name:<br />
        <input type="test" name="firm_name" /><br /><br />

        E-mail: (Please enter the email address you would like the document to be sent to)<br /> 
        <input type="text" name="email" size="50" /><br />

        Job Number:<br />
        <input type="test" name="job_number" /><br /><br />

        Document you require:<br />
        <select name="document">
            <option value="Data Sheet">Data Sheet</option>
        </select><br/ ><br />

        Discount code:<br />
        <input type="text" name="discount" size="20" /><br /><br />

        Message:<br />
        <textarea name="message"></textarea><br /><br />
        
        <input type="submit" value="Send">
        <input type="reset" value="Clear">
    </p>
</form>

As others have mentioned, it's not clear what the 500 error is, so that will need to be resolved before this will work. But here's a cleaned up version of the PHP. This has been simply tested at https://3v4l.org/WePRh, though it won't show the form or any input values, but it does prove that the script runs. This is formatted better, includes all form inputs, and should make things easier to work with:

<?php
    // get all form values
    $input['Contact name'] = $_POST['contact_name'] ?? '';
    $input['Firm name'] = $_POST['firm_name'] ?? '';
    $input['Email'] = $_POST['email'] ?? '';
    $input['Job number'] = $_POST['job_number'] ?? '';
    $input['Document'] = $_POST['document'] ?? '';
    $input['Discount'] = $_POST['discount'] ?? '';
    $input['Message'] = $_POST['message'] ?? '';

    // generate the email content (i.e. each line is like "Contact name: Mark")
    $formContent = '';
    foreach($input as $key => $value) {
        $formContent .= $key . ": " . $value . "\n";
    }

    $to = "[email protected]"; // TODO: replace this with your email address
    $subject = "Contact Form";
    
    /*
        Note that this was likely causing downstream issues. Most email clients (gmail, outlook) will mark emails as spam
        if you try to send an email from an email address that your server isn't configured to send from.
        See https://stackoverflow.com/questions/17533863/how-to-configure-php-to-send-e-mail for more info.
        It's also safer to define an email address like "[email protected]" to send your emails from.
    */
    $mailheader = "From: [email protected]";

    mail($to, $subject, $formContent, $mailheader) or die('Error sending email');

    // redirect to the thank you page
    // TODO: update this URL to be yours
    header('Location: http://www.example.com/thankyou.html');

    exit;
?>

CodePudding user response:

You can use the header() function: Example:

header('location: thankyou.html');
  • Related