0

I have a piece of PHPMailer code to send verification link to user's registered email upon completing registration. This code works exceptionally well on my local XAMPP server while I was developing my site. When I got done and deployed on my web server (hosted by namecheap), no email will be send when a user registers, while my database picks up and stores the user's details. I have searched for solution everywhere I can but couldn't find. Here is my piece of code (it still works fine on my XAMPP).

<?php


use PHPMailer\PHPMailer\PHPMailer;


use PHPMailer\PHPMailer\Exception;

require '../PHPMailer/src/PHPMailer.php';

require '../PHPMailer/src/SMTP.php';
        $mail = new PHPMailer;

        //Enable SMTP debugging. 
        $mail->SMTPDebug = 2;                               
        //Set PHPMailer to use SMTP.
        $mail->isSMTP();
        $mail->SMTPOptions = array(
            'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            )
        );         

        //Set SMTP host name                          
        $mail->Host = "smtp.gmail.com";
        //Set this to true if SMTP host requires authentication to send email
        $mail->SMTPAuth = true;                          
        //Provide username and password     
        $mail->Username = "myusername@gmail.com";                 
        $mail->Password = "myemailpassword";                           
        //If SMTP requires TLS encryption then set it
        $mail->SMTPSecure = "tls";                           
        //Set TCP port to connect to 
        $mail->Port = 587;                                   

        $mail->From = "myemail";
        $mail->FromName = "myname";

        $mail->addAddress($email);

        $mail->isHTML(true);
        $mail->CharSet = 'UTF-8';

        $mail->Subject = "ACTIVATE YOUR ACCOUNT";
        $search  = array('[sponsor]', '[email]', '[uid]', '[code]', '[username]', '[phone]', '[password]', '[bankname]', '[accname]', '[accno]');
        $replace = array($sponsor, $email, $uid, $code, $username, $phone, $_POST['password'], $bankname, $accname, $accno);
        $mail->Body = str_replace($search, $replace, file_get_contents('../Emails/registeremail.html'));

        if(!$mail->send()) 
        {
            //add support link later
            $_SESSION['sign_msg']= "Something Went Wrong, Please contact Support";
            header('location:signup.php');
        } 
        else 
        {

        $_SESSION['sign_msg'] = "Verification link has been sent to your email. &nbsp; &nbsp; &#9745; <br>
        Click on the link to verify your account";
        header('location:signup.php');
            }
?>
Benito
  • 181
  • 1
  • 9
  • 1
    Does `$mail->send()` fail? Maybe your host doesn't allow sending mail like this? Did you check? Or perhaps you can ask them to check (or check yourself if you have access) in the logs of the mailserver to see if it processed and then sent the mail. Possibly the mail is actually _sent_, but just not _received_ - many things can go wrong in between sending and receiving, but the most common is usually things to do with spam filtering etc. Read https://stackoverflow.com/a/24644450/5947043 - it may be informative. – ADyson Mar 04 '19 at 22:23
  • @ADyson, the if (!$mail->send()) should at least return a failure or success message in the $_SESSION['sign_msg'], but it does none of this. The email is not delivered/received. – Benito Mar 04 '19 at 22:44
  • 1
    Show your debug output, check your mail server logs. Disabling certificate verification is likely to be hiding multiple delivery problems; don’t do that. – Synchro Mar 05 '19 at 05:40
  • If you aren't even seeing the messages then potentially something is crashing before that. If it doesn't display the messages, what does it do instead? Does it redirect you to signup.php, or not? Have you got PHP error reporting switched on to show exceptions? Normally in a hosting environment they should at least be logged to an error_log file somewhere on the account, rather than being shown on screen. You need to investigate a bit more. – ADyson Mar 05 '19 at 06:41

0 Answers0