0

Let me start by saying I have looked into the documentation and unfortunately did not manage to fix my problem. Mainly what I am trying to do is, after the user registers, an email is sent to him and a vkey is sent to my database. After the user clicks on the verification link there the "verified" on my database is supposed to change to 1.

However I am not being able to do so. I am using cpanels emails and I have sent an email through the webmail, so I know they are working. But my script for some reason isn't, I tried following the documentation though...

Here is the function.php:

function createUser($conn, $name, $email, $username, $pwd, $vkey){
  $sql = "INSERT INTO users (usersName, usersEmail, usersUid, usersPwd, vkey) VALUES (?, ?, ?, ?, ?);";
  $stmt = mysqli_stmt_init($conn);

  if (!mysqli_stmt_prepare($stmt, $sql) ) {
    header("location: index.html?error=stmtfailed");
    exit();
  }

    $hashedPwd = password_hash($pwd, PASSWORD_DEFAULT);

    mysqli_stmt_bind_param($stmt, "sssss", $name, $email, $username, $hashedPwd, $vkey);
    mysqli_stmt_execute($stmt);
    mysqli_stmt_close($stmt);

    header("location: ../mainpage/index.html");
    exit();


    $to = $email;
    $subject = "Verificação da conta";
    $message = "
    Thanks for signing up!
    Your account has been created, you can login with the following credentials after you have activated your account by pressing the url below.
      
    ------------------------
    Username: '.$name.'
    Password: '.$pwd.'
    ------------------------
      
    Please click this link to activate your account:
    https://universitymmt.com/includes/verify.php?vkey=$vkey'.$email.'&hash='.$pwd.'";




    $headers = "De: suporte@universitymmt.com \r\n";
    $headers .= "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" ."\r\n";
  
    mail($to, $subject, $message, $headers); 
  
    header("location: index.html?accountverified");





    

}

This page is has the php that calls for the functions.php:

<?php

if(isset($_POST["submit"])){

  $name = $_POST['name'];
  $email = $_POST['email'];
  $username = $_POST['uid'];
  $pwd = $_POST['pwd'];
  $pwdRepeat = $_POST['pwdrepeat'];

  $vkey = md5(time() .$uid);

  require_once 'db_connection.php';
  require_once 'functions.php';

  if (emptyInputSignup($name, $email, $username, $pwd, $pwdRepeat) !== false) {
    header("location: ../index.html?error=emptyInput");
    exit();
  }
  if (invalidUid($username) !== false) {
    header("location: ../index.html?error=invalidUid");
    exit();
  }
  if (invalidEmail($email) !== false) {
    header("location: ../index.html?error=invalidEmail");
    exit();
  }

  if (pwdMatch($pwd, $pwdRepeat) !== false) {
    header("location: ../index.html?error=passwordsdontmatch");
    exit();
  }

  if (uidExists($conn, $username, $email) !== false) {
    header("location: ../index.html?error=usernameTaken");
    exit();
  }

  createUser($conn, $name, $email, $username, $pwd, $vkey);


  $to = $email;
    $subject = "Verificação da conta";
    $message = "<a href='https://universitymmt.com/includes/verfiy.php?vkey=$vkey'> Confirmar a minha conta</a>";
    $headers = "De: suporte@universitymmt.com \r\n";
    $headers .= "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" ."\r\n";
  
    mail($to, $subject, $message, $headers); 
 

}

I may add, I added the mail function on both files, hoping one of them would work, which it didn't lol.

Thank you for any help guys.

  • 1
    Does this answer your question? [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail) – Dharman Nov 05 '20 at 11:09
  • Do you test this local on your machine or on a webserver of a hosting company? On local machine you need to configure your webserver to allow it to send emails – Baracuda078 Nov 05 '20 at 11:10
  • @Baracuda078 I am using hostgators mailserver, through cpanel –  Nov 05 '20 at 11:16
  • Before your mail part I see an `exit();` function so your mail function will never run – Baracuda078 Nov 05 '20 at 11:18
  • @Baracuda078 OHHH I see, let me try that. –  Nov 05 '20 at 12:12

0 Answers0