如何使用AJAX提交带有$ _POST数据的电子邮件

时间:2016-11-07 01:35:19

标签: javascript php ajax email

我想将一些数据从localStorage提交到

Autoform.submitStoredData = function() {
    var data = localStorage.tbRecoveredData;
    if(data) {
        jQuery.ajax ({
            type: "POST",
            url:"http://www.thewebsite.com/Autoform.php",
            data: data,
            success: function() {
                console.log("success");
            },
            error: function(xhr,status,error) {
                console.log("payload failed to submit with xhr: " + xhr + ", status: " + status + ", and error: " + error);
            }
        });
        localStorage.removeItem("tbRecoveredData");
    }
};

到目前为止,我在控制台中取得了“成功”。 Autoform PHP看起来像这样:

<?php

    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        $data = $_POST;
        mail('myemail@gmail.com', 'OK SO here at least is the captured string', $data);
    }

?>

这没有任何作用,或者至少没有发送电子邮件。我承认我对PHP不太了解,我已经尝试了谷歌搜索没有太多运气。我是否需要将其包装在一种自调用函数中,因为它似乎没有执行PHP代码。任何帮助表示赞赏谢谢!

2 个答案:

答案 0 :(得分:2)

好的,如果您尝试使用以下代码并将其命名为NULL

var_dump(mail(...));

然后,您需要配置服务器以使PHP在mail()函数中内置它。有几种方法可以做到:

答案 1 :(得分:0)

如果您想通过功能邮件发送电子邮件()。您必须设置配置。像上面的答案。所以我建议您通过SMTP发送电子邮件。无需配置即可轻松发送。您只需注册一封私人电子邮件,如电子邮件或雅虎。

Refer this Question

// Pear Mail Library
require_once "Mail.php";

$from = '<your@mail.com>'; //change this to your email address
$to = '<someone@mail.com>'; // change to address
$subject = 'Insert subject here'; // subject of mail
$body = "Hello world! this is the content of the email"; //content of mail

$headers = array(
    'From' => $from,
    'To' => $to,
    'Subject' => $subject
);


$smtp = Mail::factory('smtp', array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => '465',
        'auth' => true,
        'username' => 'your@gmail.com', //your gmail account
        'password' => 'snip' // your password
    ));

// Send the mail
$mail = $smtp->send($to, $headers, $body);