dongluo9156 2013-08-04 06:01
浏览 14
已采纳

使用PHP邮件发送邮件()

I've been trying to create a simple contact form using PHP. So, I've managed to get the form to work by plugging in values for all variables like so and it sends the email just fine:

$to = "andrew@mingbogu.com";
$subject = "Job";
$message = "Test";
$from = "example@example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);

However, as soon as I replace the values with $_REQUEST[var], the email gets sent to my Junk email with no from address:

$to = "andrew@mingbogu.com";
$subject = "Job";
$message = $_REQUEST['clientMessage'];
$from = $_REQUEST['clientEmail'];
    $headers  = 'MIME-Version: 1.0' . "
";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "
";
$headers .= "From: $from";
mail($to,$subject,$message,$headers);

Screenshot of the email: enter image description here

I'm getting the values through an ajax form using jQuery:

    var clientName = $("#name").val();
    window.alert(clientName);
    var clientEmail = $("#email").val();    
    window.alert(clientEmail);
    var clientMessage = $("#message").val();            
    window.alert(clientMessage);
     var data = "from=" + clientEmail + "&subject=" + clientName + "&message=" + clientMessage;
     $.ajax({
            type: "POST",
            url: "email.php",
            data: data,
            success: function() {
                $("#loading").fadeOut(100).hide();
                $("#name").val(' ');
                $("#email").val(' ');
                $("#message").val(' ');
                $("#message-sent").fadeIn(100).show();
            }
        });

I've printed out clientName, clientMessage, and clientEmail all right before sending them into the ajax form and they all print out with correct values. Any idea what I'm doing wrong?

HTML form:

 <form id="contactMe" data-abide>
 <input type="text" id="name">
  <input type="text" id="email">        
  <textarea  id="message"></textarea>
 <button type="submit" id="sendEmail" class="button">Submit</button>
  • 写回答

2条回答 默认 最新

  • dongsu3138 2013-08-04 06:34
    关注

    You're using the wrong variable to read the client email. In your JS, you're pulling it into a variable called clientEmail, but then you're giving it the name from when passing the AJAX request:

    var data = "from=" + clientEmail + "&subject=" + clientName + "&message=" + clientMessage;
    

    So from within PHP, you should be accessing it as $_REQUEST['from'], not $_REQUEST['clientEmail'].

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?