2017-03-06 2 views
1

Ich habe ein Kontaktformular auf meiner Website erstellt (usgn angularjs gebaut) und nun versucht, Daten mit phpmailer zu posten. Leider bin ich steckengeblieben. Beim Klicken auf "Senden" klicke ich auf "500 Interner Serverfehler" in der Konsole. Ich habe versucht, Cache, Cookies zu löschen und die Seite neu zu laden, aber es ist immer noch dasselbe. Ich verstehe nicht, was ich vermisse. Hier ist mein Code:Post Form Daten mit Angularjs und PhpMailer

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 = "[email protected]"; //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("[email protected]"); //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; 

        } 
       }); 

     }; 
}); 
+0

Was ist der Fehler in Ihrem Fehlerprotokoll? – RGriffiths

+0

Siehe [Warum sind angulare $ http Erfolg/Fehler-Methoden veraltet? Aus v1.6 entfernt?] (Http://stackoverflow.com/questions/35329384/why-are-angular-http-success-error-methods-deprecated-removed-from-v1-6/35331339#35331339) – georgeawg

Antwort

1

Ich würde wetten, dass Sie Ihren Code auf einem veralteten PHPMailer-Beispiel basieren, aber Sie verwenden eine neuere Version von PHPMailer selbst, und da Sie den Ratschlag in der Readme nicht befolgen, kann die SMTP-Klasse nicht gefunden werden , was zu einem Fehler 500 führt.

Sie müssen lesen, wie grundlegende Debugging in PHP zu tun, und suchen Sie nach den Fehlern in Ihrem Web-Server-Protokolle. Wenn Sie keinen Zugriff auf Protokolle haben (d. H. Sie sind auf günstigem Shared Hosting), können Sie vorübergehend ini_set('display_errors', true);.