PHPMailer won't work in a function

centurionguy

New member
I can send a test email using PHP Mailer if all the code is on one file, but if I used an 'include' or 'require' then it fails.
Example that works: (test1.php)

PHP:
//test1.php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer00/src/Exception.php';
require 'PHPMailer00/src/PHPMailer.php';
require 'PHPMailer00/src/SMTP.php';
        $mail = new PHPMailer();

        $mail->SMTPDebug = 2;
        $mail->Mailer = "smtp";
        $mail->Host = "mail.myhost.com";
        $mail->Port = 587;
        $mail->SMTPAuth = true; // turn on SMTP authentication
        $mail->Username = [EMAIL]auth@myhost.com[/EMAIL]"; // SMTP username
        $mail->Password = "authPassword"; // SMTP password
        $mail->Priority = 1;

        $mail->AddAddress("[EMAIL]to@myhost.com[/EMAIL]","Adressee");
        $mail->SetFrom("[EMAIL]from@myhost.com[/EMAIL], "Sender");
        $mail->AddReplyTo("[EMAIL]from@myhost.com[/EMAIL], "Sender");

        $mail->Subject  = "Hello";
        $mail->Body     = "This is my message.";
        $mail->WordWrap = 50;

        if(!$mail->Send()) {
        echo 'Email message was <b>NOT</b> sent.';
        echo 'Mailer error: ' . $mail->ErrorInfo;
        } else {
        echo 'Email message has been sent.';
        }
?>
The above runs OK and the email is sent.


Example that fails:
(testmail.php)
PHP:
//testmail.php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

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

require ('testmailfunction.php');

testmailfunction();
?>
Creates the function (testmailfunction.php)

PHP:
//testmailfunction.php
function testmailfunction(){
      
        $mail = new PHPMailer();

        $mail->SMTPDebug = 2;
        $mail->Mailer = "smtp";
        $mail->Host = "mail.myhost.com";
        $mail->Port = 587;
        $mail->SMTPAuth = true; // turn on SMTP authentication
        $mail->Username = [EMAIL]auth@myhost.com[/EMAIL]"; // SMTP username
        $mail->Password = "authPassword"; // SMTP password
        $mail->Priority = 1;

        $mail->AddAddress("[EMAIL]to@myhost.com[/EMAIL]","Adressee");
        $mail->SetFrom("[EMAIL]from@myhost.com[/EMAIL], "Sender");
        $mail->AddReplyTo("[EMAIL]from@myhost.com[/EMAIL], "Sender");

        $mail->Subject  = "Hello";
        $mail->Body     = "This is my message.";
        $mail->WordWrap = 50;

        if(!$mail->Send()) {
        echo 'Email message was <b>NOT</b> sent.';
        echo 'Mailer error: ' . $mail->ErrorInfo;
        } else {
        echo 'Email message has been sent.';
        }
}
?>
When I run testmail.php I get the following error:

Fatal error: Uncaught Error: Class "PHPMailer" not found in C:\xampp\htdocs\testmailfunction.php:4 Stack trace: #0 C:\xampp\htdocs\testmail.php(11): testmailfunction() #1 {main} thrown in C:\xampp\htdocs\testmailfunction.php on line 4

I am lost because the code from the working file (test1.php) is pasted into the 'function' file (testmailfunction.php) to create the function. No other changes have been made.
What am I missing here? Any hep would be appreciated.
 
Last edited by a moderator:
You are using the PHPMailer class inside the testmailfunction.php file, so you need to import the class there, the file should starts with:
PHP:
<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

//testmailfunction.php
function testmailfunction(){
      
        $mail = new PHPMailer();
 
Back
Top