使用Angularjs和PhpMailer发布表单数据

时间:2017-03-06 22:17:36

标签: php angularjs phpmailer

我已经在我的网站上建立了一个联系表格(建立了usng angularjs),现在尝试使用phpmailer发布数据。不幸的是,我被困住了。单击“发送”按钮时,我在控制台中面临500内部服务器错误。我尝试清除缓存,cookie并重新加载页面,但它仍然是相同的。我不明白我错过了什么。这是我的代码:

Contact.html

  <form class="form-inline" ng-submit="processForm()">
       <div id="name-group" class="form-group" ng-class="{ 'has-error' : errorName }">
         <input name="name" type="text" class="form-control" placeholder="Name" ng-model="formData.name">
         <span class="help-block" ng-show="errorName"></span>
       </div>
      <div class="form-group" id="superhero-group" ng-class="{ 'has-error' : errorSuperhero }">
         <input name="email" type="email" class="form-control" placeholder="Email">
      </div>
         <textarea name="message" class="form-control" rows="7" placeholder="Your message"></textarea>
         <button type="submit" name="submit" class="btn btn-default">Send</button>
   </form>

的index.php

 <?php
       require_once('pages/class.phpmailer.php');
       $errors = array();   // array to hold validation errors
       $data = array(); // array to pass back data
                       // validate the variables ======================================================
        if (empty($_POST['name']))
            $errors['name'] = 'Name is required.';
        if (empty($_POST['superheroAlias']))
            $errors['superheroAlias'] = 'E-mail is required.';
    // return a response ===========================================================
        // response if there are errors
        if ( ! empty($errors)) {
            // if there are items in our errors array, return those errors
            $data['success'] = false;
            $data['errors']  = $errors;

        } else {
            $mail = new PHPMailer(); // create a new object
            $mail->IsSMTP(); // enable SMTP
            $mail->SMTPAuth = true; // authentication enabled
            $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
            $mail->Host = "smtp.gmail.com";
            $mail->Port = 465; // or 587
            $mail->IsHTML(true);
            $mail->Username = "abc@gmail.com"; //Email that you setup
            $mail->Password = "12345"; // Password
            $mail->Subject = "Y-Web Contact mail from " . $_POST['name'] . ", e-mail: " .$_POST['superheroAlias']. "";
            $mail->Body = $_POST['content'];
            $mail->AddAddress("abc@gmail.com"); //Pass the e-mail that you setup
             if(!$mail->Send())
                {
                        echo "Mailer Error: " . $mail->ErrorInfo;
                }
                else
                {
                    $data['success'] = true;
                    $data['message'] = 'Thank you for sending e-mail.';
                }

        }
        echo json_encode($data);
  ?>

contactController.js

var GrapevineApp = angular.module('GrapevineApp');
GrapevineApp.controller('contactController', function($scope, $http) {

    // create a blank object to hold our form information
        // $scope will allow this to pass between controller and view
        $scope.formData = {};

        // process the form
        $scope.processForm = function () {
            $http({
                method: 'POST',
                url: 'pages/index.php',
                data: $.param($scope.formData),  // pass in data as strings
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' }  // set the headers so angular passing info as form data (not request payload)
            })
                .success(function (data) {
                    console.log(data);

                    if (!data.success) {
                        // if not successful, bind errors to error variables
                        $scope.errorName = data.errors.name;
                        $scope.errorSuperhero = data.errors.superheroAlias;
                    } 
                    else {
                        // if successful, bind success message to message
                        $scope.message = data.message;

                    }
                });

        };  
});

1 个答案:

答案 0 :(得分:1)

我敢打赌你的代码基于一个过时的PHPMailer示例,但你使用的是更新版本的PHPMailer本身,并且由于你没有按照自述文件中的建议,它没有找到SMTP类,导致错误500。

您需要阅读如何在PHP中进行基本调试,并查找Web服务器日志中的错误。如果您无法访问日志(即您使用便宜的共享主机),则可以暂时ini_set('display_errors', true);