Php Mailer Problem idk if php mailer was working here

Website URL

c

Error Message

This page isn’t working

cmsystem.infinityfreeapp.com is currently unable to handle this request.

HTTP ERROR 500

Other Information

so i was doing a php mailer on my register but its not working i also dont know what do folders, or php do i need to upload for phpmailer folder i kinda have problem on php mailer

Heres the code for it

<?php
// Include the database connection file
include('db.php');

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

// Include PHPMailer
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;


// Define error message variables
$usernameErr = $emailErr = $passwordErr = "";

// Register user
if (isset($_POST['register'])) {
    // Get form inputs
    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = $_POST['password'];

    // Validate form inputs
    if (empty($username)) {
        $usernameErr = "Username is required.";
    }

    // Email validation
    if (empty($email)) {
        $emailErr = "Email is required.";
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $emailErr = "Invalid email format.";
    } elseif (strpos($email, '@gmail.com') === false) {
        $emailErr = "Please use a Gmail address (e.g., [email protected]).";
    }

    if (empty($password)) {
        $passwordErr = "Password is required.";
    }

    // Check if email already exists in the database
    if (empty($usernameErr) && empty($emailErr) && empty($passwordErr)) {
        // Check if the email already exists
        $stmt = $conn->prepare("SELECT * FROM users WHERE email = ?");
        $stmt->bind_param("s", $email);
        $stmt->execute();
        $result = $stmt->get_result();

        if ($result->num_rows > 0) {
            // Email already exists
            $emailErr = "The email address is already registered.";
        } else {
            // Hash the password using bcrypt
            $hashed_password = password_hash($password, PASSWORD_DEFAULT);

            // Generate a random 6-digit verification code
            $verification_code = rand(100000, 999999);

            // Prepare and execute SQL query to insert user data into the 'users' table
            $stmt = $conn->prepare("INSERT INTO users (username, email, password, verification_code) VALUES (?, ?, ?, ?)");
            $stmt->bind_param("ssss", $username, $email, $hashed_password, $verification_code);

            if ($stmt->execute()) {
                echo "Registration successful! Please check your email for the verification code.";

                // Send verification code to the user's email using PHPMailer
                $mail = new PHPMailer(true);
                try {
                    // Server settings
                    $mail->isSMTP();
                    $mail->Host = 'smtp.gmail.com'; // Gmail SMTP server
                    $mail->SMTPAuth = true;
                    $mail->Username = '123'; // Your full Gmail address
                    $mail->Password = '123secret'; // If 2FA is enabled, use the App Password here
                    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
                    $mail->Port = 587; // Port for TLS

                    // Recipients
                    $mail->setFrom('[email protected]', 'Noel Mara'); // Your full Gmail address
                    $mail->addAddress($email); // Add recipient's Gmail address

                    // Content
                    $mail->isHTML(true);
                    $mail->Subject = 'Email Verification Code';
                    $mail->Body = 'Your verification code is: ' . $verification_code;

                    // Send email
                    $mail->send();
                    echo 'Verification code sent to ' . $email;

                    // Redirect to verify_otp.php with the email as a parameter
                    header("Location: verify_otp.php?email=" . urlencode($email));
                    exit();

                } catch (Exception $e) {
                    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
                }
            } else {
                echo "Error: " . $stmt->error;
            }

            $stmt->close();
        }
    }

    $conn->close();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>User Registration</title>
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light">

<div class="container d-flex justify-content-center align-items-center min-vh-100">
    <div class="row w-100 justify-content-center">
        <!-- Column takes 12 on small screens (full width) and 6 on medium and larger screens (half width) -->
        <div class="col-12 col-md-6 col-lg-4">
            <div class="card shadow">
                <div class="card-body">
                    <h5 class="card-title text-center fs-3 mb-3">User Registration</h5>
                    <form method="POST" action="register.php">
                        <!-- Username -->
                        <div class="mb-3">
                            <label for="username" class="form-label">Username</label>
                            <input type="text" class="form-control" id="username" name="username" required>
                            <span class="text-danger"><?php echo $usernameErr; ?></span>
                        </div>

                        <!-- Email -->
                        <div class="mb-3">
                            <label for="email" class="form-label">Email</label>
                            <input type="email" class="form-control" id="email" name="email" required>
                            <span class="text-danger"><?php echo $emailErr; ?></span>
                        </div>

                        <!-- Password -->
                        <div class="mb-3">
                            <label for="password" class="form-label">Password</label>
                            <input type="password" class="form-control" id="password" name="password" required>
                            <span class="text-danger"><?php echo $passwordErr; ?></span>
                        </div>

                        <!-- Submit Button -->
                        <button type="submit" name="register" class="btn btn-primary w-100">Register</button>
                    </form>
                    <hr>
                    <p class="text-center">Already have an account? <a href="login.php">Login here</a></p>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>

</body>
</html>

My folder



Please read

6 Likes

yep i already know whats causing the problem the php mailer but i dont know how to fix it since im kinda new on putting php mailer online since its just move the folder when im at localhost but idk what to do online when i erase the php mailer code it works but there is something wrong on i think the php mailer butt i dont get it so i was hoping if someone already did and find a ways to solve it

The first step is to read that article you were given and enable full error message display (so it will show more details instead of just a 500 error). You can enable it by following the instructions in that article

6 Likes

thanks for replying but it is already solve i just happen to change something on the exception my php mailer and the errors gone i just ask some ai what is the problem on it and solve it

Be careful using AI to correct code.

It can often make suggestions that have massive security issues.

Also AI code has a tendency of producing false postives by the scanning tool.

5 Likes

oh it just delete some parts and it works everything working perfectly fine First Version (official/latest from PHPMailer)

<?php

namespace PHPMailer\PHPMailer;

class Exception extends \Exception
{
    public function errorMessage()
    {
        return '<strong>' . htmlspecialchars($this->getMessage(), ENT_COMPAT | ENT_HTML401) . "</strong><br />\n";
    }
} 

Second Version

 <?php

namespace PHPMailer\PHPMailer;

class Exception extends \Exception
{
    public function errorMessage()
    {
        return '<strong>' . htmlspecialchars($this->getMessage()) . "</strong><br>\n";
    }
}
1 Like

just deleted this part , ENT_COMPAT | ENT_HTML401)

