PHP class - PHPMailer unexpected 'use' (T_USE)

左心房为你撑大大i 提交于 2021-02-05 08:33:26

问题


I would to like use the PHPMailer classes for testing. After reading the official documentation I see there are two ways to include them in my project:

1) Using composer

2) Copying contents and include paths

I don't know how do use the first option, composer. The second option, copying contents and include paths, looks easier.

I have made a file named test.php with these lines of code:

<?php

        session_start();

        if(isset($_SESSION['username']) and $_SESSION['username'] != ''){

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

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

            $mail = new PHPMailer;

            echo 'Versión actual de PHP: ' . phpversion();

        }else{
            ?>
            <br>
        <br>
        <div class="row">
              <div class="text-center">
                  <p class='errorLogin'>Inactive session, relogin <a href="login.php">here</a></p>
              </div>
        </div>
<?php
        }?>

This code only loads the clases into the environment and makes instance of the object PHPMailer class.

After I run it, the log file shows an error:

[Tue Oct 17 10:17:10.331051 2017] [:error] [pid 3879] [client 192.168.0.184:50679] PHP Parse error: syntax error, unexpected 'use' (T_USE) in /var/www/test/sendMail.php

The PHP version: 5.6.30-0+deb8u1

Could anybody help me?


回答1:


The problem is your use of the use keyword. From the documentation:

The use keyword must be declared in the outermost scope of a file (the global scope) or inside namespace declarations. This is because the importing is done at compile time and not runtime, so it cannot be block scoped.

As such, your code should be something like this:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

session_start();

if (isset($_SESSION['username']) and $_SESSION['username'] != ''){
  [...]



回答2:


<?php 
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Your function Code 
?>

Use this classes on top of the page not inside of the function.




回答3:


<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';

If you're not using the SMTP class explicitly (you're probably not), you don't need a use line for the SMTP class.


As Per Doc of Phpmailer

<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

//Load composer's autoloader
require 'vendor/autoload.php';

$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'user@example.com';                 // SMTP username
    $mail->Password = 'secret';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('from@example.com', 'Mailer');
    $mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
    $mail->addAddress('ellen@example.com');               // Name is optional
    $mail->addReplyTo('info@example.com', 'Information');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');

    //Attachments
    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
}



回答4:


I still don't get it all with the use of "use" but in time I will learn and understand. My solution to the problem anyhow was to find other "use" in my code and place this use next to them - even if it was not at the beginning of the code.

Look for any other installed application like in my case Guzzle. Then simply put them together.



来源:https://stackoverflow.com/questions/46788047/php-class-phpmailer-unexpected-use-t-use

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!