ajax php post不发送电子邮件

时间:2016-10-26 10:47:42

标签: javascript php jquery ajax

我不确定为什么当我访问php脚本并手动发送帖子数据时它不起作用然而当我通过js发送它得到正确的响应(成功)但电子邮件不发送

形式:

<div name="submited" id="submitted" class="col-md-6 col-sm-6 contact_form">
                    <div class="con-fm">
                                                  <div class="col-sm-12">
            <p class="contact-success" id="contact-success">Thank you, you have now emailed scottish enterprise</p>                 
                <p class="contact-error">Error! please contact site administrators</p>
                <p class="contact-error-1">Error! Please fill in all the form fields</p>
                                    <p class="contact-error-2">Error! You need to enter your name</p>
                                    <p class="contact-error-3">Error! You need to enter you email address</p>
                                    <p class="contact-error-4">Error! You need to enter a message</p>
                                    <p class="contact-error-5">Error! File type should be .Docx, .PDF, .RTF or .Doc</p>
                                </div>
                        <form id="hideme" method="POST" enctype="multipart/form-data">
                          <div class="form-group">
                            <label for="name">Name:</label>
                            <input type="text" class="form-control" name="name" id="name">
                          </div>
                          <div class="form-group">
                            <label for="email">Email:</label>
                            <input type="email" class="form-control" name="email" id="email">
                          </div>
                            <div class="form-group">
                              <label for="comment">Message:</label>
                              <textarea class="form-control" rows="5" name="comment" id="comment"></textarea>
                            </div>
                            <div class="form-group">
                              <label for="att">Attachment:</label>
                             <div class="row">
                                <div class="col-md-5">
                                    <div>
                                                                                   <div><input id="upload" name="file" type="file" id="file" /></div>



                                    </div>
                                </div>
                                <div class="col-md-1">

                                </div>
                                <div class="col-md-5 btn11">
                                    <button value="submit" name="submit" id="submit" class="btn btn-default">Send Email</button>
                                </div>
                             </div>
                            </div>                              
                        </form>
                    </div>
                </div>

js:

   <script type="text/javascript">
  $(function() {
    $(".contact_form").submit(function() {
        var email = $("#name").val();
        var name = $("#email").val();
        var comment = $("#comment").val();
        var file = $("#file").val();
        var submit = $("#submit").val();

      $.ajax({
        url: "email.php",
        type: "POST",
        data: {email: email, name: name, comment: comment, file: file, submit: submit},
        dataType: "json",
        success: function(data) {
      if(data.status == 'success'){
           $('#hideme').fadeIn().delay(0).fadeOut();
             $('#contact-success').fadeIn().delay(3000);
            }else if(data.status == 'error'){
         $('.contact-error').fadeIn().delay(6000).fadeOut();
     }else if(data.status == 'error1'){
         $('.contact-error-1').fadeIn().delay(6000).fadeOut();
     }else if(data.status == 'error2'){
         $('.contact-error-2').fadeIn().delay(6000).fadeOut();
     }else if(data.status == 'error3'){
         $('.contact-error-3').fadeIn().delay(6000).fadeOut();
     }else if(data.status == 'error4'){
         $('.contact-error-4').fadeIn().delay(6000).fadeOut();
     }else if(data.status == 'error5'){
         $('.contact-error-5').fadeIn().delay(6000).fadeOut();
     }
     },
     error: function(msg) {
         $('.contact-error').fadeIn().delay(6000).fadeOut();
     }
      });
      return false;
    });
  });
</script>

