使用AngularJS和PHP发送电子邮件? [英] Sending an email with AngularJS and PHP?

查看:192
本文介绍了使用AngularJS和PHP发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此处关于使用$http服务将数据发送到PHP的信息.

I've found a tutorial here about using the $http service to send data to PHP.

我想我了解Angular中提交的语法,但是我不明白他是如何使用$data = file_get_contents("php://input");获取变量的.似乎是在向file_get_contents提供随机网址?

I think I understand the syntax for the submission in Angular, but I don't understand how he is getting the variables with $data = file_get_contents("php://input"); It seems like feeding file_get_contents a random URL?

无论如何,这是我所知道的.旨在发送电子邮件而无需刷新.我现在不希望退货,我只想发送电子邮件.

Regardless, here is as far as I got. This is intended to send an email without a refresh. I'm not interested in returning anything right now, I just want to send the email.

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:

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的信息.

I've tried to read the documentation on $http but I think I already have that part right. It doesn't say anything about PHP.

我从这里去哪里?

推荐答案

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

With the default Angular settings, you need to read the input from php://input because angular does not send the data in application/x-www-form-urlencoded format. It sends it to the server in application\json format.

您有两个选择:

  1. 在Angular的$http函数中设置headers选项,如下所示:
  1. Set the headers option in Angular's $http function, like so:

$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'];等等.
  1. Use php://input instead of $_POST. You would do something like $post = json_decode(file_get_contents('php://input'));, and then you can do $user_email = $post['email']; and so on.


更新:

再三考虑,选项一不仅仅改变标题.您还必须转换数据. 这篇文章有一个如何做到这一点的一个很好的例子.它还显示了另一个选项,它是更改默认的content-type标头,并将其设置为整个模块的默认行为.


Update:

On second thought, there's a little more to option one than just changing the header. You also have to transform the data. This post has a good example of how to do that. It also shows you another option, which is to change the default content-type header and have this be the default behavior for your whole module.

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

Here is the function provided in that blog post to transform the data to a url-encoded string:

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属性:

If you were to use this with the first option I listed above, you would simply pass the JS object to the params() function before assigning it to the data property:

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

这篇关于使用AngularJS和PHP发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