简体   繁体   English

“使用 PHPMailer\PHPMailer\PHPMailer”不起作用

[英]"use PHPMailer\PHPMailer\PHPMailer" not working

I am trying to make a form that can contact me via email with PHP.我正在尝试制作一个可以通过 email 和 PHP 与我联系的表格。 I am new to PHP so after watching a couple of videos I am making with PHPMailer.我是 PHP 的新手,所以在观看了我用 PHPMailer 制作的几个视频之后。 After I downloaded the zip file from GitHub and installing composer and these things, the code shows some errors.在我从 GitHub 下载 zip 文件并安装 composer 和这些东西后,代码显示了一些错误。 When I declare "use\PHPMailer\PHPMailer\PHPMailer," it says Unexpected, 'Unknown' .当我声明 "use\PHPMailer\PHPMailer\PHPMailer" 时,它会显示Unexpected, 'Unknown'

My code is this我的代码是这个

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

The code is from the GitHub page I just copied and pasted it to test it.该代码来自我刚刚复制并粘贴以测试它的GitHub页面。 I edited the JSON file and all of them but it doesn't work.我编辑了 JSON 文件和所有文件,但它不起作用。 Appreciate the help from everyone.感谢大家的帮助。 Thank you谢谢

Move your autoload to the top of the file, using PSR4 before the autoload is in wont work.将自动加载移动到文件顶部,在自动加载不起作用之前使用 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;
...

Though you should really keep your autoloader in a seperate file.尽管您确实应该将自动加载器保存在单独的文件中。 eg a index.php and create a Mailer class of your own.例如index.php并创建您自己的 Mailer class。

How you would go about doing this would be adding an autoload to your composer.json您将如何 go 执行此操作将自动加载到您的composer.json 。json

{
    "name": "acme/app",
    "type": "project",
    "authors": [
        {
            "name": "some name",
            "email": "email@example.com"
        }
    ],
    "require": {
        "phpmailer/phpmailer": "^6.4"
    },
    "autoload": {
        "psr-4": {"Acme\\": "src/"}
    }
}

Creating a src folder with a file named App.php使用名为App.php的文件创建src文件夹

<?php
namespace Acme;

class App
{
    public static function boot()
    {
        echo 'works'; // Do what ever you want. 
    }
}

Create a index.php in the root在根目录下创建index.php

<?php
require 'vendor/autoload.php';

Acme\App::boot();

then run composer install or composer du and run php index.php and your set.然后运行 composer install 或 composer du 并运行php index.php和你的设置。

The most likely explanation is that you are running PHP that's older than version 5.3, which is when namespaces and the use statement were introduced.最可能的解释是您运行的 PHP 早于 5.3 版,此时引入了命名空间和 use 语句。 Upgrade your PHP - any new development should be using 8.0.升级您的 PHP - 任何新的开发都应该使用 8.0。

In my case, I found that the classes were being loaded inside another PHP function.就我而言,我发现这些类被加载到另一个 PHP function 中。 That caused the error.这导致了错误。

From the documentation :文档中:

These must be at the top of your script, not inside a function这些必须在脚本的顶部,而不是在 function 内
use PHPMailer\PHPMailer\PHPMailer;使用 PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;使用 PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;使用 PHPMailer\PHPMailer\Exception;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM