Kofi Amoussou
Kofi Amoussou

Reputation: 92

Phpmailer causing internal error 500 with ajax

i'm including my phpmailer in my registration page and using simple ajax to push data interact with db... everything works fine in my localhost but when i upload to server, i get this internal error 500 html

<script>
$(document).ready(function(){
    $("#register-btn").click(function(){

        var name=$("#hostel-name").val();
        var email=$("#email").val();
        var conf_email=$("#conf-email").val();
        var password=$("#password").val();


        $.ajax({
            url:"functions/register.php",
            data:{
                name:name,
                email:email,
                conf_email:conf_email,
                password:password
                },
            type:"POST",
            success:function(data){
                $("#result").html(data);
            }
        });
    });
   });

</script>
<section id="result">

</section>

<section class="row">
    <section class="container">
        <section class="col-md-offset-4 col-md-4" id="register">
        <h1 class="title text-center" style="border-left:0;margin-top: -10px;"><a href="index">Accomonde</a></h1>
           <h3 class="title">Sign Up</h3>
           <br>

                <section class="form-group col-md-12">
                <label> Name</label>
                <input type="text" class="form-control" name="hostel-name" id="hostel-name" autocomplete="off" placeholder="Hostel Name" required>
                </section>

                <section class="form-group col-md-12">
                <label>Email</label>
                <input type="email" class="form-control" name="email" id="email" autocomplete="off" placeholder="Your Email" required>
                </section>

                <section class="form-group col-md-12">
                <label>Confirm Email</label>
                <input type="email" class="form-control" name="conf-email" id="conf-email" autocomplete="off" placeholder="Confirm Your Email" required>
                </section>

                <section class="form-group col-md-12">
                <label>Password</label>
                <input type="password" class="form-control" name="password" id="password" autocomplete="off" placeholder="Your Password" required>
                <br>
                <section class="form-group">
                    <button class="form-control btn-primary" name="register" id="register-btn" ><span class="fa fa-paper-plane"> Sign Up</span></button>
                </section>
                <section class="form-group">
                    <ul class="list-inline">
                        <li class="pull-left">Already Have an Account?</li>
                        <li class="pull-right"><a href="login">Log in!</a>   </li>
                    </ul>
                </section>

        </section>
    </section>
 </section>

```

and this is my php script

<?php
    require_once '../libs/phpmailer/PHPMailerAUtoload.php';
    try{
        include '../database/connection.php';
        $hostel_name=$_POST['name'];
        $email=filter_var($_POST['email'],FILTER_VALIDATE_EMAIL);
        $password=hash('sha256',$_POST["password"]);
        $confemail=filter_var($_POST['conf_email'],FILTER_VALIDATE_EMAIL);
        $confirm_code=md5(uniqid(rand()));
        $user_id=md5(rand(0,10000));
         //new block added
            if($name="" or $email=="" || $password=="" || $confemail==""  ){echo '<p  class="alert alert-danger text-center">Please Make Sure All Fields Are Filled <span class="close pull-right"><span class="close pull-right"> <a href="#" >&times;</a></span></p>';}

          elseif($email==$confemail){
       $query=$dbc->query("select * from users where email='$email'");


     if($result=$query->fetch(PDO::FETCH_NUM)==1){ echo '
    <p class="alert alert-danger text-center">We already have someone with that email <span class="close pull-right"><span class="close pull-right"> <a href="#" >&times;</a></span></p>'; }elseif($result=$query->fetch(PDO::FETCH_NUM)==0){ 
      $data_result=$dbc->query("insert into users(hostel_name,email,password,confirm_code,user_id) values('$hostel_name','$email','$password','$confirm_code','$user_id')");
     if($data_result){
          $m = new PHPMailer;
        $m ->isSMTP();
        $m->SMTPAuth=true;

        // debugging
        // $m->SMTODebug=1
        // endof debug
        $m->Host="smtp.gmail.com";
        $m->Username="[email protected]";
        $m->Password="lollypop28:)";
        $m->SMTPSecure='ssl';
        $m->Port=465;
        $m->isHtml(true);

        $m->Subject = 'Welcome to Efie';
        ob_start();
include '../views/email/register-email.php';
$body = ob_get_contents();

   $m->msgHTML($body, dirname(__FILE__));
        $m->FromName="Efie Ghana";
        $m->AddAddress($email,$hostel_name);
        ob_end_clean();
        $m->send();

       echo '<p class="alert alert-success text-center error-message"> You\'re almost there!  Check your inbox to activate your account.</p>';

        }

   }
 }
}catch(Exception $e){echo '<p class="alert alert-danger text-center error-message">Something is not right <span class="pull-right close"> <a href="#" >&times;</a></span></p>';}

$dbc=null;





 ?>

```

Upvotes: 0

Views: 353

Answers (1)

Synchro
Synchro

Reputation: 37770

Linux file systems are usually case-sensitive, so this will not work:

require_once '../libs/phpmailer/PHPMailerAUtoload.php';

Should be:

require_once '../libs/phpmailer/PHPMailerAutoload.php';

In general, any time you get an error 500, you can find more detail on the error in your web server's log files.

Also your script is vulnerable to SQL injection - while you're checking that the email address is valid, it's possible to have a string that's a valid email address that's also a valid SQL injection attack. Always escape what you're putting into your SQL.

Upvotes: 1

Related Questions