使用从ajax请求中检索的数据发送电子邮件

时间:2022-10-16 12:53:27

I'm trying to retrieve information from text fields on button click and send the data to mail@example.com:

我正在尝试通过按钮单击从文本字段中检索信息并将数据发送到mail@example.com:

$(document).ready(function() {
    $("#submit").click(function() {
        var email = $("#email").val();
        var name = $("#Name").val();
        var subject = $("#Subject").val();
        var text = $("#textarea").val();
        var telephone = $("#tel").val();
        var varData = "email : " + email + "name : " + name + "subject : " + subject + "text : " + text + "telephone : " + telephone;
        console.log(varData);

        $.ajax({
            type: "POST",
            url: 'sendPHP.php',
            data: varData,
            success: function() {
                alert("message sent");
            }
        });
    });
});

sendPHP.php:

<?php 
  $to = 'mail@example.com';
  $name = $_POST['email'];
  and so on ..
  ..
  ..
  mail(mail@example.com, name, (here goes email, name, subject, textarea, tel) as message in new lines);
    and somewhere i have to write from what emial im sending to example@gmail.com and what is it's password i guess
?>

1 个解决方案

#1


2  

While it is still somewhat unclear what your exact concern is, I have a few thoughts on your process.

虽然目前还不清楚您的确切关注点,但我对您的流程有一些想法。

1 - You are sending data to a php file as a string which is not recommended and is probably giving you problems right there. I have really seen 2 approaches to sending data to the server via post: A) store your data in a form and use jQuery's $().serialize() function, or B) store your data in a JSON object and send it that way.

1 - 您正在将数据作为字符串发送到php文件,这是不推荐的,并且可能在那里给您带来问题。我已经看到了两种通过post将数据发送到服务器的方法:A)将数据存储在表单中并使用jQuery的$()。serialize()函数,或者B)将数据存储在JSON对象中并以这种方式发送。

Option B is my preferred method because JSON simplifies everything. It has the advantage that your data is already stored as key/value pairs, and PHP makes it super easy to work with JSON using the json_decode function. Let's make a few changes to the existing code you have:

选项B是我首选的方法,因为JSON简化了一切。它的优点是您的数据已经存储为键/值对,而PHP使用json_decode函数可以非常轻松地使用JSON。让我们对您现有的代码进行一些更改:

$(document).ready(function() {
$("#submit").click(function() {
    var email = $("#email").val();
    var name = $("#Name").val();
    var subject = $("#Subject").val();
    var text = $("#textarea").val();
    var telephone = $("#tel").val();
    var varData = {
                     "email"      : email , 
                     "name"       : name, 
                     "subject"    : subject, 
                     "text"       : text ,
                     "telephone"  : telephone
                   } //This is your data in JSON form

    console.log(varData);

    $.ajax({
        type: "POST",
        url: 'sendPHP.php',
        data: JSON.stringifiy(varData), //Notice that I added JSON.stringify
        success: function() {
            alert("message sent");
        }
    });
});

});

Now I'll show you how to handle this in PHP - it's actually very simple.

现在我将向您展示如何在PHP中处理它 - 它实际上非常简单。

Step 1 - turn this JSON object into an associative array so that it's easy to work with:

第1步 - 将此JSON对象转换为关联数组,以便于使用:

$input = json_decode(file_get_contents('php://input'), true);

Now we have a variable called $input that is an associative array with all of your mail data - let's set up the variables you need.

现在我们有一个名为$ input的变量,它是一个包含所有邮件数据的关联数组 - 让我们设置你需要的变量。

$email      = $input['email']; //Notice string in brackets is JSON key
$name       = $input['name'];
$subject    = $input['subject'];
$text       = $input['text'];
$telephone  = $input['telephone'];

Ok, cool - all of your data you gathered from the front end is now ready for use on the back end. So it looks like you're using the built-in mail() function that PHP offers. I would highly recommend using a library for this as they are typically much more reliable and verbose. My favorite is called PHPMailer - here's a link to their github page https://github.com/PHPMailer/PHPMailer.

好的,很酷 - 您从前端收集的所有数据现在都可以在后端使用了。所以看起来你正在使用PHP提供的内置mail()函数。我强烈建议使用一个库,因为它们通常更加可靠和冗长。我最喜欢的是PHPMailer - 这是他们的github页面https://github.com/PHPMailer/PHPMailer的链接。

If you'd like to use that library, the process is simple.

如果您想使用该库,则过程很简单。

First, include the autoloader file in your script

首先,在脚本中包含自动加载器文件

