我正在尝试使用我手动下载的phpmailer库发送电子邮件。我尝试遵循https://alexwebdevelop.com/phpmailer-tutorial/上提供的教程,但我没有选择使用composer的选项,因为我对Composer的了解非常有限..请多多包涵:)
我在主机的服务器上的一个文件夹中安装了一个src zip文件,如下图所示:
运行PHP脚本时,出现以下错误消息:
我的代码源'send_mail_3.php'已安装在名为'php'(web2 \ php)的文件夹中
我今天正在寻求支持,因为我不知道从哪里开始调查。
我希望这个问题对其他用户来说是可以理解和有用的。
感谢您的帮助。
这里是我的send_mail_3.php的内容:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
/* Exception class. */
require '\PHPMailer\src\Exception.php';
/* The main PHPMailer class. */
require '\PHPMailer\src\PHPMailer.php';
/* SMTP class, needed if you want to use SMTP. */
require '\PHPMailer\src\SMTP.php';
/* Create a new PHPMailer object. Passing TRUE to the constructor enables exceptions. */
$mail = new PHPMailer(TRUE);
/* Open the try/catch block. */
try {
/* Set the mail sender. */
$mail->setFrom('[email protected]', 'Darth Vader');
/* Add a recipient. */
$mail->addAddress('[email protected]', 'Emperor');
/* Set the subject. */
$mail->Subject = 'Force';
/* Set the mail message body. */
$mail->Body = 'There is a great disturbance in the Force.';
/* Finally send the mail. */
$mail->send();
}
catch (Exception $e)
{
/* PHPMailer exception. */
echo $e->errorMessage();
}
catch (\Exception $e)
{
/* PHP exception (note the backslash to select the global namespace Exception class). */
echo $e->getMessage();
}
您在混淆路径和名称空间。您的use
语句正确,但是您的require路径却不正确。假设您已将PHPMailer放在脚本旁边的PHPMailer
文件夹中,则应为:
/* Exception class. */
require './PHPMailer/src/Exception.php';
/* The main PHPMailer class. */
require './PHPMailer/src/PHPMailer.php';
/* SMTP class, needed if you want to use SMTP. */
require './PHPMailer/src/SMTP.php';
除此之外,学习如何使用作曲家。
因为您正在加载php mailer的名称空间,所以我认为您只需要使用完整路径即可,例如require '\PHPMailer\src\Exception.php';
。您可以尝试执行以下步骤:
尝试为名称空间设置别名
use PHPMailer\PHPMailer\PHPMailer as Mailer;
需要它,但使用别名名称空间:require 'Mailer\PHPMailer.php';