【问题标题】:PHPMailer throwing an error looking for PHPMailer\PHPMailer\Exception which isn't in my code?PHPMailer 在寻找不在我的代码中的 PHPMailer\PHPMailer\Exception 时抛出错误?
【发布时间】:2018-12-19 08:40:10
【问题描述】:

在 PHPMailer 代码方面需要一些帮助。来自日志的错误低于在发送表单时出现的错误。已阅读所有引用此错误的帖子,但我的问题有所不同,因为我不允许在托管公司的共享服务器上使用 Composer。我正在手动安装文件。

这是日志文件错误:

[18-Dec-2018 22:30:51 UTC] PHP Fatal error:  Uncaught Error: Class 'PHPMailer\PHPMailer\Exception' not found in /home1/example/public_html/PHPMailer/src/PHPMailer.php:1720
Stack trace:
#0 /home1/example/public_html/PHPMailer/src/PHPMailer.php(1518): PHPMailer\PHPMailer\PHPMailer->mailSend('Date: Tue, 18 D...', '<html>".$feedba...')
#1 /home1/example/public_html/PHPMailer/src/PHPMailer.php(1352): PHPMailer\PHPMailer\PHPMailer->postSend()
#2 /home1/example/public_html/adoption/sendEmailTest.php(22): PHPMailer\PHPMailer\PHPMailer->send()
#3 {main}
  thrown in /home1/example/public_html/PHPMailer/src/PHPMailer.php on line 1720

这是第 1719-1721 行的代码,但此文件是 PHPMailer 的 OOB:

if (!$result) {
            throw new Exception($this->lang('instantiate'), self::STOP_CRITICAL);
        }

这是我的 php 文件代码:

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

require '../PHPMailer/src/Exception.php';
require '../PHPMailer/src/PHPMailer.php';
require '../PHPMailer/src/SMTP.php';

if(isset($_POST['submit'])){    
    $email = $_POST['email'];
    $name = $_POST['name'];
    $feedback = $_POST['feedback'];

    $mail = new PHPMailer();

    $mail->setFrom('myname@example.org', 'Org Name');
    $mail->addReplyTo($email, $name);
    $mail->addAddress('myname@example.org', 'Org Name');

    $mail->isHTML(true);

    $mail->Subject = 'Application Submission';  
    $mail->Body = '<html>".$feedback."</html>';

    if (!$mail->send())
    {
        echo $mail->ErrorInfo;
    }
}

?>

这是表单代码,所以你有它:

    <form action="sendEmailTest.php" method="post" name="adoption">

        <table width="700" border="0" cellspacing="3" cellpadding="3">
            <tr>
                <td colspan="2" bgcolor="#FFFFCC"><h2><strong>Form Test</strong></h2></td>
            </tr>
            <tr>
                <td width="183"><label>Name: </label></td>
                <td width="496"><input name="name" type="text" required="required" id="name" value="" size="75" /></td>
            </tr>
            <tr>
                <td><label>Email Address: </label></td>
                <td><input type="text" name="email" id="email" value="" size="75" /></td>
            </tr>
            <tr>
                <td><label>Feedback</label></td>
                <td><textarea name="feedback" id="feedback" cols="45" rows="5"></textarea></td>
            </tr>
            <tr>
                <td><input type="submit" value="Submit Form" name="submit"/></td>
            </tr>
        </table>     
    </form>

【问题讨论】:

  • 谢谢你们!打算把它们扔回去试一试,但我目前没有在我的代码中使用异常或 smpt 类,这让我很困惑。我按照这篇文章说如果我不使用它们,删除它们? “如果您不想使用异常,只需省略参数(或将其设置为 FALSE)。在这种情况下,您需要检查 send() 方法的返回值并在 $ErrorInfo 属性中查找错误消息:”@987654321 @
  • 教程 0、@KirkBeard 和不要惊慌 1...我将它们重新添加进去,错误就消失了。现在有一个新错误(无法实例化邮件功能)但需要研究它,因为它对我来说是一个新错误。谢谢你们!
  • @CinderGirl 不客气!我在我的答案中添加了一些解释,以解决您的 cmets 问题。希望这将有助于减轻一些混乱。 :-)
  • 在这种情况下不能使用 composer 是不正确的。您可能无法在您的服务器上运行它,但您可以在本地运行它并上传它创建的供应商文件夹以及您的其余代码。自述文件对此进行了描述。

标签: php phpmailer


【解决方案1】:

您的代码在开始时似乎缺少所需的类。

您的示例包括:

<?php

use PHPMailer\PHPMailer\PHPMailer;
require '../PHPMailer/src/PHPMailer.php';

你需要添加:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require '../PHPMailer/src/Exception.php';
require '../PHPMailer/src/PHPMailer.php';
require '../PHPMailer/src/SMTP.php';

这应该可以解决您在帖子中提到的 PHP 错误。

【讨论】:

    【解决方案2】:

    如果您不使用自动加载,则必须要求并使用代码中使用的任何类。

    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    
    require '../PHPMailer/src/PHPMailer.php';
    require '../PHPMailer/src/Exception.php';
    

    关于您对该问题的评论,您没有在代码中使用异常,但 PHPMailer 类在内部使用它们。将控制器的异常参数留空并不会完全禁用 PHPMailer 异常。如果您查看源代码,您会看到throw new Exception... 被包裹在if ($this-&gt;exceptions) { } 中的一些地方,而其他地方无论如何都会抛出它们。

    您在添加 require/use 语句后遇到的异常并不是真正的新问题,它是您在遇到“找不到类”错误时最初尝试抛出的异常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-19
      • 2010-09-24
      • 2023-03-30
      相关资源
      最近更新 更多