0

I have a "Send to friend" php that is working great if you use a normal php post, This way I know this PHP can send mails. so the problem is not sendtomail.php which I also will post at the bottom.

/* Código AJAX Send to Friend*/
$(function() {
  $('.error').hide();
  $('input.text-input').css({backgroundColor:"#FFFFFF"});
  $('input.text-input').focus(function(){
    $(this).css({backgroundColor:"#FFDDAA"});
  });
  $('input.text-input').blur(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
  });

  $(".enterrenvi").click(function() {
        // validate and process form
        // first hide any error messages
    $('.error').hide();
        
      var youremailaddress = $("input#youremailaddress").val();
        if (youremailaddress == "") {
      $("label#youremailaddress_error").show();
      $("input#youremailaddress").focus();
      return false;
    }
        var friendsemailaddress = $("input#friendsemailaddress").val();
        if (friendsemailaddress == "") {
      $("label#friendsemailaddress_error").show();
      $("input#friendsemailaddress").focus();
      return false;
    }
    
        
        var dataString = 'youremailaddress='+ youremailaddress + '&friendsemailaddress=' + friendsemailaddress;
        //alert (dataString);return false;
        
        $.ajax({
      type: "POST",
      url: "sendtofriend.php",
      data: dataString,
      success: function() {
        $('#message').html("<div id='messagein'></div>");
        $('#messagein').html("<h2>Contact Form Submitted!</h2>")
        .append("<p>We will be in touch soon.</p>")
        .hide()
        .fadeIn(1500, function() {
          $('#messagein').append("<img id='checkmark' src='images/check.png' />");
        });
      }
     });
    return false;
    });
});
$(document).ready(function(){
  $("input#youremailaddress").select().focus();
});

Sendtofriend.php

<?PHP
$uemail = $_POST["youremailaddress"]; 
$femail = $_POST["friendsemailaddress"];
$message = "myfootballproject.com";
$link = $_SERVER['HTTP_REFERER'];

$to = "$femail";
$subject = "Tu amigo $uemail te invita a conocer My Football Project";
$headers = "From: $uemail\n";
$message = "Hi $femail.  Te invitamos a conocer My Football Project.
$link
$message";

if (mail($to,$subject,$message,$headers) ){ echo "se envío el correo"; }
else { echo "fallo el envío";};


?>

Form:

<div id='message'>
    <div id='messagein'></div>
<form action="" method="post" id="sendfriendd">

<div id="inpumail" >

    <input type="text" name="youremailaddress" id="youremailaddress" size="40" value="" class="text-input" />  
    <label class="error" for="youremailaddress" id="youremailaddress_error">This field is required.</label>  

</div>


<br>
<div id="inpumail2" >

   
    <input type="text" name="friendsemailaddress" id="friendsemailaddress" size="40" value="<%= t('generales.amigcorreoo') %>" class="text-input" />  
    <label class="error" for="friendsemailaddress" id="friendsemailaddress_error">This field is required.</label>

</div>


<br>
<input type="submit" name="Submit" value=" <%= t('generales.enviarcorreoo') %> " class="enterrenvi">
</form>
</div>
4

1 回答 1

0

您正在尝试发送POST带有GET参数的数据。

看看这里的数据字符串。得到:

    var dataString = 'youremailaddress='+ youremailaddress + '&friendsemailaddress=' + friendsemailaddress;

但你实际上提出了一个POST请求:

    $.ajax({
         type: "POST",

要解决这个问题,要么使用$_GET,要么继续使用$_POSTjQuery 的$.post方法。不过,我强烈建议在这种情况下使用 POST。

另外,尽量不要依赖 PHP 的mail()方法。将 SMTP 与 Swiftmailer 之类的类一起使用。

于 2013-09-11T10:34:57.353 回答