php - 如何在 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))
语句之后添加命名空间,但它会引发相同的错误。
解决方案
更改 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 标记之后的代码顶部
推荐阅读
- javascript - 检查数组中的多个数字
- python - 我如何在 Instagram 中查看我的关注者的用户名
- python - 如何在纯 Tensorflow 中重写 Keras Stateful LSTM?
- typeorm - 如何允许与可为空的循环关系:假?
- java - 使用 thread1 打印数字 1,2,3,使用 thread2 打印数字 4,5,6,使用 thread3 打印数字 7,8,9,再使用 thread1 打印 10,11,12
- python-3.x - 使用类变量和类方法计算实例创建时不支持的操作数类型错误
- php - 如何在谷歌云平台上从 PHP 7.0 升级到 7.3?
- azure - 免费的 Azure AD 是否允许自行重置密码?
- r - 如何在 RStudio 中重新缩放森林图?
- php - 如何写... 5个字符后