0

我尝试使用 PhpMailer 和 jquery ajax 发送和自动邮件,但我没有让它工作。当我禁用 ajax 功能并正常发送表单时,邮件会正确发送,但是当我打开 ajax 功能时它不起作用。我认为他们的问题在于 $mail->AddAddress 因为如果我手动设置它:

$mail->AddAddress=("emailexample.com")

发送邮件(即使使用 ajax)

但如果我像这样动态设置它:

$mail->AddAddress=($email)

它在使用 ajax 时不起作用。

这是我的代码。

<form id="reserva" name="reserva" action="http://elsalvadorenelmundo.com/restaurantes/mail.php" method="post" >


                            <input type="hidden" id="date" name="date" />

                           <div style=" padding-top:15px;"> 

                            <select name="hora"  id="hora">

                                <option value=11>11</option>
                                <option value="12">12</option>

                            </select>


                            :

                            <select name="minuto" id="minuto">

                                <option value="00">00</option>
                                <option value="15">15</option>

                            </select>
                            <input type="hidden" name="mail" value="<?php echo $row['mail']; ?>" />

                            <input type="text" name="name" id="name" size="25" value="" class="text-input" />  



                            <input type="text" name="lname" id="lname" size="25" value="" class="text-input" />  
                           >


                            <input type="text" name="cel" id="cel" size="25" value="" class="text-input" />  



                            <input type="text" name="email" id="email" size="25" value="" class="text-input" />  

                            <div style="float:right;">
                            <input type="text" name="comensales" id="comensales" size="5" value="" maxlength="3" class="text-input" />  

                            <input type="hidden" id="nombre" name="date" value="<?php echo $row['tittle']; ?>" />


                       <div id="tester" title="Reservar"><button id="chooseDateBtnSidebar" type="submit" name="submit" class="submitBtn  big sidebarbutton boton"  ><span>Reservar</span></button></div>

                    </div>

                </form>

jQuery:

     $("#submit").click(function() {  

  $('.error').hide();  
    var mail= $("input#mail").val(); 
    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

            if (mail == "") {  
              $("span#textfieldRequiredMsg").show();  
              return false; 
            }else if (!emailReg.test(mail)){  
              $("span#textfieldInvalidFormatMsg").show();  
              return false; 
            }


  })

  $('.error').hide();  
  $(".boton").click(function() {  
    // validate and process form here  
        $('.error').hide();  
            var date = $("input#date").val();  
            if (date == "") {  
              $("div#date_error").show();  
              return false; 
            } 
              var hora = $("select#hora").val();  
                if (hora == "") {  
              $("label#hora_error").show();  
              $("select#hora").focus();  
              return false;  
            }  
              var minuto = $("select#minuto").val();  
                if (minuto == "") {  
              $("label#hora_error").show();  
              $("select#hora").focus();  
              return false;  
            }  
            hora = hora + ":" + minuto;

              var name = $("input#name").val();  
                if (name == "") {  
              $("label#name_error").show();  
              $("input#name").focus();  
              return false;  
            }  
              var lname = $("input#lname").val();  
                if (lname == "") {  
              $("label#lname_error").show();  
              $("input#lname").focus();  
              return false;  
            }  
              var cel = $("input#cel").val();  
                if (cel == "") {  
              $("label#cel_error").show();  
              $("input#cel").focus();  
              return false;  
            }  
                var email = $("input#email").val();  
                if (email == "") {  
              $("label#email_error").show();  
              $("input#email").focus();  
              return false;  
            }   

                var comensales = $("input#comensales").val();  
                if (comensales == "") {  
              $("label#comensales_error").show();  
              $("input#comensales").focus();  
              return false;  
            }   
            var nombre= $("input#nombre").val();
            var mail= $("input#mail").val();

            var dataString = 'date='+ date + '&hora='+ hora + '&name='+ name + '&nombre='+ nombre + '&lname=' + lname + '&cel=' + cel + '&email=' + email  + '&mail=' + mail + '&comensales=' + comensales + '&submit=' + "submit"; 
        //  alert (dataString);return false;  

            $.ajax({  
              type: "POST",  
              url: "/restaurantes/mail.php",  
              data: dataString,  
              success: function() {  
                top.location.href = '#';
                $('#weekendReservation').html("<div class='tiempo'></div>");  
                $('.tiempo').html("<h3>Su reserva no ha sido procesada</h3>")  
                .append("<p>Lo sentimos pero tu reserva no ha podido ser procesada, el restaurante no posee mesas disponibles para la fecha que has seleccionado. Si tienes cualquier problema no dudes en contactarnos.</p> ")  

                .hide()  
                .fadeIn(5500, function() {  
                  $('#message').append("<img id='checkmark' src='images/check.png' />");  
                });  
              }  
            });  
            return false;  

    }); 

和 mail.php

<?php 
require_once("class.phpmailer.php");
require_once("class.smtp.php");
$email= $_POST['mail'];
var_dump($_POST);
if(isset($_POST['submit'])){
    //Datos de la persona
    $msj= "<strong>Nombre:</strong> ";
    $msj.= $_POST['name'] . " " . $_POST['lname'];
    $msj.= "<br/> <strong>Telefono: </strong>";
    $msj.= $_POST['cel'];
    $msj.= "<br/> <strong>Email: </strong>";
    $msj.= $_POST['email'];
    //Datos de la reserva
    $msj.= "<br/><br/> <strong>Datos de la Reservaci&oacute;n</strong><br/>";
    $msj.= "<strong>Fecha:</strong> ";
    $msj.= $_POST['date'];
    $msj.= "<br/><strong>Hora: </strong>";
    $msj.= $_POST['hora'];
    $msj.= "<br/><strong>N&deg; de personas: </strong>";
    $msj.= $_POST['comensales'];
    $msj.= "<br/><br/> <strong>Restaurante: </strong>";
    $msj.= $_POST['nombre'];
    // PHPMailer's Object-oriented approach
    $mail = new PHPMailer();

// Can use SMTP
// comment out this section and it will use PHP mail() instead
$mail->IsSMTP();
$mail->SMTPSecure = "ssl";
$mail->Host = "x.x.x";
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->Port = 465;
$mail->IsHTML(true);
$mail->Username = "xxxxxxxxx";
$mail->Password = "xxxxxxxxx";

    // Could assign strings directly to these, I only used the 
    // former variables to illustrate how similar the two approaches are.
    $mail->FromName = "El Salvador en el Mundo";
    $mail->From     = "mail@mail.com";
    $mail->AddAddress($email);
    $mail->AddAddress("mail@mail.com");

    $mail->Subject  = "Reservaci&oacute;n para ". $_POST['name']. " " . $_POST['lname']. " ({$_POST['comensales']} personas)";
    $mail->Body     = $msj;

    $result = $mail->Send();

}

?>

我不明白,但是当我关闭 ajax 功能时,它工作得很好。我需要帮助!!

4

1 回答 1

1

更改$email= $_POST['mail'];$email = $_POST['email'];

如果这不是问题,那么请确保dataString作为 ajax 请求的一部分包含您需要的所有参数(例如两者mailemail)。

于 2013-01-22T00:49:15.940 回答