简体   繁体   English

使用PHP mail()发送邮件

[英]Sending mail using PHP mail()

I've been trying to create a simple contact form using PHP. 我一直在尝试使用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: 但是,一旦我用$ _REQUEST [var]替换了这些值,该电子邮件就会发送到我的垃圾电子邮件,而没有发件人地址:

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

Screenshot of the email: 电子邮件的屏幕截图: 在此处输入图片说明

I'm getting the values through an ajax form using jQuery: 我正在使用jQuery通过ajax表单获取值:

    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. 在将它们发送到ajax表单之前,我已经正确打印了clientNameclientMessageclientEmail ,它们都以正确的值打印出来。 Any idea what I'm doing wrong? 知道我在做什么错吗?

HTML form: HTML形式:

 <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>

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: 在你的JS,你拉入一个变量叫clientEmail ,但后来你给它的名字from通过AJAX请求时:

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

So from within PHP, you should be accessing it as $_REQUEST['from'] , not $_REQUEST['clientEmail'] . 因此,从PHP内部,您应该以$_REQUEST['from']而不是$_REQUEST['clientEmail']身份进行访问。

This is only a "supplement" to the accepted answer (good catch Nick ), since the Email was treated as Junk/Spam. 由于电子邮件被视为垃圾邮件/垃圾邮件,因此这只是对已接受答案 (“很好的昵称” )的“补充”

Adding proper headers such as: 添加适当的标题,例如:

$headers  = "From: $from";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

will help reduce the chance of E-mail ending up in Junk or treated as Spam. 将有助于减少电子邮件最终成为垃圾邮件或被视为垃圾邮件的机会。

Certain web sites will mark it as Spam if proper headers are not included. 如果未包含适当的标题,则某些网站会将其标记为垃圾邮件。

More information on the mail() can be found on PHP.net 可以在PHP.net上找到有关mail()更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM