Search code examples
phpsmtpgmailphpmailer

Having problem with sending email with PHPMailer?


When I run my php program I get this error:

Undefined variable: mail in C:\xampp\htdocs\Trying_login_register\controllers\emailcontroller.php on line 22

here is my php code:

<?php
require 'vendor/autoload.php';
require 'config/constant_email.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;

$mail = new PHPMailer(true);
global $mail;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = EMAILaddress; //paste one generated by Mailtrap
$mail->Password = PASSWORD; //paste one generated by Mailtrap
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
function sendVerificationEmail($userEmail, $token)
{
    $body='<h1>Send HTML Email using SMTP in PHP</h1>
    <p>This is a test email I’m sending using SMTP mail server with PHPMailer.</p>';

    $mail->setFrom(EMAILaddress);
    $mail->addReplyTo(EMAILaddress);
    $mail->addAddress('ADDRESS');
    $mail->Subject = 'Verify your email address';
    $mail->isHTML(true);
    $mail->Body = $body;
    if ($mail->send()) {
        echo 'message has been successfully sent';
    }

    else {
        echo 'Mailor error: ' . $mail->ErrorInfo;
    }
}

I can't find the problem. Please help me.


Solution

  • This code won't work for three reasons:

    Firstly, $mail is already defined in the global scope, so you don't need global $mail there - just delete that line.

    Next, your sendVerificationEmail function does need to access the $mail global, so you should add global $mail; inside that function.

    Lastly, it still won't do anything because though you have defined the sending function, you're not calling it, so its code will never run.

    Another minor issue is that you have requested that PHPMailer throws exceptions on error (by passing true to the constructor), but you have no exception handling, so if there is an error your script will just die rather than handling the error nicely. The if statement you have around the send call will do fine.

    While those things will fix what you have written, a simpler approach would be to just drop the function definition altogether, like this:

    require 'vendor/autoload.php';
    require 'config/constant_email.php';
    
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\SMTP;
    
    $mail = new PHPMailer();
    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;
    $mail->Username = EMAILaddress; //paste one generated by Mailtrap
    $mail->Password = PASSWORD; //paste one generated by Mailtrap
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port = 587;
    $body='<h1>Send HTML Email using SMTP in PHP</h1>
    <p>This is a test email I’m sending using SMTP mail server with PHPMailer.</p>';
    $mail->setFrom(EMAILaddress);
    $mail->addReplyTo(EMAILaddress);
    $mail->addAddress('ADDRESS');
    $mail->Subject = 'Verify your email address';
    $mail->isHTML(true);
    $mail->Body = $body;
    if ($mail->send()) {
        echo 'message has been successfully sent';
    } else {
        echo 'Mailor error: ' . $mail->ErrorInfo;
    }