2017-11-25 189 views
-1

我工作的一個項目,我需要發送電子郵件用戶輸入電子郵件地址和密碼後。他們可以激活他們發送電子郵件的帳戶點擊。但是當我使用PHPMailer時出現錯誤。錯誤是像新的PHPMailer需要PHPMailerAutoload.php?

Fatal error: Uncaught Error: Class 'PHPMailer\PHPMailer' not found in C:\xampp\htdocs\E-Commerce-New-2\registration.php:20 Stack trace: #0 {main} thrown in C:\xampp\htdocs\E-Commerce-New-2\registration.php on line 20

在新的PHPMailer版本使用PHPMailerAutoload.php文件?我認爲答案是否定的。

我嘗試使用該代碼$mail = new PHPMailer\PHPMailer();,也驗證碼$mail = new PHPMailer();還沒有人工作。我把我的文件結構向下跌破

File Strucure

而且在這裏我過去我的代碼行。

<?php 
     session_start(); 
     include("includes/db.php"); 
     include("functions/functions.php"); 
     include('PHPMailer/PHPMailer.php'); 
    ?> 
    <?php 
     // If the values are posted, insert them into the database. 
     if (isset($_POST['email']) && isset($_POST['password'])){ 
      $email = $_POST['email']; 
      $password = $_POST['password']; 

      $verification_key = md5($email); 
      $query = "INSERT INTO `customers` (customer_pass,customer_email,verification_key) VALUES ('$password', '$email','$verification_key')"; 

      $result = mysqli_query($con, $query); 
      if($result){ 

      $mail = new PHPMailer\PHPMailer(); 
      $mail->setFrom('[email protected]clickshop.com'); 
      $mail->addAddress($email,'test'); 
      $mail->Subject = "Verify Your Account"; 
      $mail->isHTML(true); 
      $mail->Body = ' Please verify your email address to activate your account by clicking on this link <br> 
       <a href="http://localhost/E-Commerce-New-2/verify.php?key="'. $verification_key.'>Click Here</a>'; 

       if(!$mail->send()) { 
        $fmsg= 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo; 
       } else { 
        $smsg = 'User Registration successfull. Please Check your email to Activate Account!'; 
       } 
     }else{ 
      $fmsg = "User registration failed"; 
     } 
     } 
    ?> 

有人可以給我一個建議嗎?

+0

有些人否決沒有解釋任何理由 –

+0

你真的需要把在短短的最小的量的努力來解決這個問題 - 這可能是爲什麼你得到downvotes。閱讀PHPMailer附帶的自述文件,並將代碼基於所提供的示例。花費3小時努力的 – Synchro

+0

可能不多?我知道了。謝謝你的建議 –

回答

1

在新的PHPMailer V6.0 +,你現在需要的PHPMailer的類導入到全局命名空間,在你的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'; 

$mail = new PHPMailer(); 
$mail->IsSMTP(); 
$mail->Host = "mail.example.com"; 

$mail->SetFrom("$from", "$from"); 
$mail->AddAddress("$to"); 

$mail->Subject = "$subject"; 
$mail->Body = "$message"; 
$mail->Send();