2017-10-17 2 views
-1

Ich würde gerne die Klassen von PHPMailer zum Testen verwenden. lesen Nach dem oficial documentation ich sehe, gibt es zwei Möglichkeiten in meinem Projekt gehören:PHP-Klasse - PHPMailer unerwartete "Verwendung" (T_USE)

1) Unter Verwendung Komponist

2) Kopieren von Inhalten und Suchpfad

Die erste Option, Komponist, ich weiß nicht, wie geht es? Und die zweite Möglichkeit, Inhalte zu kopieren und Pfad einzuschließen, sieht einfacher aus.

Ich habe eine test.php mit diesen Linien machen:

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

Dieser Code nur den clases in Umgebung laden und Instanz der Objekt PHPMailer Klasse.

Nach Durchlauf es das Logfile zeigt einen Fehler:

[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

Die PHP-Version: 5.6.30-0 + deb8u1

Kann mir jemand helfen?

+0

Nicht sicher, aber ich denke, müssen Sie 'require' und dann' use' –

+0

@MilanChheda bereits versuchen Sie es zuerst erforderlich und nach benutze es. Aber es ist der gleiche Fehler. – rumar

+0

welche php version verwendest du? – Philipp

Antwort

3

Das Problem ist die Verwendung des Schlüsselwortes use. Aus der Dokumentation:

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.

Als solche sollten Sie Ihren Code so etwas wie dieses:

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

session_start(); 

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

danke. Das Problem war nur das äußerste Ausmaß. – rumar

0
<?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'; 

Wenn Sie nicht den SMTP-Klasse explizit (Sie sind wahrscheinlich nicht) , benötigen Sie keine Benutzungszeile für die SMTP-Klasse.


Wie Per Doc von 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 = '[email protected]';     // 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('[email protected]mple.com', 'Mailer'); 
    $mail->addAddress('[email protected]', 'Joe User');  // Add a recipient 
    $mail->addAddress('[email protected]');    // Name is optional 
    $mail->addReplyTo('[email protected]', 'Information'); 
    $mail->addCC('[email protected]'); 
    $mail->addBCC('[email protected]'); 

    //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; 
} 
Verwandte Themen