AngularJs将表单数据发送到SPA中的电子邮件

时间:2016-02-23 18:01:23

标签: javascript php angularjs

我正在尝试为SPA建立联系表单。我正在使用AngularJS将数据发送到电子邮件而不刷新页面。我是jcs-autoValidate来验证表单字段。我在前端使用Bootstrap表单,在服务器端使用PHP。

的Javascript

var app = angular.module('myApp', ['jcs-autoValidate']);

app.run(['defaultErrorMessageResolver',
        function(defaultErrorMessageResolver) {
            defaultErrorMessageResolver.getErrorMessages().then(function (errorMessages) {
                errorMessages['validPhone'] = 'Please enter valid phone number';
            });
        }]);

app.controller('FormController', function($scope, $http) {
   $scope.result = 'hidden'
   $scope.resultMessage;
   $scope.formData;
   $scope.submitButtonDisabled = false;
   $scope.submitted = false;
   $scope.submit = function(help) {
       $scope.submitted = true;
       $scope.submitButtonDisabled = true;
       if (help.$valid) {
           $http({
               method: 'POST',
               url: 'process.php',
               data: $.param($scope.formData),
               headers: {'Content-Type': 'application/x-www-form-urlencoded'}
           }).success(function(data){
               console.log(data);
               if (data.sucess) {
                   $scope.submitButtonDisabled = true;
                   $scope.resultMessage = data.message;
                   $scope.result = 'bg-sucess';
               } else {
                   $scope.submitButtonDisabled = false;
                   $scope.resultMessage = data.message;
                   $scope.result = 'bg-danger';
               }
           });
       } else {
           $scope.submitButtonDisabled = false;
           $scope.resultMessage = 'Failed, your message was not deliver. Please try sending again or reach us on the above numbers. Thank you!';
           $scope.result = 'bg-danger';
       }
   }
});

PHP

<?php
require_once 'phpmailer/PHPMailerAutoload.php';

    if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['phone']) && isset($_POST['message'])) {

    if (empty($_POST['name']) || empty($_POST['email']) || empty($_POST['phone']) || empty($_POST['message'])) {
        $data = array('success' => false, 'message' => 'Please the form completely.');
        echo json_encode($data);
        exit;
    }

    $mail = new PHPMailer();

    $mail->From = $_POST['email'];
    $mail->FromName = $_POST['name'];
    $mail->AddAddress('someemail@someone.com');
    $mail->Body = "Name: " . $_POST['name'] . "\r\n\r\nMessage: " . stripslashes($_POST['message']);

    if (isset($_POST['ref'])) {
        $mail->Body .= "\r\n\r\nRef: " . $_POST['ref'];
    }
    if(!$mail->send()) {
        $data = array('success' => false, 'message' => 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo);
        echo json_encode($data);
        exit;
    }

    $data = array('success' => true, 'message' => 'Thank you for contacting us! We will get back to you soon.');
    echo json_encode($data);

    } else {

    $data = array('success' => false, 'message' => 'Please the form completely.');
    echo json_encode($data);
    }

控制台

  

{&#34;成功&#34;:真实,&#34;消息&#34;:&#34;感谢您与我们联系!我们将得到   很快回复你。&#34;}

但是,我的电子邮件地址没有收到任何电子邮件,我在代码中更改了上面的电子邮件地址以用于演示目的。如果有人可以指导我或指出我做错了什么,因为我已经改变了我的代码10次而没有成功。

提前感谢您的帮助!

0 个答案:

没有答案