<?php
    require('PHPMailer-master/PHPMailerAutoload.php');

Next, and they have this documented in numerous examples, you create a new instance of PHPMailer and set some basic variables - this is straight from their documentation and I promise you'll have less headache than if you try the PHP mail() approach https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps.

接下来,他们在许多示例中都记录了这一点,您创建了一个PHPMailer的新实例并设置了一些基本变量 - 这可以直接来自他们的文档,我保证您会比尝试PHP mail()方法更轻松。 ://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps。

Best of luck and let me know if I was unclear on anything.

祝你好运,如果我对任何事情都不清楚,请告诉我。

#1


2  

While it is still somewhat unclear what your exact concern is, I have a few thoughts on your process.

虽然目前还不清楚您的确切关注点,但我对您的流程有一些想法。

1 - You are sending data to a php file as a string which is not recommended and is probably giving you problems right there. I have really seen 2 approaches to sending data to the server via post: A) store your data in a form and use jQuery's $().serialize() function, or B) store your data in a JSON object and send it that way.

1 - 您正在将数据作为字符串发送到php文件,这是不推荐的,并且可能在那里给您带来问题。我已经看到了两种通过post将数据发送到服务器的方法:A)将数据存储在表单中并使用jQuery的$()。serialize()函数,或者B)将数据存储在JSON对象中并以这种方式发送。

Option B is my preferred method because JSON simplifies everything. It has the advantage that your data is already stored as key/value pairs, and PHP makes it super easy to work with JSON using the json_decode function. Let's make a few changes to the existing code you have:

选项B是我首选的方法,因为JSON简化了一切。它的优点是您的数据已经存储为键/值对,而PHP使用json_decode函数可以非常轻松地使用JSON。让我们对您现有的代码进行一些更改:

$(document).ready(function() {
$("#submit").click(function() {
    var email = $("#email").val();
    var name = $("#Name").val();
    var subject = $("#Subject").val();
    var text = $("#textarea").val();
    var telephone = $("#tel").val();
    var varData = {
                     "email"      : email , 
                     "name"       : name, 
                     "subject"    : subject, 
                     "text"       : text ,
                     "telephone"  : telephone
                   } //This is your data in JSON form

    console.log(varData);

    $.ajax({
        type: "POST",
        url: 'sendPHP.php',
        data: JSON.stringifiy(varData), //Notice that I added JSON.stringify
        success: function() {
            alert("message sent");
        }
    });
});

});

Now I'll show you how to handle this in PHP - it's actually very simple.

现在我将向您展示如何在PHP中处理它 - 它实际上非常简单。

Step 1 - turn this JSON object into an associative array so that it's easy to work with:

第1步 - 将此JSON对象转换为关联数组,以便于使用:

$input = json_decode(file_get_contents('php://input'), true);

Now we have a variable called $input that is an associative array with all of your mail data - let's set up the variables you need.

现在我们有一个名为$ input的变量,它是一个包含所有邮件数据的关联数组 - 让我们设置你需要的变量。

$email      = $input['email']; //Notice string in brackets is JSON key
$name       = $input['name'];
$subject    = $input['subject'];
$text       = $input['text'];
$telephone  = $input['telephone'];

Ok, cool - all of your data you gathered from the front end is now ready for use on the back end. So it looks like you're using the built-in mail() function that PHP offers. I would highly recommend using a library for this as they are typically much more reliable and verbose. My favorite is called PHPMailer - here's a link to their github page https://github.com/PHPMailer/PHPMailer.

好的,很酷 - 您从前端收集的所有数据现在都可以在后端使用了。所以看起来你正在使用PHP提供的内置mail()函数。我强烈建议使用一个库,因为它们通常更加可靠和冗长。我最喜欢的是PHPMailer - 这是他们的github页面https://github.com/PHPMailer/PHPMailer的链接。

If you'd like to use that library, the process is simple.

如果您想使用该库,则过程很简单。

First, include the autoloader file in your script

首先,在脚本中包含自动加载器文件

<?php
    require('PHPMailer-master/PHPMailerAutoload.php');

Next, and they have this documented in numerous examples, you create a new instance of PHPMailer and set some basic variables - this is straight from their documentation and I promise you'll have less headache than if you try the PHP mail() approach https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps.

接下来,他们在许多示例中都记录了这一点,您创建了一个PHPMailer的新实例并设置了一些基本变量 - 这可以直接来自他们的文档,我保证您会比尝试PHP mail()方法更轻松。 ://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps。

Best of luck and let me know if I was unclear on anything.

祝你好运,如果我对任何事情都不清楚,请告诉我。