2016-11-21 10 views
0
<!DOCTYPE html> 
<html lang="en"> 
<head> 

    <title>AJAX Contact Form Demo</title> 

    <link rel="stylesheet" href="style.css"> 
</head> 
<body> 
    <div id="page-wrapper"> 
     <h1>AJAX Contact Form Demo</h1> 

     <div id="form-messages"></div> 

     <form id="main-contact-form" method="post" action="mailer.php"> 
      <div class="field"> 
       <label for="name">Name:</label> 
       <input type="text" class="form-control" id="name" name="name" required> 
      </div> 

      <div class="field"> 
       <label for="email">Email:</label> 
       <input type="email" class="form-control" id="email" name="email" required> 
      </div> 

      <div class="field"> 
       <label for="message">Message:</label> 
       <textarea id="message" class="form-control" name="message" required></textarea> 
      </div> 

      <div class="field"> 
       <button type="submit" name="submit">Send</button> 
      </div> 
     </form> 
    </div> 

    <script src="jquery-2.1.0.min.js"></script> 
    <script type="text/javascript" src="pap.js"></script> 
</body> 
</html> 

私のAjaxコード:PHP AJAXメーラー問題

$(function() { 

    // Get the form. 
    var form = $('#main-contact-form'); 

    // Get the messages div. 
    var formMessages = $('#form-messages'); 

    // Set up an event listener for the contact form. 
    $(form).submit(function(e) { 
     // Stop the browser from submitting the form. 
     e.preventDefault(); 

     // Serialize the form data. 
     var formData = $(form).serialize(); 

     // Submit the form using AJAX. 
     $.ajax({ 
      type: 'POST', 
      url: $(form).attr('action'), 
      data: formData 
     }) 

     .done(function(response) { 
      // Make sure that the formMessages div has the 'success' class. 
      $(formMessages).removeClass('error'); 
      $(formMessages).addClass('success'); 

      // Set the message text. 
      $(formMessages).text(response); 

      // Clear the form. 
      $('#name').val(''); 
      $('#email').val(''); 
      $('#message').val(''); 

     }) 
     .fail(function(data) { 
      // Make sure that the formMessages div has the 'error' class. 
      $(formMessages).removeClass('success'); 
      $(formMessages).addClass('error'); 

      // Set the message text. 
      if (data.responseText !== '') { 
       $(formMessages).text(data.responseText); 
      } else { 
       $(formMessages).text('Oops! An error occured and your message could not be sent.'); 
      } 
     }); 

    }); 

}); 
ここ

は私のPHPのメーラーコード:

<?php 


if(isset($_POST['submit'])) 
{ 
     $name = strip_tags(trim($_POST["name"])); 
     $name = str_replace(array("\r","\n"),array(" "," "),$name); 
     $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL); 
     $message = trim($_POST["message"]); 


$mess= 
'Full Name: $name<br /> 
Email: $email<br /> 
Comments: $message<br /> 
'; 

    require "phpmailer/class.phpmailer.php"; //include phpmailer class 

    // Instantiate Class 
    $mail = new PHPMailer(); 

    // Set up SMTP 
    $mail->IsSMTP();    // Sets up a SMTP connection 
    $mail->SMTPAuth = true;   // Connection with the SMTP does require authorization  
    $mail->SMTPSecure = "ssl";  // Connect using a TLS connection 
    $mail->Host = "smtp.gmail.com"; //Gmail SMTP server address 
    $mail->Port = 465; //Gmail SMTP port 
    //$mail->Encoding = '7bit'; 

    // Authentication 
    $mail->Username = "Enter Your Email-ID For Testing"; // Your full Gmail address 
    $mail->Password = "Your Email Password"; // Your Gmail password 

    // Compose 
    $mail->SetFrom($_POST['email'], $_POST['name']); 
    $mail->AddReplyTo($_POST['email'], $_POST['name']); 
    $mail->Subject = "hello";  // Subject (which isn't required) 
    $mail->MsgHTML($mess); 
    echo"hello"; 
    // Send To 
    $mail->AddAddress("[email protected]", "Bla Bla"); // Where to send it - Recipient 
    $result = $mail->Send();  // Send! 
    $mess = $result ? 'Successfully Sent!' : 'Sending Failed!';  
    unset($mail); 
    echo 'Successfully Sent!'; 

} 

?> 

私は問題は$混乱キーワードであると思うが、私がしたいですこれらの接頭辞を送信メールの詳細と連結します。

しかし、私もそれを訂正しても、それ以降もメールは送信されません。

誰でもこの問題がなぜ起こっているのか教えてください。

メールを送信するためには、phpmailerクラスを使用してください。

ありがとうございました。

+0

値$ _POST [ '提出']?あなたはどのようなエラーが何を持っていないさを可能にしますか? – madalinivascu

+0

@madalinivascu:このコードには誤りはありません。しかし、メールは行きません。 mailer.phpが呼び出されていないため、Ajaxに問題があります。 –

+0

あなたはmailer.phpを呼び出していないことをどのように知っていますか? – madalinivascu

答えて

0

XAMPP、WAMP、LAMPなどのローカルサーバーを使用している場合は、サーバーのphp.iniにSMTPを設定する必要があるかもしれません

ウェブサーバーにアプリをデプロイする場合は、Gmailアカウントが「安全性の低いアプリ」のアカウントへの接続を許可しているかどうかを確認してください。

あなたはログにエラーを追跡し、私たちの問題に関する詳細な情報をもたらすためにphpmailerのデバッグを使用することがあります。

$ mail-> SMTPDebug = 2; //は、(テスト用)SMTPのデバッグ情報 // 1 =エラーとメッセージ // 2 =メッセージのみ