Search code examples
phpemailphpmailer

New PHPMailer required PHPMailerAutoload.php?


I'm working on a project, And i need send email after enter email and password by user. They can activate them account click that send email. But i got error when i using PHPMailer. Error was that like

Fatal error: Uncaught Error: Class 'PHPMailer\PHPMailer' not found in C:\xampp\htdocs\E-Commerce-New-2\registration.php:20 Stack trace: #0 {main} thrown in C:\xampp\htdocs\E-Commerce-New-2\registration.php on line 20

In new PHPMailer version use PHPMailerAutoload.php file? I think answer is no.

I tried use that code $mail = new PHPMailer\PHPMailer(); and also this code $mail = new PHPMailer(); No one is worked yet. I put my file structure down below

File Strucure

And here i'm past my codes lines.

<?php 
        session_start();
        include("includes/db.php");
        include("functions/functions.php");
        include('PHPMailer/PHPMailer.php');
    ?>
    <?php
        // If the values are posted, insert them into the database.
        if (isset($_POST['email']) && isset($_POST['password'])){
            $email = $_POST['email'];
            $password = $_POST['password'];

            $verification_key = md5($email);
            $query = "INSERT INTO `customers` (customer_pass,customer_email,verification_key) VALUES ('$password', '$email','$verification_key')";

            $result = mysqli_query($con, $query);
            if($result){

            $mail = new PHPMailer\PHPMailer();
            $mail->setFrom('[email protected]');
            $mail->addAddress($email,'test');
            $mail->Subject = "Verify Your Account";
            $mail->isHTML(true);
            $mail->Body   = ' Please verify your email address to activate your account by clicking on this link <br>
                <a href="http://localhost/E-Commerce-New-2/verify.php?key="'. $verification_key.'>Click Here</a>';

                if(!$mail->send()) {
                    $fmsg= 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo;
                } else {
                    $smsg = 'User Registration successfull. Please Check your email to Activate Account!';
                }
        }else{
            $fmsg = "User registration failed";
        }
       } 
    ?>

Can anybody give me a suggestion?


Solution

  • In the new PHPMailer v6.0+, you will now need to import the PHPMailer classes into the global namespace at the very top of your PHP script.

    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    
    require 'path/to/PHPMailer/src/Exception.php';
    require 'path/to/PHPMailer/src/PHPMailer.php';
    require 'path/to/PHPMailer/src/SMTP.php';
    
    $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->Host = "mail.example.com";
    
    $mail->SetFrom("$from", "$from");
    $mail->AddAddress("$to");
    
    $mail->Subject = "$subject";
    $mail->Body = "$message";
    $mail->Send();