Im Batman
Im Batman

Reputation: 1876

Email sending with AngularJS and PHP

I have created an application in AngularJS and it has an email contact form and it sends emails using PHP file in server.

Here's my Controller.js part in AngularJS:

$scope.feedbacksubmit= function (){  

  var name1 = document.getElementById("name").value;
  var email1 = document.getElementById("email").value;
  var message1 = document.getElementById("message").value;


    $http({
    url: "http://boost.meximas.com/mobile/email.php", 
    method: "POST",
    data: { name: name1, email: email1, message:message1 }

    }).success(function(data, status, headers, config) {
   // this callback will be called asynchronously
   // when the response is available

    if(status == 200) {

      var return_data = data;

        if(return_data != 0){


          $scope.hide();
          //$scope.closeFeedback();
        }else{

          $scope.showEmailError();
        }
      }
    }).
  error(function(data, status, headers, config) {
   // called asynchronously if an error occurs
   // or server returns response with an error status.
   console.log(status);
   $scope.showAlertNetwork();
   $scope.hide();

    });

  };

Here's my PHP Code:

<?php 

    $array = json_decode(file_get_contents('php://input'), true);
    $name = $array['name'];
    $email = $array['email'];
    $message = $array['message'];

    if (($name=="")||($email=="")||($message=="")) 
        { 
        printf("0"); 
        } 
    else{         
        $from="From: $name<$email>\r\nReturn-path: $email"; 
        $subject="Message sent using your contact form"; 
        mail("[email protected]", $subject, $message, $from); 

        } 

?> 

The problem arises when I fill in the contact form and hit the Send button. I'm getting $scope.showEmailError();. But I get the email without problem.

And if I try to hit the button without filling the form still getting same $scope.showEmailError(); message.

Upvotes: 3

Views: 5652

Answers (2)

Ferry francois
Ferry francois

Reputation: 1

    <?php

    $data = json_decode(file_get_contents("php://input"),true);

    $name = $data->name;
    $email = $data->email;
    $sujet = $data->sujet;
    $contenu = $data->contenu;

   if($name && $email && $sujet && $contenu){

            $destinataire = '[email protected]';
            // Pour les champs $expediteur / $copie / $destinataire, séparer par une virgule s'il y a plusieurs adresses
            $expediteur = $email;
            $copie = '[email protected]';
            $copie_cachee = '[email protected]';
            $objet = $sujet; // Objet du message
            $headers  = 'MIME-Version: 1.0' . "\n"; // Version MIME
            $headers .= 'Content-type: text/html; charset=ISO-8859-1'."\n"; // l'en-tete Content-type pour le format HTML
            $headers .= 'Reply-To: '.$expediteur."\n"; // Mail de reponse
            $headers .= 'From: '.$name.'<'.$name.'>'."\n"; // Expediteur
            $headers .= 'Delivered-to: '.$destinataire."\n"; // Destinataire
            $headers .= 'Cc: '.$copie."\n"; // Copie Cc
            $headers .= 'Bcc: '.$copie_cachee."\n\n"; // Copie cachée Bcc
            $message = '<div style="width: 100%; text-align: justify; font-weight: bold">hello</div>';

            mail($destinataire, $objet, $message, $headers);
   }else{

    }

?>

Upvotes: 0

maurycy
maurycy

Reputation: 8465

Why don't you use model values from input? It looks like you try to use AngularJS but you still think with other frameworks in mind

In the following example I'm sending model to php script, if NAME is not filled then PHP returns 404 error and it's handled in AngularJS $http via .error handler. You don't have to add so much extra logic to success to deal with it

http://plnkr.co/edit/sw9RRXb3kdEWXszJdwX3?p=preview

html

<input type="text" ng-model="formData.name" placeholder="name">
<input type="text" ng-model="formData.email" placeholder="email">
<input type="text" ng-model="formData.message" placeholder="message">

javascript

  $scope.formData = {
    'name': '',
    'email': '',
    'message': ''
  };
  $scope.postData = function () {
    $http.post('http://edeen.pl/form.php', $scope.formData)
    .success(
      function(data){
        $scope.response = data.replace(/\ /g, '&nbsp;&nbsp;').replace(/\n/g, '<br/>') //format response
      })
    .error(
      function(data){
        $scope.response = data
      })
  }

php

$input = json_decode(file_get_contents('php://input'));

if($input->name === ''){
  header("HTTP/1.0 404 Not Found");
  echo "Something went terribly wrong, missing name maybe?";
  return;
}

header('Content-Type: application/json');
var_dump($input);
    
    

Upvotes: 2

Related Questions