【问题标题】:Sending an email with AngularJS and PHP?使用 AngularJS 和 PHP 发送电子邮件?
【发布时间】:2015-06-30 18:39:27
【问题描述】:

我找到了一个教程here,关于使用$http 服务向PHP 发送数据。

我想我理解 Angular 中提交的语法,但我不明白他是如何使用 $data = file_get_contents("php://input"); 获取变量的。这似乎是给 file_get_contents 提供一个随机 URL?

无论如何,这就是我所得到的。这是为了在不刷新的情况下发送电子邮件。我现在对返回任何东西不感兴趣,我只想发送电子邮件。

HTML:

<script type="text/javascript" src="http://allenhundley.com/js/contact/contact.js"></script>
<div id="format">
    <div id="header">
    </div>
    <p id="text">
        Have any questions? Have a project? Shoot me an email here or at Allen@AllenHundley.com. 
    </p>
    <br />
    <input class="input" type="email" ng-model="email" placeholder="Email"/>
    <input class="input" type="text" ng-model="subject" placeholder="Subject"/>
    <textarea id="message" ng-model="message" placeholder="Message"></textarea>
    <button id="send" name="send" ng-click="emailCtrl.send()">Send Email</button>
</div>

AngularJS:

var emailController = spa.controller("emailController", ["$scope", "$http", function($scope, $http) {

    this.send = function() {

        $http.post('/php/send_email.php', {
            email : $scope.email,
            subject : $scope.subject,
            message : $scope.message
        });
    };
});

PHP:

<?php

    $user_email = $_POST["email"];
    $user_subject = $_POST["subject"];
    $user_message_input = $_POST["message"];

    $user_headers = 'MIME-Version: 1.0\r\n';
    $user_headers .= 'Content-type:text/html;charset=UTF-8\r\n';
    $user_headers .= 'From: <noReply@AllenHundley.com>\r\n';

    $user_message = "
        <html>
            <head>
                <title>
                    Thanks for contacting me!
                </title>
            </head>
            <body>
                " . $user_message_input . "
            </body>
        </html>
    ";

    mail($user_email, $user_subject, $user_message, $user_headers);

    $developer_to = "Allen@AllenHundley.com";
    $developer_subject = $user_subject;
    $developer_message = $user_message_input;

    $developer_headers = 'MIME-Version: 1.0\r\n';
    $developer_headers .= 'Content-type:text/html;charset=UTF-8\r\n';
    $developer_headers .= 'From: <' . $user_email . '>\r\n';

    $user_message = "
        <html>
            <head>
                <title>
                    Thanks for contacting me!
                </title>
            </head>
            <body>
                " . $developer_message . "
            </body>
        </html>
    ";

    mail($developer_to, $developer_subject, $developer_message, $developer_headers);

?>

我尝试阅读$http 上的文档,但我认为我已经掌握了这部分内容。它没有说任何关于 PHP 的内容。

我从这里去哪里?

【问题讨论】:

  • 您的问题得到解答了吗?

标签: php ajax angularjs email http


【解决方案1】:

使用默认的 Angular 设置,您需要从 php://input 读取输入,因为 Angular 不会以 application/x-www-form-urlencoded 格式发送数据。它以application\json 格式将其发送到服务器。

你有两个选择:

  1. 在 Angular 的 $http 函数中设置 headers 选项,如下所示:

$http({
  method: 'POST',
  url: '/php/send_email.php',
  headers: {'Content-Type': 'application/x-www-form-urlencoded'},
  data: {
    email : $scope.email,
    subject : $scope.subject,
    message : $scope.message
  }
});
  1. 使用php://input 而不是$_POST。您可以执行$post = json_decode(file_get_contents('php://input')); 之类的操作,然后您可以执行$user_email = $post['email']; 等操作。

更新:

再想一想,选项一不仅仅是更改标题。您还必须转换数据。 This post 有一个很好的例子来说明如何做到这一点。它还向您展示了另一个选项,即更改默认的内容类型标头并将其作为整个模块的默认行为。

这是该博客文章中提供的将数据转换为 url 编码字符串的函数:

var param = function(obj) {
  var query = '', name, value, fullSubName, subName, subValue, innerObj, i;

  for(name in obj) {
    value = obj[name];

    if(value instanceof Array) {
      for(i=0; i<value.length; ++i) {
        subValue = value[i];
        fullSubName = name + '[' + i + ']';
        innerObj = {};
        innerObj[fullSubName] = subValue;
        query += param(innerObj) + '&';
      }
    }
    else if(value instanceof Object) {
      for(subName in value) {
        subValue = value[subName];
        fullSubName = name + '[' + subName + ']';
        innerObj = {};
        innerObj[fullSubName] = subValue;
        query += param(innerObj) + '&';
      }
    }
    else if(value !== undefined && value !== null)
      query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
  }

  return query.length ? query.substr(0, query.length - 1) : query;
};

如果您要将它与我上面列出的第一个选项一起使用,您只需将 JS 对象传递给 params() 函数,然后再将其分配给 data 属性:

data: params({
  email : $scope.email,
  subject : $scope.subject,
  message : $scope.message
})

【讨论】:

  • 第三个选项设置默认标题,因此您仍然可以使用速记方法,如$http.post()
  • 我决定使用最简单的选项,但我的 php 文件中仍然没有任何数据。我留下了与我的帖子完全相同的角度和 HTML。在 PHP 文件中,我添加了$post = json_decode(file_get_contents('php://input')); 并使用$user_email = $post["email"]; 获取变量,但我没有发送任何电子邮件。有什么问题?
  • @Allenph:你在var_dump($post);时看到了什么?
  • @JaydeepGondaliya:那篇文章声称是关于 PHP 的,但其中没有 PHP。它是 ASP.NET。
猜你喜欢
  • 1970-01-01
  • 2020-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-13
  • 1970-01-01
  • 2011-01-17
相关资源
最近更新 更多