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

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.

    点赞 打赏 评论
  • dpp89959 2018-12-18 23:34

    If you aren't using autoloading, you'll have to require and use any classes used in your code.

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

    Regarding your comment on the question, you aren't using Exceptions in your code, but the PHPMailer class uses them internally. Leaving the exceptions argument of the controller empty does not disable PHPMailer exceptions altogether. If you look through the source you'll see some places where throw new Exception... is wrapped in if ($this->exceptions) { }, and other places where it will throw them regardless.

    The exception you got after adding the require/use statements isn't really a new problem, it's the exception that was originally trying to be thrown when you got the "class not found" error.

    点赞 打赏 评论

相关推荐 更多相似问题