错误,没有从联系表单接收电子邮件

时间:2015-05-06 11:55:47

标签: php html ajax

我正在尝试创建一个只是将表单发送到我的电子邮件的联系表单,我遇到了一些问题,当用户提交表单时它会响应错误,但也会显示正确发送的消息但是我没有收到任何消息。

这是错误

<b>Notice</b>:  Undefined index: contactName1 in <b>/opt/lampp/htdocs/site/contact.php</b> on line <b>8</b><br />

如果有助于我在本地服务器(lampp)上运行我的网站。

这是php

    <?php


include('scripts/connection.php');


$consubject= test_input($_POST['contactSubject1']);
$conname= test_input($_POST['contactName1']);
$conemail= test_input($_POST['contactEmail1']);
$conmessage= test_input($_POST['contactMessage1']);

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}


$email_to = "myemail@gmail.com";






    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

  if(!preg_match($email_exp,$conemail)) {


    echo "The Email address ".$conemail." is not valid";

  }else{

      // create email headers

        $email_message = "Form details below.\n\n";



        function clean_string($string) {

          $bad = array("content-type","bcc:","to:","cc:","href");

          return str_replace($bad,"",$string);

        }



        $email_message .= "Name: ".clean_string($conname)."\n";

        $email_message .= "Message: ".clean_string($conmessage)."\n";


 $from="From: $conname<$conemail>\r\nReturn-path: $conemail";
        $consubject="Message sent using your contact form";
        mail("myemail@gmail.com", $consubject, $email_message, $from); 


       // echo "Thanks for your message";

  }


?>

这是javascript

$("#contactSubmit").click(function(){
        var contactSubject = $("#contactSubject").val();
        var contactName = $("#contactName").val();
        var contactEmail = $("#contactEmail").val();
        var contactMessage = $("#contactMessage").val();

        // Returns successful data submission message when the entered information is stored in database.
        var dataStringContact = 'contactSubject1='+ contactSubject + 'contactName1='+ contactName + '&contactEmail1='+ contactEmail + '&contactMessage1='+ contactMessage;
        if(contactSubject===''||contactName===''||contactEmail===''||contactMessage==='')
        {
            alert("Please Fill All Fields");
        }
        else
        {
            // AJAX Code To Submit Form.
            $.ajax({
            type: "POST",
            url: "contact.php",
            data: dataStringContact,
            cache: false,
            success: function(resultContact){
                alert(resultContact);
                document.forms['contactForm'].reset();
            }
            });
        }
        return false;
    });

这是html

<form name="contactForm">

                                    <input id="contactSubject" name="contactSubject" type="text"class="inputs" placeholder="Subject" /> 
                                    <br> 
                                    <input id="contactName" name="contactName" type="text"class="inputs" placeholder="Name" /> 
                                    <br> 
                                    <input id="contactEmail" name="contactEmail" type="text"class="inputs" placeholder="E-mail" /> 
                                    <br> 
                                     <textarea rows="4" cols="50" id="contactMessage" name="contactMessage" type="text" class="inputs" placeholder="Write your message..."></textarea>  

                                    <br> <br>
                                     <input id="contactSubmit" type="button" name="contactSubmit" class="submitButton" value="Submit" />

                                 </form>

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

您错过了&变量中的dataStringContact(JavaScript代码段中的第8行):

var dataStringContact = 'contactSubject1='+ contactSubject + '&contactName1='+ contactName + '&contactEmail1='+ contactEmail + '&contactMessage1='+ contactMessage; 
                                                              ^

答案 1 :(得分:0)

您必须先检查是否已正确设置SMTP服务器。 如果您还没有设置任何内容,我建议您使用任何第三方库发送PHPMailer之类的电子邮件 PHPMailer