I added some code formatting to your post.

Please make sure to put source code in some kind of code block. The forum is very flexible with what type of formatting you use (Markdown, BBCode, plain HTML), but the side effect is that any source code you dump into it tends to get mangled so half is invisible and the other half is unreadable. Using the proper formatting keeps both your text and your code readable.

4 Likes

thats fine. I’ve just seen too many people say they’ve used AI code, and then the automated system flag them with a false positive for malicious code. Just wanted to make sure you didn’t fall into that trap :slight_smile:

2 Likes

i hope they can read this incase they will get same error like me or they want to get Php Mailer through online hosting

ok sorry i dont know about the formatting i cant find the button to do it or to do it im kinda new on this forum can you explain how to do it so i can try it next time

I don’t understand why this solved your issue though. The original code doesn’t appear to do anything that would not work.


Regarding the formatting, the editor has a button on the top that looks like </> that you can use. But again, the forum supports Markdown, BBCode and plain HTML, so you can just use your favorite formatting language and use whatever controls they provide.

5 Likes

i dont know but i got a code parse < like this idk maybe in my code affect something

ok thanks ill try to put it again incase maybe i just dont save some code or something or got confused saved on local host instead of infinity free i let u know

i forgot i delete some of php in my src file also maybe it also affects it i just put 3 php which is the phpmailer, stmp and exception

i did 3 changes so idk where is the correct or the one who fix it the changes was fixing some maybe a typo on here Preformatted text `require ‘PHPMailer/src/PHPMailer.php’;
require ‘PHPMailer/src/Exception.php’;
require ‘PHPMailer/src/SMTP.php’;

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;Preformatted text
or the next is deleting some of the php file on the src file or the third one is changing something on exception but I tried the exception and it works so I guess its now between the 2

this is the error before Parse error: syntax error, unexpected token “<”, expecting end of file in Exception.php

thats why i change it so maybe i just downloaded the exception.php and didnt change anything so idk why i got that error but i guess the error maybe i deleted something when i checking every php but idk why the ai change the code though if its only 1 < is gone