由于服务器升级到php7,转换为phpmailer6,名称空间错误

时间:2017-10-23 16:50:34

标签: namespaces phpmailer php-7

我希望有人可以帮我转换PHPmailer代码。

我有点迷失,无法解决我出错的地方,但它似乎是命名空间和使用部分,我现在有以下内容,虽然需求页面没问题(测试没有命名空间和使用代码)我无法使它工作!

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);

完整页面代码

    <?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>

我还没有改变任何PHPmailer代码来实际发送消息,因为我需要首先获得第一部分。 (代码不会很整洁,因为我不是专业人士......显然!)

1 个答案:

答案 0 :(得分:1)

简单错误:use需要成为您文件中的第一件事,在任何其他脚本编写之前,它也有助于将function convertTo() { var lon,lat; var str =''; for(lat = 52 + (1/3600) ; lat <= 52 + (3/3600) ; lat=lat+(1/3600)) { for(lon = 9 + (1/3600) ; lon<= 9 + (4/3600) ; lon=lon+(1/3600)) { str+="W;"+lat+";"+lon+"\n"; } } return str; }; var CSV = convertTo(); window.URL = window.webkitURL || window.URL; var contentType = 'text/csv'; var csvFile = new Blob([CSV], {type: contentType}); var a = document.createElement('a'); a.download = 'my.csv'; a.href = window.URL.createObjectURL(csvFile); a.textContent = 'Download as CSV'; a.dataset.downloadurl = [contentType, a.download, a.href].join(':'); document.body.appendChild(a); 行放在顶部。这将是你得到的错误。这包含在the PHP namespace docs