-2

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 class="bodytextgrey">
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

mark02
  • 3
  • 3
  • What does the error log tell you about that 500 error? – Nico Haase Oct 20 '22 at 14:22
  • Technically the HTML attributes with a space are ok (https://stackoverflow.com/questions/17396037/can-a-html-input-name-contain-spaces-commas-etc), but you need to access the value like `$_POST['contact name'];` instead of just `$_POST['name'];` – WOUNDEDStevenJones Oct 20 '22 at 14:28
  • do i need to write it in the php file as $contact name = $_POST['contact name']; I tried this and it doidnt work but not sure if its because the html document has it as Contact not contact and not sure if i then need to specify the same further down on the php file as a list of whats to be emailed to me – mark02 Oct 20 '22 at 14:30
  • "do i need to write(...)" - if the error message you've found tells you that this line is a problem, then yes. Otherwise, you are free to name your variables as you like, as long as they are valid (and `$contact name` is invalid due to the space) – Nico Haase Oct 20 '22 at 14:33
  • Im not sure how to access the error log to check – mark02 Oct 20 '22 at 14:37
  • Have a look at https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display or https://stackoverflow.com/questions/43314089/where-is-the-error-log-file – Nico Haase Oct 20 '22 at 14:51
  • All I need really please is someone to write a simple php code based on my html, even if just showing how I can get the mail.php file to send me contact name, and firm name. I can then add in the extra lines of code to cover the other info requested on the html by using the same format. – mark02 Oct 20 '22 at 15:06
  • I don't think the 500 error is related to this PHP script. https://3v4l.org/DjgEt seems to "work" without throwing an error, so you'll need to do more debugging to figure out where the error is coming from. – WOUNDEDStevenJones Oct 20 '22 at 15:10

2 Answers2

1

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 class="bodytextgrey"> <!-- 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 class="bodytextgrey">
        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 = "my@email.com"; // 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 "no-reply@example.com" to send your emails from.
    */
    $mailheader = "From: no-reply@example.com";

    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;
?>
WOUNDEDStevenJones
  • 5,150
  • 6
  • 41
  • 53
-2

You can use the header() function: Example:

header('location: thankyou.html');
  • Please add some explanation to your answer such that others can learn from it. How does your code resolve the 500 error? – Nico Haase Oct 20 '22 at 14:22
  • I believe the issue is Ive taken a HTML to mail template which was a basic email and message requirement, and Ive edited the HTML to add more requests for information on the contact page and then through I could just add a few lines into the mail.php page to get all the data sent via email. But if I add in extra lines the the mail.php it then doesnt work so Im clearly editing the file incorrectly and need help showing me how to add in the other data requests from the html file to my php file so these are emailed when someone submits the form – mark02 Oct 20 '22 at 14:29