发送邮件不起作用

时间:2015-02-12 13:17:24

标签: javascript php html email

我是Bootstrap的新手(提前道歉),我想做的事情非常简单。从HTML中的FORM中捕获详细信息>在提交时调用javascript>调用php启动POST>发送电子邮件到我的邮件服务器。

我实现这一目的的代码是孤立的,但是当我将这些元素嵌入到我的主HTML / JS / PHP环境中时,我一直得到“抱歉......邮件服务器没有响应”#39;来自我的PHP脚本的消息。

此问题在性质上与前一个线程类似,但因脚本始终返回错误而有所不同。

这是HTML处理FORM:

    <div class="container"> 
    <div id="contacts">
        <div class="row">   
     <!-- Alignment -->
            <div class="col-sm-offset-3 col-sm-6">
       <!-- Form itself -->
                <form name="sentMessage" class="well" id="contactForm"  novalidate>
                <legend>Contact Us v0.1</legend>
                <div class="control-group">
                    <div class="controls">
                        <input type="text" class="form-control" 
                        placeholder="Name" id="name" required
                        data-validation-required-message="Please enter your name" />
                        <p class="help-block"></p>
                    </div>
                </div>  
                <div class="control-group">
                    <div class="controls">
                        <input type="email" class="form-control" 
                        placeholder="Email" id="email" required
                        data-validation-required-message="Please enter your email" />
                    </div>
                </div>  
                <div class="control-group">
                    <div class="controls">
                        <input type="text" class="form-control" 
                        placeholder="Mobile No (Optional)" id="mobile"/>
                    </div>
                </div>                

               <div class="control-group">
                 <div class="controls">
                 <textarea rows="10" cols="100" class="form-control" 
                       placeholder="Message" id="message" required
               data-validation-required-message="Please enter your message" minlength="5" 
                       data-validation-minlength-message="Min 5 characters" 
                        maxlength="999" style="resize:none"></textarea>
        </div>
               </div>        
         <div id="success"> </div> <!-- For success/fail messages -->
        <button type="submit" class="btn btn-primary pull-right">Send</button><br />
          </form>
    </div>
      </div>
    </div>
   </div>

    <!-- Core JavaScript Files -->
    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/jqBootstrapValidation.js"></script>
    <script src="js/jquery.easing.min.js"></script> 
    <script src="js/jquery.scrollTo.js"></script>
    <script src="js/wow.min.js"></script>

    <!-- Custom Theme JavaScript -->
    <script src="js/custom.js"></script>
    <script src="js/contact_me.js"></script>

这是contact_me.js

/*
  Jquery Validation using jqBootstrapValidation
   example is taken from jqBootstrapValidation docs 
  */
$(function() {

 $("input,textarea").jqBootstrapValidation(
    {
     preventSubmit: true,
     submitError: function($form, event, errors) {
      // something to have when submit produces an error ?
      // Not decided if I need it yet
     },
     submitSuccess: function($form, event) {
      event.preventDefault(); // prevent default submit behaviour
       // get values from FORM
       var name = $("input#name").val();  
       var email = $("input#email").val(); 
       var mobile = $("input#mobile").val();
       var message = $("textarea#message").val();
       var firstName = name; // For Success/Failure Message
           // Check for white space in name for Success/Fail message
        if (firstName.indexOf(' ') >= 0) {
       firstName = name.split(' ').slice(0, -1).join(' ');
         }        
     $.ajax({
                url: "./bin/contact_me.php",
                type: "POST",
                data: {name: name, email: email, mobile: mobile, message: message},
                cache: false,
                success: function() {  
                // Success message
                   $('#success').html("<div class='alert alert-success'>");
                   $('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                    .append( "</button>");
                  $('#success > .alert-success')
                    .append("<strong>Your message has been sent to dont.reply@cheerful.com. </strong>");
          $('#success > .alert-success')
            .append('</div>');

          //clear all fields
          $('#contactForm').trigger("reset");
          },
       error: function() {      
        // Fail message
         $('#success').html("<div class='alert alert-danger'>");
                $('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                 .append( "</button>");
                $('#success > .alert-danger').append("<strong>Sorry "+firstName+" it seems that my mail server is not responding...</strong> Could you please email me directly to <a href='mailto:dont.reply@cheerful.com?Subject=Message_Me from dontreply.com'>dont.reply@cheerful.com</a> ? Sorry for the inconvenience!");
            $('#success > .alert-danger').append('</div>');
        //clear all fields
        $('#contactForm').trigger("reset");
        },
           })
         },
         filter: function() {
                   return $(this).is(":visible");
         },
       });

      $("a[data-toggle=\"tab\"]").click(function(e) {
                    e.preventDefault();
                    $(this).tab("show");
        });
  });

/*When clicking on Full hide fail/success boxes */ 
$('#name').focus(function() {
     $('#success').html('');
  });

这是contact_me.php

<?php
// check if fields passed are empty
if(empty($_POST['name'])        ||
   empty($_POST['email'])       ||
   empty($_POST('mobile'])      ||
   empty($_POST['message']) ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
    echo "No arguments Provided!";
    return false;
   }

$name = $_POST['name'];
$email_address = $_POST['email'];
$mobile = $_POST['mobile'];
$message = $_POST['message'];

// create email body and send it    
$to = 'dont.reply@cheerful.com'; // put your email
$email_subject = "Contact form submitted by:  $name";
$email_body = "Hi, you have received a new message. \n\n".
                  "Here are the details:\n \nName: $name \n ".
                  "Mobile: $mobile \n ".
                  "Email: $email_address\n Message \n $message";
$headers = "From: Contact_Form_v1\n";
$headers .= "Reply-To: $email_address"; 
mail($to,$email_subject,$email_body,$headers);
return true;            
?>

我的index.html是根级别,contact_me.js位于文件夹root / js中,而我的contact_me.php位于文件夹root / bin中。不确定是否存在未找到php脚本的问题。我的主机提供使用Linux服务器。

任何帮助都非常感激。

0 个答案:

没有答案