代码似乎没有发送电子邮件

时间:2016-12-05 09:10:51

标签: php phpmailer

我似乎陷入困境,似乎无法弄清问题所在。我对php很新,所以请耐心等待。

以下是signup.php页面的代码,这是新用户输入详细信息的位置,注册后,必须将电子邮件发送到他们提供的电子邮件地址。

代码会将用户详细信息成功添加到数据库中,但它不会发送电子邮件,也不会显示任何错误。

我做错了什么?

          <?php

         //Calls the databse file
         require_once 'Dbconfig.php';

        //Checks whether the button has been clicked
       if(isset($_POST['btn-signup']))
       {

        //Sets the values from the text fields in the form
        $uname      = trim($_POST['txt_uname']);
        $usurname   = trim($_POST['txt_usurname']);
        $ugender    = trim($_POST['txt_ugender']);
        $upass      = trim($_POST['txt_upass']);
        $umail      = trim($_POST['txt_umail']);
        $udate      = trim($_POST['txt_udate']);
        $unum       = trim($_POST['txt_unum']);
        $uimage     = trim($_POST['txt_uimage']);

        //Validation
        if($uname=="")
        {
         $error[] = "provide username!";
        }
        //Validation
       else if($usurname=="")
       {
         $error[] = "provide surname!";
       }
      //Validtation
      else if($ugender=="")
      {
        $error[] = "choose gender!";
      }
      //Validation
      else if($upass=="")
      { 
        $error[] = "enter a password";
      }
      //Validation, 
      else if(strlen($upass) < 6 )
      {
        $error[] = "Password must be atleast 6 characters long";
      }
      //Validation
      else if($umail=="")
      {
        $error[] = "Please provide email id!";
     }
     //Validationtation, checks whether the email is of the correct format
     else if(!filter_var($umail, FILTER_VALIDATE_EMAIL))
     {
        $error[] = "Please enter a valid email address!";
     }
     //Validation
     else if($udate=="")
     {
        $error[] = "Please provide your date of birth";
     }
     //Validation
     else if($unum=="")
     {
        $error[] = "Please provide your contact number";
     }
     //Validation
     else if($uimage=="")
     {
        $error[] = "Please upload an image";
     }      
     else
     {
        try
        {
            //Sets the value for the
            $umail = $_POST['txt_umail'];
            $sql = "SELECT * FROM users WHERE emailAddress = :umail";
            $stmt = $dbh->prepare($sql);

            $stmt->bindValue(':umail', $umail);
            $stmt->execute();
            $row = $stmt->fetch(PDO::FETCH_ASSOC);

            if($row > 0)
            {
                die('That email already exists!, please enter another');
            }
            else
            {   
                $stmt = $dbh->prepare("INSERT INTO users(userName,       
                  userSurname, gender, userPass, 
                   emailAddress, birthDate, contactNum, image)
                    VALUES(:uname, :usurname, :ugender, :upass ,
                     :umail, :udate, :unum, :uimage )");

                $stmt->bindparam(":uname", $uname);
                $stmt->bindparam(":usurname", $usurname);
                $stmt->bindparam(":ugender", $ugender);
                $stmt->bindparam(":upass", $upass);
                $stmt->bindparam(":umail", $umail);
                $stmt->bindparam(":udate", $udate);
                $stmt->bindparam(":unum", $unum);
                $stmt->bindparam(":uimage", $uimage);

                $result = $stmt->execute();

                ?>
                <script>
                alert('You have successfully registered, please ilog in');
                window.location.href = 'index.php';
                </script>

              <?php


                if($result)
                {
                    require_once 'PHPMailerAutoload.php';
                    $mail               = new PHPMailer();
                    $mail->IsSMTP();
                    $mail->SMTPDebug = 2;
                    $mail->Host         = "localhost";
                    $mail->Port         = 25;
                    $mail->SMTPAuth     = true;
                    $mail->SMTPSecure   = ssl;
                    $mail->Username     = "smtp_username";
                    $mail->Password     = "smtp_password";
                    $mail->SetFrom      = "newsblock@gmail.com";
                    $mail->FromName     = "NewsBlock";
                    $mail->AddAddress($umail);
                    $mail->AddReplyTo("newsblock@gmail.com");
                    $mail->IsHTML   = true;
                    $mail->Subject  = "Successfully registered";
                    $mail->Body = "Thank you for choosing to become 
                                  part of the NewsBlock family
                                   You can now access our website 
                                    by logging in";

                    if(!$mail->Send())
                    {
                        echo "There was an error sending the email" 
                        . '' . $mail->ErrorInfo;
                        return false;
                    }
                    else
                    {
                        echo 'Thank you for registering with News Block. 
                        Please login';
                        header("Location: index.php"); 
                      }
                    } 
                 }
               }
              catch(PDOException $e)
              {
                echo $e->getMessage();
             }
           }
         }  
         include_once 'Class.php';

     ?>

0 个答案:

没有答案