doujiao3998 2018-12-18 22:42
浏览 610
已采纳

PHPMailer抛出错误寻找PHPMailer \ PHPMailer \ Exception哪个不在我的代码中?

Need some help with PHPMailer code. Error from log is below which appears on send of form. Have read all of the posts referencing this same error but my issue is different as I am not allowed to use Composer on my hosting company's shared server. I am following a manual install of the files.

Here's the log file error:

[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

Here's the code from lines 1719-1721, but this file is OOB for PHPMailer:

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

Here's my php file code:

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

?>

Here's the form code so you have it:

    <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>
  • 写回答

2条回答 默认 最新

  • drvlf9739 2018-12-18 23:34
    关注

    Your code appears to be missing the required classes at the start.

    Your example includes:

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

    You need to add:

    <?php
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    
    require '../PHPMailer/src/Exception.php';
    require '../PHPMailer/src/PHPMailer.php';
    require '../PHPMailer/src/SMTP.php';
    

    This should solve the PHP errors you mentioned in your post.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵
  • ¥15 cfx离心泵非稳态计算