phpnamespacesphpmailerphp-7

Converting to phpmailer6 due to server upgrade to php7, namespace error


I hope someone can help me with converting PHPmailer code.

I have got a bit lost and can't work out where I am going wrong but it seems to be the namespace and use part, I have the following at the moment and although the require pages are OK (tested without the namespace and use code) I can't make it work!

namespace MyProject;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require '/home/lanoil/public_html/libraries/php_mailer/phpmailer.php';
require '/home/lanoil/public_html/libraries/php_mailer/SMTP.php';
require '/home/lanoil/public_html/libraries/php_mailer/Exception.php';

$mail = new PHPMailer(true);

FULL Page code

    <?php 
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

define('_JEXEC', 1);   

// this file is in a subfolder 'scripts' under the main joomla folder
define('JPATH_BASE', realpath(dirname(__FILE__) . '/..'));
require_once JPATH_BASE . '/includes/defines.php';
require_once JPATH_BASE . '/includes/framework.php';

// instantiate application
$app   = JFactory::getApplication('site');

// database connection
$dbj     = JFactory::getDbo();

// External database connection
JLoader::discover('Lanoil', JPATH_LIBRARIES . '/lanoil');
$config = new LanoilConfig();
$db = new LanoilDb();

if (isset($_POST['deadline'])) {

$deadline = ($_POST['deadline']/1000);

$db->query("update automate set deadline =".$deadline); // adding deadline to db for cron job.

$sql= "SELECT DISTINCT `email` FROM `tbl_clients` where `email` IS NOT NULL";
$stmt = $db->prepare($sql);
$stmt->execute();
$emails = array();
foreach($db->query($sql) as $row){
    array_push($emails, $row['email']);     
}

$test = implode(", ",$emails); 
echo $test; 

foreach($emails as $email){
   $mail->AddBCC($email);
}

function url_get_contents ($Url) {
    if (!function_exists('curl_init')){ 
        die('CURL is not installed!');
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $Url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}


namespace MyProject;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require '/home/lanoil/public_html/libraries/php_mailer/phpmailer.php';
require '/home/lanoil/public_html/libraries/php_mailer/SMTP.php';
require '/home/lanoil/public_html/libraries/php_mailer/Exception.php';

$mail = new PHPMailer(true);


try {

//$mail->Host       = "mail.yourdomain.com"; // SMTP server
  //Create a new PHPMailer instance
  $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
  $mail->SMTPAuth   = true;                  // enable SMTP authentication
  $mail->SMTPSecure = "tls";                 // sets the prefix to the servier
  $mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
  $mail->Port       = 587;                   // set the SMTP port for the GMAIL server
  $mail->Username   = "gordon@rural.co.uk";  // GMAIL username
  $mail->Password   = "password";            // GMAIL password
  $mail->AddReplyTo('gordon@rural.co.uk', 'Gordon Muir');
  $mail->AddAddress('gordon@rural.co.uk', 'Gordon Muir');
  $mail->SetFrom('gordon@rural.co.uk', 'Gordon Muir');
  $mail->AddReplyTo('gordon@rural.co.uk', 'Gordon Muir');
  $mail->Subject = 'Time to order your oil from the Rural Oil Club';
  $mail->AltBody = 'This email is to alert you that it is now time to order your oil from the Rural Oil, Club'; // optional - MsgHTML will create an alternate automatically
  $body = url_get_contents(page to call);
  $mail->MsgHTML($body);
  $mail->Send();
  echo "Message Sent OK
<p></p>
  \n";
} catch (Exception $e) {
    echo $e->errorMessage();
} catch (\Exception $e) {
    echo $e->getMessage();
}

// Publish Order Oil menu item
$query = $dbj->getQuery(true);
// Fields to update.
$fields = array(
    $dbj->quoteName('published') . ' = 1'
);
// Conditions for which records should be updated.
$conditions = array(
    $dbj->quoteName('id') . ' = 195'
);
$query->update($dbj->quoteName('#__menu'))->set($fields)->where($conditions);
$dbj->setQuery($query);
$result = $dbj->execute();
}
else
{
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
 <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
$(function() {
$( "#datepicker" ).datepicker({ dateFormat: "@" });
});

</script>
</head>

<body>
<form action="" method="post">
Deadline: <input type="text" name="deadline" id="datepicker"><br>
<input type="submit">
</form>

<?php }
?>

</body>
</html>

I haven't changed any of the PHPmailer code for the actual sending of the message yet as I need to get the first part right first. (The code won't be very tidy as I'm not a pro...obviously!)


Solution

  • Easy error: namespace needs to be the first thing in your file, before any other scripting, and it helps to stick use lines at the top too. That will be the error you're getting. This is covered in the PHP namespace docs.