简体   繁体   中英

PHP cannot POST /email.php error

I am creating a website with a contact form that sends an e-mail to me with information from the visiting user (name, email, subject and message). I have tried using both the mail() function as well as the PHPMailer library, but keep getting the error Cannot POST /email.php as soon as I submit the form.

I think it has something to do with my HTML code, but I am not entirely sure. I have attached both the HTML and PHP code below.

HTML Code

<div class="col-md-9  wow fadeInRight animated">
                    <form class="contact-form" action="/email.php" method="post" >
                        <div class="row">
                            <div class="col-md-6">
                                <input type="text" class="form-control" name="sender" id="sender" placeholder="Name">
                                <input type="email" class="form-control" name="senderemail" id="email" placeholder="Email">
                                <input type="text" class="form-control" name="sendsubject" id="subject" placeholder="Subject">
                            </div>
                            <div class="col-md-6">
                                <textarea class="form-control" style="resize:none" name="sendermessage" id="message" rows="25" cols="20" placeholder="Type message here..."></textarea>
                                <input type="submit" name="submit" class="btn btn-default submit-btn form_submit" value="SEND MESSAGE"></input>
                            </div>
                        </div>
                    </form>
                </div>

PHP Code - Using mail() function

<?php

if(isset($_POST['submit'])) {
    $recipient='naman.mandhan@gmail.com';
    $subject=$_POST['sendersubject'];
    $sender=$_POST['sender'];
    $senderEmail=$_POST['senderemail'];
    $message=$_POST['sendermessage'];

    //$mailBody="Name: $sender\nEmail: $senderEmail\n\n$message";

    mail($recipient, $subject, $mailBody, "From: $sender <$senderEmail>");

}
?>

PHP Code - Using PHPMailer

<?php
require 'PHPMailer/PHPMailerAutoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer;

//Tell PHPMailer to use SMTP
$mail->isSMTP();

//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;

//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';

//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6

//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;

//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "naman.mandhan@gmail.com";

//Password to use for SMTP authentication
$mail->Password = "nahdnam";

//Set who the message is to be sent from
$mail->setFrom($senderEmail, $sender);

//Set an alternative reply-to address
//$mail->addReplyTo('replyto@example.com', 'First Last');

//Set who the message is to be sent to
$mail->addAddress($recipient, 'Naman Mandhan');

//Set the subject line
$mail->Subject = $subject;

//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
//$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));

//Include the sender message in the body
$mail->Body = $message;
$mail->AltBody = $message;

//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');

//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}

?>

Things I Have Checked/Tried

  1. email.php is in the same folder as the html file.
  2. Configured php.ini to the Gmail SMTP server and port.
  3. I receive the following error and have checked but there is nothing that should be causing it, since I have used the same WAMP local server on a different project and gotten the code to write data into a database through PHP.

    [21-Jul-2016 23:52:23 UTC] PHP Warning: PHP Startup: Unable to load dynamic library 'c:/wamp/bin/php/php5.5.12/ext/php_intl.dll' - The specified module could not be found.

    in Unknown on line 0

    [21-Jul-2016 23:52:23 UTC] PHP Warning: PHP Startup: Unable to load dynamic library 'c:/wamp/bin/php/php5.5.12/ext/php_ldap.dll' - The specified module could not be found.

    in Unknown on line 0

you have a typo in the code for your form / email.php

in the form the input for subject is

name="sendsubject"

but in your email.php it is

$subject=$_POST['sendersubject'];

Given the other two php inputs are sender..., it would be easier to change the form input name to be:

name="sendersubject"

Incidentally - you should also include labels with a for="" attribute that links the label to the id of each input. This is for accessibility and SEO purposes to ensure that the label and input are linked.

In your form, change the action parameter from

action="/email.php"

To:

action="email.php"

The problem was that the first path is relative to the root of the website, so the form is POSTing to example.com/email.php and not finding anything. You get a 404 Not found result.

The 2nd path will look for email.php in the same folder as the HTML file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM