簡體   English   中英

AngularJS:使用$ http命中mail.php時得到響應

[英]AngularJS : to get response when hitting mail.php using $http

這是我的控制器功能:

$scope.submitContactForm = function() {
    $log.info($scope.contact);
    if ($scope.contactform.$valid) {
        $http({
            method  : 'POST',
            url     : 'http://blog.local/php/mail.php',
            data    : $scope.contact, //forms user object
            headers : {'Content-Type': 'application/x-www-form-urlencoded'}
        })
        .success(function(data) {
            $log.info(data);
            if (data.errors) {
                // Showing errors.
            } else {
                $scope.messageForm = data.message;
            }
        });
    }
};

還有我的mail.php

<?php
session_start();
require_once('class.phpmailer.php');
require_once('class.smtp.php');
if($_POST) {
if( !isset($_SESSION['sended']) ) {
    // Re-check with php
    if( isset( $_POST['name'] ) && !empty( $_POST['name'] ) ):
        $name = filter_var(trim($_POST['name']), FILTER_SANITIZE_STRING);
    else:
      echo $error = 'Name is empty!';
       return;
    endif;

    if( isset( $_POST['contactlastname'] ) && !empty( $_POST['contactlastname'] ) ):
        $lastname = filter_var(trim($_POST['contactlastname']), FILTER_SANITIZE_STRING);
        // Add lastname to name
        $name = ($lastname) ?  $name. ' ' .$lastname : $name;
    endif;

    if( isset( $_POST['email'] ) && !empty( $_POST['email'] ) ):
        $email = filter_var(trim($_POST['email']), FILTER_SANITIZE_EMAIL);
            if( !filter_var( $email , FILTER_VALIDATE_EMAIL ) ):
               echo $error = 'Email is not valid!';
                return;
            endif;
    else:
        echo $error = 'Email is empty!';
      return;
    endif;

   if( isset( $_POST['subject'] ) && !empty( $_POST['subject'] ) ):
        $subject = filter_var(trim($_POST['subject']), FILTER_SANITIZE_STRING);
    else:
        $subject = "Hello";
    endif;

    if( isset( $_POST['message'] ) && !empty( $_POST['message'] ) ):
         $message = filter_var(trim($_POST['message']), FILTER_SANITIZE_STRING);
    else:
      echo  $error = 'Message is empty!';
        return;
    endif;

   if(!isset($error)) {
        // if we have no validation errors prepare mail
        $mail = new PHPMailer;
        $mail->isSMTP();
        $mail->SMTPDebug = 0;
        $mail->Debugoutput = 'html';

        //Set the hostname of the mail server gmail - yandex- outlook or your hosting's
        $mail->Host = "smtp.gmail.com"; // <------------ change with your host name
        // use
        // $mail->Host = gethostbyname('smtp.gmail.com');
        // if your network does not support SMTP over IPv6

        //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
        $mail->Port = 465; // <------------ Change with port 25 - 465 - 587 and etc..

        //Set the encryption system to use - ssl (deprecated) or tls
        $mail->SMTPSecure = 'ssl'; // <------------ tls (port 587) or ssl (port 465)

        //Whether to use SMTP authentication
        $mail->SMTPAuth = true;

        //Username to use for SMTP authentication - use full email address for gmail
        $mail->Username = "sharjilk62@gmail.com"; // <------------ Smtp authentication - username here

        //Password to use for SMTP authentication
        $mail->Password = ""; // <------------ Smtp authentication -password here

        $mail->setFrom($email, $name);
        $mail->AddReplyTo($email,$name);

        //Set who the message is to be sent to --- CHANGE THIS EMAIL ADDDRES WITH THE ONE YOU WANT TO RECEIVE EMAILS AND WWIT YOUR NAME
        $mail->addAddress('sharjilk62@gmail.com', 'Sharjil'); // <----------- CHANGE YOUR WITH YOUR EMAIL ADDRES

        $mail->Subject = $subject;
        $mail->msgHTML($message);

        // If send me copy checkbox is checked send a copy to user
        if( isset( $_POST['contactselfemail'] ) ):
            $mail->addCC($email);
        endif;

        // Send mail and report the result
        if($mail->send()):
            echo 'success';
            $_SESSION['sended'] = 'sended';            
        else:
            echo 'error';
            unset( $_SESSION['sended'] );
        endif;
    }
} else {
    echo 'already';
}
}
?>

當我使用$ http命中mail.php時,它會給我空字符串作為響應,而電子郵件不會發送到我的收件箱。 我不太了解php。 任何人都可以告訴我在mail.php中應該做些什么更改,以便發送電子郵件並獲得成功和錯誤的正確響應。

要訪問后期數據,請在php文件中使用:

$postdata = file_get_contents("php://input");
$data_object = json_decode($postdata);

在那之后使用

$ var_name = $ data_object-> var_name;

訪問后期數據

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM