如何使用angularjs和php发送电子邮件验证?

时间:2017-03-12 17:10:22

标签: php mysql angularjs

我创建了注册表单和相应的控制器和后端php代码。 已注册的数据正确存储。但我没有在我的电子邮件ID中收到邮件。请帮帮我...

我的HTML代码



<div class="col-lg-6 col-lg-offset-3 well " style="margin-top:1em; background-color:black; ">

  <h4 style="color:white; text-align:center;"> <strong> FILL UP REGISTRAION FORM  </strong> </h4>

</div>


<div class="col-lg-6 col-lg-offset-3 well" style="margin-bottom:10em;">

  <form name="register" ng-app="TempleWebApp" ng-controller="RegisterCtrl" ng-submit="SignUp(register.$valid)" novalidate>

    <!-- First Name -->
    <div class="form-group col-lg-6" ng-class="{ 'has-error' : register.fname.$invalid && (register.fname.$dirty || submitted)}">
      <label>First Name</label>
      <input class="form-control" type="text" name="fname" ng-model="fname" placeholder="First Name" ng-required="true">
      <span class="help-block" ng-show="register.fname.$invalid && register.fname.$error.required && (register.fname.$dirty || submitted)">
				          First Name is required.</span>
    </div>

    <!-- Last Name -->
    <div class="form-group col-lg-6" ng-class="{ 'has-error' : register.lname.$invalid && (register.lname.$dirty || submitted)}">
      <label>Last Name</label>
      <input class="form-control" type="text" name="lname" ng-model="lname" placeholder="Last Name" ng-required="true">
      <span class="help-block" ng-show="register.lname.$invalid && register.lname.$error.required && (register.lname.$dirty || submitted)">
				         Last Name is required.</span>
    </div>

    <!-- City -->
    <div class="form-group col-lg-6" ng-class="{ 'has-error' : register.city.$invalid && (register.city.$dirty || submitted)}">
      <label>City</label>
      <input class="form-control" type="text" name="city" ng-model="city" placeholder="City" ng-required="true">
      <span class="help-block" ng-show="register.city.$invalid && register.city.$error.required && (register.city.$dirty || submitted)">
				         City is required.</span>
    </div>

    <!-- Gender -->
    <div class="form-group col-lg-6" ng-class="{ 'has-error' : register.gender.$invalid && (register.gender.$dirty || submitted)}">
      <label>Gender</label> <br>
      <input type="radio" name="gender" ng-model="gender" value="male" ng-required="true"> Male
      <input type="radio" name="gender" ng-model="gender" value="female" ng-required="true" style="margin-left:5em;"> Female
      <span class="help-block" ng-show="register.gender.$invalid && register.gender.$error.required && (register.gender.$dirty || submitted)">
				         Gender is required.</span>
    </div>


    <!-- Email -->
    <div class="form-group col-lg-12" ng-class="{ 'has-error' : register.email.$invalid && (register.email.$dirty || submitted)}">
      <label>Email</label>
      <input class="form-control" type="text" name="email" ng-model="useremail" placeholder="Email" ng-pattern="/^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/" ng-required="true">
      <span class="help-block" ng-show="register.email.$invalid && register.email.$error.required && (register.email.$dirty || submitted)">
				          Email is required.</span>
      <span class="help-block" ng-show="register.email.$error.pattern">
				        Enter Valid  Email .</span>
    </div>


    <!-- Password -->
    <div class="form-group col-lg-6" ng-class="{ 'has-error' : register.password.$invalid && (register.password.$dirty || submitted)}">
      <label>Password</label>
      <input class="form-control" type="password" name="password" ng-model="userpassword" placeholder="Password" ng-required="true">
      <span class="help-block" ng-show="register.password.$invalid && register.password.$error.required && (register.password.$dirty || submitted)">
				          Password is required.</span>
    </div>

    <!-- CONFIRM PASSWORD -->
    <div class="form-group col-lg-6" ng-class="{ 'has-error' : register.confirmPassword.$invalid && (register.confirmPassword.$dirty || submitted)}">
      <label>Confirm Password</label>
      <input type="Password" name="confirmPassword" class="form-control" ng-model="confirmPassword" placeholder="Confirm Your Password" ng-compare="password" ng-required="true">
      <p ng-show="register.confirmPassword.$error.required && (register.confirmPassword.$dirty || submitted)" class="help-block">confirm password is required.</p>
      <p ng-show="register.confirmPassword.$error.compare  && (register.confirmPassword.$dirty || submitted)" class="help-block">Confirm password doesnot match.</p>
    </div>

    <div class="col-lg-12 well " ng-repeat="error in errors" style="background-color:red; margin-top:0.5em;"> {{ error}} </div>
    <div class="col-lg-12 well" ng-repeat="msg in msgs" style="margin-top:0.5em;">
      <h5 style="color:green;">{{ msg}} </h5>
    </div>


    <button type="submit" class="btn btn-success col-lg-12">
				  <span ng-show="searchButtonText == 'REGISTERING'"><i class="glyphicon glyphicon-refresh spinning"></i></span>
                  {{ searchButtonText }}
				</button>


  </form>
</div>
&#13;
&#13;
&#13;

我的控制器

app.controller(&#39; RegisterCtrl&#39;,功能($ scope,$ location,$ http,$ timeout){

          $scope.gender = '';


            $scope.errors = [];
            $scope.msgs = [];
            $scope.searchButtonText = "REGISTER DETAILS";
            $scope.test = "false";


           $scope.SignUp = function(isValid) {

               // Set the 'submitted' flag to true
              $scope.submitted = true; 
               $scope.errors.splice(0, $scope.errors.length); // remove all error messages
                $scope.msgs.splice(0, $scope.msgs.length);



              if (isValid) {  

               $http.post('php/register.php', 
                            { 'fname': $scope.fname, 
                              'lname': $scope.lname, 
                              'city': $scope.city, 
                              'gender': $scope.gender, 
                              'pswd' : $scope.userpassword, 
                              'email': $scope.useremail
                            })
                    .success(function(data, status, headers, config) {
                        if (data.msg != '')
                        {
                        $scope.msgs.push(data.msg);
                        $scope.test = "true";
                        $scope.searchButtonText = "REGISTERING";
                        //var goTopayment = function() { $scope.searchButtonText = "REGISTER DETAILS"; $location.path('/login'); };
                        // $timeout(goTopayment, 3000);
                        }
                      else
                      {
                        $scope.errors.push(data.error);
                       }
                     })
                  .error(function(data, status) { // called asynchronously if an error occurs or server returns response with an error status.

                    $scope.errors.push(status);
                    });
            } // closing bracket for IF(isvalid)

        } // closing bracket for $scope.SIGNUP = function   

}); //注册结束括号

我的PHP代码是

&#13;
&#13;
<?php

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

$fname = mysql_real_escape_string($data->fname);
$lname = mysql_real_escape_string($data->lname);
$city = mysql_real_escape_string($data->city);
$gender = mysql_real_escape_string($data->gender);
$upswd = mysql_real_escape_string($data->pswd);
$uemail = mysql_real_escape_string($data->email);


$con = mysql_connect('localhost', 'root', '');
mysql_select_db('registraion', $con);

$qry_em = 'select count(*) as cnt from users where Email ="' . $uemail . '"';
$qry_res = mysql_query($qry_em);
$res = mysql_fetch_assoc($qry_res);



if($res['cnt']==0){
$qry = 'INSERT INTO users (Firstname,Lastname,City,Gender,Password,Email) values 
               ("' . $fname . '","' . $lname . '","' . $city . '","' . $gender . '","' . $upswd . '","' . $uemail . '")';
			   
$qry_res1 = mysql_query($qry);

     if (!$qry_res1) {
			die('Invalid query: ' . mysql_error());
		} else {
			return mysql_insert_id();
		}
	
	
	$current_id = mysql_insert_id(); //last insert id			   

 
   if(!empty($current_id)) {
				$actual_link = "http://localhost/angular/php/"."activate.php?uid=" . $current_id;
				
				$EmailTo =  $uemail ;
				$Subject = "User Registration Activation Email";
				$Content = "Click this link to activate your account. <a href='" . $actual_link . "'>" . $actual_link . "</a>";
				$MailHeaders = "From: Admin\r\n";
				$success = mail($EmailTo, $Subject, $Content, $MailHeaders);
				
				
				if($success ) {
					
			        $arr = array('msg' => "You have registered and the activation mail is sent to your email. Click the activation link to activate you account.", 'error' => '');
                    $jsn = json_encode($arr);
                    print_r($jsn);
					
				  }
				} 
 
}


else
{
     $arr = array('msg' => "", 'error' => 'User Already exists with same email');
    $jsn = json_encode($arr);
    print_r($jsn);
}
?>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

最后我解决了。问题是找到$ current_id变量的最后插入的id值。由于我没有得到此变量的正确值,因此$ Emailto变量的值未分配电子邮件ID。所以我将PHP代码更改为以下方式。

<?php

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

$fname = mysql_real_escape_string($data->fname);
$lname = mysql_real_escape_string($data->lname);
$city = mysql_real_escape_string($data->city);
$gender = mysql_real_escape_string($data->gender);
$upswd = mysql_real_escape_string($data->pswd);
$uemail = mysql_real_escape_string($data->email);


$con = mysql_connect('localhost', 'root', '');
mysql_select_db('registraion', $con);

$qry_em = 'select count(*) as cnt from users where Email ="' . $uemail . '"';
$qry_res = mysql_query($qry_em);
$res = mysql_fetch_assoc($qry_res);



if($res['cnt']==0){
$qry = 'INSERT INTO users (Firstname,Lastname,City,Gender,Password,Email) values 
               ("' . $fname . '","' . $lname . '","' . $city . '","' . $gender . '","' . $upswd . '","' . $uemail . '")';
			   
$qry_res1 = mysql_query($qry);

	
	//changed current_id value finding method.
	$current_id = mysql_query("select uid from users ORDER BY uid DESC LIMIT 1"); //last insert id			   

 
   if(!empty($current_id)) {
				
				$actual_link = "http://localhost/angular/php/"."activate.php?uid=" . $current_id;
				
				$EmailTo = $uemail;
				$Subject = "User Registration Activation Email";
				$Content = "Click this link to activate your account. <a href='" . $actual_link . "'>" . $actual_link . "</a>";
				$MailHeaders = "From: Admin\r\n";
				
				if(mail($EmailTo, $Subject, $Content, $MailHeaders) ) {
					
			        $arr = array('msg' => "You have registered and the activation mail is sent to your email. Click the activation link to activate you account.", 'error' => '');
                    $jsn = json_encode($arr);
                    print_r($jsn);
					
				  }
				} 
 
}


else
{
     $arr = array('msg' => "", 'error' => 'User Already exists with same email');
    $jsn = json_encode($arr);
    print_r($jsn);
}
?>