首页 > 解决方案 > 如何在 php 文件中为 phpMailer 使用“use”关键字以避免意外的“use”(T_USE)错误

问题描述

这是我用于发送确认电子邮件的 php 代码。当我运行此代码时出现错误,

解析错误:语法错误,第 52 行 C:\xampp\htdocs\www\includes\signup.inc.php 中的意外“使用”(T_USE)

 <?php
    ...................
    ...................
  if (mysqli_stmt_execute($stmt)){
    $email_sent_to = $email;
    $email_sent_from = "mymail@gmail.com";
    $subject = "Verify your Email";
    $smtp_username = "mymail@gmail.com";
    $smtp_password = "mypassword";
    $message = "<a href='http://domain/verify.php?vkey=$vkey'>Register Your Account</a> ";

    //Include required phpMailer files
    require 'PHPMailer.php';
    require 'SMTP.php';
    require 'Exception.php';

    //Define name spaces
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\SMTP;
    use PHPMailer\PHPMailer\Exception;

    //Create instance of phpMailer
    $mail = new PHPMailer();
    //Set mailer to use smtp
    $mail->isSMTP();
    //Define smtp host
    $mail->Host = "smtp.gmail.com";
    //Enable smtp authentication
    $mail->SMTPAuth = "true";
    //Set type of encryption (ssl/tls)
    $mail->SMTPSecure = "tls";
    //Set port to connect smtp
    $mail->Port = "587";

    //Set gmail userName
    $mail->Username = $smtp_username;
    //Set gmail Password
    $mail->Password = $smtp_password;

    //Set content type
    $mail->isHTML(true); 
    //Set email Subject
    $mail->Subject = $subject;
    //Set sender email
    $mail->setFrom($emai_sent_from);
    //Email body
    $mail->Body = $message;
    //Add recipient
    $mail->addAddress($email_sent_to);

    //Finally send mail
        if ($mail->Send()){
            echo "Email Send Successfully...!";
        }else{
            echo "Failed to send Email....!";
        }

    //Clossing smtp connection
    $mail->smtpClose();

}


?>

我还尝试在代码开头和if (mysqli_stmt_execute($stmt))语句之后添加命名空间,但它会引发相同的错误。

标签: phpnamespacesphpmailer

解决方案


更改 require 和 use 定义

//Include required phpMailer files
require 'PHPMailer.php';
require 'SMTP.php';
require 'Exception.php';

//Define name spaces
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

if (mysqli_stmt_execute($stmt)){
    $email_sent_to = $email;
    $email_sent_from = "mymail@gmail.com";
    $subject = "Verify your Email";
    $smtp_username = "mymail@gmail.com";
    $smtp_password = "mypassword";
    $message = "<a href='http://domain/verify.php?vkey=$vkey'>Register Your Account</a> ";

    //Create instance of phpMailer
    $mail = new PHPMailer();
    //Set mailer to use smtp
    $mail->isSMTP();
    //Define smtp host
    $mail->Host = "smtp.gmail.com";
    //Enable smtp authentication
    $mail->SMTPAuth = "true";
    //Set type of encryption (ssl/tls)
    $mail->SMTPSecure = "tls";
    //Set port to connect smtp
    $mail->Port = "587";

    //Set gmail userName
    $mail->Username = $smtp_username;
    //Set gmail Password
    $mail->Password = $smtp_password;

    //Set content type
    $mail->isHTML(true); 
    //Set email Subject
    $mail->Subject = $subject;
    //Set sender email
    $mail->setFrom($emai_sent_from);
    //Email body
    $mail->Body = $message;
    //Add recipient
    $mail->addAddress($email_sent_to);

    //Finally send mail
        if ($mail->Send()){
            echo "Email Send Successfully...!";
        }else{
            echo "Failed to send Email....!";
        }

    //Clossing smtp connection
    $mail->smtpClose();

}

在 php 标记之后的代码顶部


推荐阅读