PHP无法POST /email.php错误

时间:2016-07-23 03:35:30

标签: php html html5 forms wamp

我正在创建一个网站,其中包含一个联系表单,该表单会向访问用户发送电子邮件(姓名,电子邮件,主题和消息)。我已尝试同时使用mail()函数以及PHPMailer库,但在提交表单后立即收到错误Cannot POST /email.php

我认为它与我的HTML代码有关,但我不完全确定。我在下面附上了HTML和PHP代码。

HTML代码

<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代码 - 使用mail()函数

<?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代码 - 使用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!";
}

?>

我检查/尝试过的事情

  1. email.php与html文件位于同一文件夹中。
  2. 将php.ini配置为Gmail SMTP服务器和端口。
  3. 我收到以下错误并已检查但是没有什么应该导致它,因为我在不同的项目上使用了相同的WAMP本地服务器并获得了通过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

2 个答案:

答案 0 :(得分:0)

您的表单/ email.php

的代码中有拼写错误

以主题的输入

的形式
name="sendsubject"

但是在你的email.php中它是

$subject=$_POST['sendersubject'];

鉴于另外两个php输入是sender ...,将表单输入名称更改为:

name="sendersubject"

顺便说一下 - 你还应该包含带有for =&#34;&#34;的标签。将标签链接到每个输入的id的属性。这是为了可访问性和搜索引擎优化目的,以确保标签和输入链接。

答案 1 :(得分:0)

在表单中,从

更改操作参数
action="/email.php"

要:

action="email.php"

问题是第一条路径是相对于网站的根目录,所以表单是POST到example.com/email.php而没有找到任何东西。您会收到404 Not found结果。

第二条路径将在与HTML文件相同的文件夹中查找email.php