2017-10-17 3 views
-1

테스트 용 PHPMailer 클래스를 사용하고 싶습니다.PHP 클래스 - PHPMailer 예기치 않은 'use'(T_USE)

작곡가

2) 복사 내용과 포함 경로를 사용

1)

첫 번째 옵션, 작곡가, 나도 몰라 : 후 내 프로젝트에 포함하는 방법은 두 가지가 있습니다 참조 oficial documentation 읽기 어때? 두 번째 옵션 인 내용을 복사하고 경로를 포함하는 것이 더 쉬운 것처럼 보입니다.

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

이 코드는 환경에 clases을로드하고 객체 PHPMailer 클래스의 인스턴스를 만들 :

나는이 라인으로 test.php을 가지고있다.

은 실행 한 후에는 로그 파일에 오류를 보여줍니다

[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

PHP의 버전 : 5.6.30-0 + deb8u1

누군가가 나를 도울 수 있을까요?

+0

확실하지 Phpmailer의 문서에 따라 ,하지만 난 생각을 수행해야'require' 먼저 다음'use' –

+0

는 @MilanChheda 이미 필요로 먼저 시도 후 그걸 써. 하지만 같은 오류입니다. – rumar

+0

어떤 PHP 버전을 사용하고 있습니까? – Philipp

답변

3

문제는 use 키워드를 사용하는 것입니다. 문서에서 :

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

session_start(); 

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

감사합니다. 문제는 가장 바깥 쪽 범위에서만 선언되었습니다. – 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'; 

가 명시 적으로 SMTP 클래스를 사용하지 않는 경우 (당신은 아마 아니에요) : 이와 같이

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.

, 코드는 다음과 같이해야한다 SMTP 클래스에는 사용 줄이 필요하지 않습니다.


<?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]', '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; 
}