我正在尝试做一个表格,可以通过电子邮件与PHP联系我。我对PHP很陌生,所以在看了几段我用PHPMailer制作的视频之后。在我从GitHub下载了zip文件并安装了composer之后,代码显示了一些错误。当我声明“使用\PHPMailer\PHPMailer\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\SMTP;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'vendor/autoload.php';
//Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'smtp.example.com'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = 'user@example.com'; //SMTP username
$mail->Password = 'secret'; //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; //Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = 587; //TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
//Recipients
$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User'); //Add a recipient
$mail->addAddress('ellen@example.com'); //Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');
//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. Mailer Error: {$mail->ErrorInfo}";
}
?>代码来自我刚刚复制和粘贴的GitHub页面,以测试它。我编辑了JSON文件和所有这些文件,但是它不起作用。感谢大家的帮助。谢谢
发布于 2021-05-12 06:38:23
最有可能的解释是,您正在运行的PHP比版本5.3更早,版本5.3是引入名称空间和use语句时使用的。升级PHP -任何新开发都应该使用8.0。
发布于 2021-05-12 01:35:38
将自动加载移动到文件的顶部,在自动加载之前使用PSR4。
<?php
require 'vendor/autoload.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\SMTP;
use PHPMailer\PHPMailer\Exception;
...尽管你真的应该把你的自动加载器放在一个单独的文件里。例如,创建一个index.php并创建自己的Mailer类。
您将如何做到这一点将添加一个自动加载到您的composer.json
{
"name": "acme/app",
"type": "project",
"authors": [
{
"name": "some name",
"email": "email@example.com"
}
],
"require": {
"phpmailer/phpmailer": "^6.4"
},
"autoload": {
"psr-4": {"Acme\\": "src/"}
}
}使用名为src的文件创建App.php文件夹
<?php
namespace Acme;
class App
{
public static function boot()
{
echo 'works'; // Do what ever you want.
}
}在根目录中创建一个index.php
<?php
require 'vendor/autoload.php';
Acme\App::boot();然后运行composer安装或composer du,并运行php index.php和您的集合。
发布于 2022-02-01 19:56:19
我已经看到了类被放置在PHP函数中的情况。这会引发一个错误。
来自文档
--这些必须在脚本的顶部,而不是在函数中 使用PHPMailer\PHPMailer\PHPMailer; 使用PHPMailer\PHPMailer\SMTP; 使用PHPMailer\PHPMailer\Exception;
https://stackoverflow.com/questions/67496103
复制相似问题