I am trying to send an email via angular. Am I approaching this in the correct manner?

时间:2016-04-04 18:31:03

标签: javascript php angularjs

I'm just trying to learn something new. I want to be able to send an email using angular and php. I am turning my form data into an object. Then I want to send this object as a variable and send the data to a php file. I am able to create the variable but I don't believe the data is being set in the php file. What am I missing here?

angular.module('app', []);
var vm = this;

var url = '/contact-form-handler.php';
vm.codeStatus = "";
vm.form = {};

vm.submit = function($event) {

  $event.preventDefault();

  console.log(this.form);
  $http({
    method: "POST",
    url: url,
    data: vm.form,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  }).
  success(function(result) {
    console.log(result);
    console.log('hit');
  }).
  error(function(result) {
    console.log('error');
  });
  return false;

};
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<?php

header('Access-Control-Allow-Origin: *');


if (isset($_POST["data"])) {

	$from = 'Portfolio';
	$to = 'rafael.heard@gmail.com';
    $subject = 'Portfolio Inquiry';
	$body = $_POST['data'];


	mail($to, $subject, $body, $from);

    $result = 'mail sent';
	
}else{
	$result =  'not set';
}
?>

<div ng-app="app" ng-controller="MainController as main">
  <form role="form" name="contactForm" ng-submit="main.submit($event)" required novalidate>
    <div class="form-groups">
      <div class="group">
        <input type="text" name="username" ng-model="main.form.name" placeholder="Name (required)" required />
      </div>

      <div class="group">
        <input type="email" name="userEmail" ng-model="main.form.email" placeholder="Email (required)" required />
      </div>

      <div class="group">
        <textarea type="text" name="message" ng-model="main.form.message" ng-minlength="10" rows="10" required></textarea>
      </div>

      <button>Send</button>
    </div>
  </form>

1 个答案:

答案 0 :(得分:0)

我想出了这个。我在我的php文件和角度http中更改了我的方法。我序列化了这个对象。

&#13;
&#13;
<?php

header('Access-Control-Allow-Origin: *');

if (isset($_POST["subject"])) {


    $name = $_POST['name'];
    $subject = $_POST['subject'];
    $from = $_POST['email'];
    $message = $_POST['message'];
	$to = 'rafael.heard@gmail.com';


	mail($to, $subject, $message, $from);
    

}else{
	echo 'not set';
}
?>
&#13;
grep -no man file | uniq -c | cut -d":" -f1 | xargs printf "%d occurances in line %s\n"
&#13;
&#13;
&#13;