php:

    <?php

     function remove_bad ($input){

            $output = htmlentities(trim($input));

            return $output;


        }

        function error ($input){

            $output = $input;

            return $output;

        }

    if(isset($_POST['name']) && isset($_POST['email']) && isset($_POST['comment']) && isset($_POST['submit']) 
            && !empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['comment']) && !empty($_POST['submit'])){



        $name = remove_bad($_POST['name']);
        $email = remove_bad($_POST['email']);
        $comment = remove_bad($_POST['comment']);
        if(isset($_FILES['file']['name']) && !empty($_FILES['file']['name'])){
    $folder = "upload/";
    $temp = explode(".", $_FILES["file"]["name"]);
    $newfilename = round(microtime(true)).'.'. end($temp);
    $db_path ="$folder".$newfilename  ;
    $listtype = array(
    '.doc'=>'application/msword',
    '.docx'=>'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
    '.rtf'=>'application/rtf',
    '.pdf'=>'application/pdf'); 
    if ( is_uploaded_file( $_FILES['file']['tmp_name'] ) )
    {
    if($key = array_search($_FILES['file']['type'],$listtype))
    {if (move_uploaded_file($_FILES['file']  ['tmp_name'],"$folder".$newfilename))
    {

    require_once('mail/class.phpmailer.php');
        $emaila = new PHPMailer();
    $emaila->From      = $email;
    $emaila->FromName  = $name;
    $emaila->Subject   = 'Capital';
    $emaila->Body      = $comment;
    $emaila->AddAddress( 'myemail@email.com' );

    $file_to_attach = $folder.$newfilename;
    $emaila->AddAttachment( $file_to_attach , $folder.$newfilename );
    $emaila->Send();

    $response_array = array();
    $response_array['status']  = 'success';
    header('Content-type: application/json');
        echo json_encode($response_array);
    //exit(header("Location: index.php#submitted"));





    }

    }else{
         $response_array = array();
    $response_array['status']  = 'error5';
    header('Content-type: text/html');
        echo json_encode($response_array);
        //$error = "File type should be .Docx, .PDF, .RTF or .Doc";
    }
    }
    else    
    {
         $response_array = array();
    $response_array['status']  = 'error5';
    header('Content-type: application/json');
        echo json_encode($response_array);
    //$error = "File Type Should Be .Docx or .Pdf or .Rtf Or .Doc";
    }
        }else{

              require_once('mail/class.phpmailer.php');
        $emaila = new PHPMailer();
    $emaila->From      = $email;
    $emaila->FromName  = $name;
    $emaila->Subject   = 'Capital';
    $emaila->Body      = $comment;
    $emaila->AddAddress( 'myemail@email.com' );


    $emaila->Send();    
     $response_array = array();
    //$response_array['status']  = 'success';
     $response_array['status']  = 'success';
    header('Content-type: application/json');
        echo json_encode($response_array);



        }



    }elseif(isset($_POST['name']) && isset($_POST['email']) && isset($_POST['comment']) && isset($_POST['submit']) 
            && !empty($_POST['name']) && !empty($_POST['email']) && empty($_POST['comment']) && !empty($_POST['submit'])){

     // $error = error("You need to enter a message");
       $response_array = array();
    $response_array['status']  = 'error4';
    header('Content-type: application/json');
        echo json_encode($response_array);

    }elseif(isset($_POST['name']) && isset($_POST['email']) && isset($_POST['comment']) && isset($_POST['submit']) 
            && !empty($_POST['name']) && empty($_POST['email']) && !empty($_POST['comment']) && !empty($_POST['submit'])){

      // $error = error("You need to enter you email address");
       $response_array = array();
    $response_array['status']  = 'error3';
    header('Content-type: application/json');
        echo json_encode($response_array);

    }elseif(isset($_POST['name']) && isset($_POST['email']) && isset($_POST['comment']) && isset($_POST['submit']) 
            && empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['comment']) && !empty($_POST['submit'])){

        //$error = error("You need to enter your name");

     $response_array = array();
    $response_array['status']  = 'error2';
    header('Content-type: application/json');
        echo json_encode($response_array);

    }elseif(empty($_POST['name']) or empty($_POST['email']) or empty($_POST['comment']) && !empty($_POST['submit'])){



   //  $error = error("Please fill in all the form fields");

     $response_array = array();
    $response_array['status']  = 'error1';
    header('Content-type: text/html');
        echo json_encode($response_array);

    }




    ?>

1 个答案:

答案 0 :(得分:0)

$('body').on('click', '#product_edit_custom_add_btn', function () {

    var MTitle = $('#custom_product_edit_name_tb').val();
    var MDescription = $('#custom_product_edit_description_tb').val();
    var ProductTitle = $('#custom_product_edit_name_tb').val();
    var ProductPrice = $('#custom_product_edit_price_tb').val();
    var ProductUrl = $('#custom_product_edit_website_tb').val();
    var LeftTimeLimit = $('#startTime').text();
    var RightTimeLimit = $('#endTime').text();


    var file_data = $("#edit_product_photo_uploader").prop("files")[0];   
    console.log("filesss", file_data);
    var form_data = new FormData();                  
    form_data.append("VideoId", MindexVideoIdG)              
    form_data.append("SetId", MindexSetIdG)
    form_data.append("StartTime", LeftTimeLimit)
    form_data.append("EndTime", RightTimeLimit)
    form_data.append("MType", 3)
    form_data.append("MSubType", 3)
    form_data.append("MTitle", MTitle)
    form_data.append("MDescription", MDescription)
    form_data.append("ProductTitle", ProductTitle)
    form_data.append("ProductPrice", ProductPrice)
    form_data.append("ProductUrl", ProductUrl)
    form_data.append("file", file_data)


    if (MTitle == "" || file_data == undefined) {
        $('#custom_product_edit_name_tb').css("border", "2px red solid");
        $('#edit_product_photo_prev').css("border", "2px red solid");
    }
    else {
        $.ajax({
            cache: false,
            contentType: false,
            processData: false,
            url: '/MindexEditor/AddCustomProduct',
            method: 'POST',
            data: form_data,
            success: function (response) {
                if (response.status == "success") {
                    EmptyEditSectionCustomProductTB();
                    UpdateProductViewSection();
                    UpdateCustomProductManagerView();
                    showToast("success", "New Person Added Successfully");

                }
            }
        })
    }
});

您需要以这种方式发送文件和数据。只有这样才能奏效。