I have read a bunch of questions and answers (like this and this and even this which might be more relevant) on SO regarding this. They all deal with a file not being found due to a path incorrectly specified. In my case, the file is found and included (as you will see below), but the function is still not found. I get the error:
Fatal error: Call to undefined function mysender() in ....
Here is my first file (test.php) in which I am including another file (myemailsender.php below)
<?php
// File: test.php
namespace PHPMailer\PHPMailer;
error_reporting(E_ALL);
ini_set('display_errors', 1);
// I have tried all of the below methods
require_once('../files/myemailsender.php'); // I am pretty sure this works
// require_once('/var/www/example.com/files/mysender.php');
// require_once($_SERVER['DOCUMENT_ROOT'] . '/files/mysender.php'); // I can't use this because the file has to be executed as a Cron which doesn't have Server Variables.
// require_once('https://example.com/files/mysender.php'); // this gives a wrapper error
// Code to set the various variables goes here...
$SendParams = array("SendTo"=>$SendTo,
"Body"=>$EmailMessage,
"Subject"=>$Subject);
mysender($SendParams);
?>
Here is the file myemailsender.php
<?php
// File: myemailsender.php
namespace PHPMailer\PHPMailer;
error_reporting(E_ALL);
ini_set('display_errors', 1);
echo "<br><br>DOCUMENT_ROOT: " . $_SERVER['DOCUMENT_ROOT']; // This prints!
require_once('../PHPMailer-master/src/PHPMailerAutoload.php');
require_once('../PHPMailer-master/src/PHPMailer.php');
require_once('../PHPMailer-master/src/SMTP.php');
require_once('../PHPMailer-master/src/Exception.php');
echo "<br><br>SERVER_NAME: " . $_SERVER['SERVER_NAME']; // This also prints!
// The below commented code works when this file is executed by itself in the browser
// $SendParams = array("SendTo"=>array("a@example.net,Me", "b@example.com, CS"),
// "Body"=>"This is a test from SendEmail and contains only default text in the message body.",
// "Subject"=>"This is the default Subject");
// SendEmail($SendParams); This works if it is used in this file
function mysender($params) {
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Host = "smtp.gmail.com";
$mail->Mailer = "smtp";
$mail->Port = 587;
$mail->Username = "examplemail@gmail.com";
$mail->Password = "xxxxxxxxxx";
// removed the code which parses $params and creates the Body Subject etc.
$mail->Body = $Body;
$mail->Subject = $Subject;
$mail->SetFrom('z@example.com', 'example');
$mail->addReplyTo('y@example.com');
$mail->addBCC('x@gmail.com');
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
}
echo "<br><br>REMOTE_ADDR: " . $_SERVER['REMOTE_ADDR']; //So does this!
?>
As I have noted, those messages print, so AFAICT the file is being loaded. I removed the entire code inside the function to make sure that the problem wasn't caused by bad code. Then, I started removing other pieces of code and found one that worked! I removed the line:
namespace PHPMailer\PHPMailer;
and the error went away! Of course, that makes the whole thing useless. Am I doing something wrong?
Incidentally, I did another test. I used the following code:
<?php
function consolelog($data) {
if (is_array($data) || is_object($data)) {
echo "<script>console.log('" . json_encode($data) . "');</script>";
} else {
echo "<script>console.log('". $data . "');</script>";
}
}
?>
and included it in the same file (test.php above), like this:
require_once('../files/consolelog.php');
consolelog("It is working!");
The above works - the message appears in the page Console. So circling back, it appears that namespace line is the culprit. Can it be? How can I fix it?