【问题标题】:How to send an email with $_POST data submitted with AJAX如何使用 AJAX 提交包含 $_POST 数据的电子邮件
【发布时间】:2017-03-20 06:57:07
【问题描述】:

我想从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 代码没有被执行。任何帮助表示感谢!

【问题讨论】:

  • 您可以通过输入var_dump(mail(...)) 来检查发生了什么并告诉我们结果吗?看起来mail() 配置不正确。
  • var_dump 是否在某处记录了某些内容?我按照你说的做了,但是不知道去哪里找结果
  • 你需要检查你的输出。不要在您当前的实现中尝试它。使用新的.php 文件并尝试测试您的邮件函数是否返回 true 或 fals.e
  • 好吧,我试试看
  • 它返回 NULL - 所以我确定这就是问题现在如何解决 - 那么一些配置?

标签: javascript php ajax email


【解决方案1】:

如果您想通过函数 mail() 向您发送电子邮件。您必须设置配置。就像上面的答案。所以我建议你通过 SMTP 发送电子邮件。无需配置即可轻松发送。你只需要注册一个私人邮箱,比如Email或者Yahoo。

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

【讨论】:

    【解决方案2】:

    好的,如果您尝试输入以下代码并发现它是NULL

    var_dump(mail(...));
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多