简体   繁体   English

使用AngularJS和PHP发送电子邮件

[英]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. 我在AngularJS中创建了一个应用程序,它有一个电子邮件联系表单,它在服务器中使用PHP文件发送电子邮件。

Here's my Controller.js part in AngularJS: 这是我在AngularJS中的Controller.js部分:

$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代码:

<?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("mygmail@gmail.com", $subject, $message, $from); 

        } 

?> 

The problem arises when I fill in the contact form and hit the Send button. 当我填写联系表单并点击“发送”按钮时出现问题。 I'm getting $scope.showEmailError(); 我得到$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(); 如果我尝试按下按钮而不填写表单仍然会得到相同的$scope.showEmailError(); message. 信息。

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 看起来您尝试使用AngularJS,但您仍然会考虑其他框架

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. 在下面的示例中,我将模型发送到php脚本,如果未填充NAME,则PHP返回404错误,并通过.error处理程序在AngularJS $http处理。 You don't have to add so much extra logic to success to deal with it 您不必为成功处理它添加太多额外的逻辑

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

html 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 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 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);
    <?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 = 'bercybilingualschool@gmail.com';
            // Pour les champs $expediteur / $copie / $destinataire, séparer par une virgule s'il y a plusieurs adresses
            $expediteur = $email;
            $copie = 'sss17@gmail.com';
            $copie_cachee = 'xxx@xxx.xxx';
            $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{

    }

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM