【问题标题】:Email sending with AngularJS and PHP使用 AngularJS 和 PHP 发送电子邮件
【发布时间】:2015-06-13 10:16:44
【问题描述】:

我在 AngularJS 中创建了一个应用程序,它有一个电子邮件联系表,它使用服务器中的 PHP 文件发送电子邮件。

这是我在 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();

    });

  };

这是我的 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); 

        } 

?> 

当我填写联系表格并点击发送按钮时,问题就出现了。我收到$scope.showEmailError();。但是我没有问题地收到电子邮件。

如果我尝试在不填写表单的情况下点击按钮,仍然会收到相同的 $scope.showEmailError(); 消息。

【问题讨论】:

    标签: javascript php angularjs email


    【解决方案1】:

    为什么不使用输入中的模型值?看起来您尝试使用 AngularJS,但您仍然在考虑其他框架

    在以下示例中,我将模型发送到 php 脚本,如果未填写 NAME,则 PHP 返回 404 错误,并在 AngularJS $http 中通过 .error 处理程序进行处理。你不必为成功添加太多额外的逻辑来处理它

    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);
        
        
    

    【讨论】:

      【解决方案2】:
          <?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{
      
          }
      
      ?>
      

      【讨论】:

      • 我已经这样做了
      • 请添加一些说明解释答案
      猜你喜欢
      • 1970-01-01
      • 2020-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-13
      • 1970-01-01
      • 2011-01-17
      相关资源
      最近更新 更多