【问题标题】:Getting Fatal error: Cannot redeclare class PHPMailer\PHPMailer\PHPMailer [closed]出现致命错误:无法重新声明类 PHPMailer\PHPMailer\PHPMailer [关闭]
【发布时间】:2020-10-16 04:47:33
【问题描述】:
    <?php
    /**
     * send mail short leave
     */
    require_once("../../hrm/Emails/smtp/PHPMailer.php");
      require_once("../../hrm/Emails/smtp/SMTP.php");
        
    class SendMailsleave
    {
        private $as;
        
        function __construct($u_id)
        {
            $this->as = $u_id;
            //echo $this->as;
            $this->SendMail();
    
        }
    
        public function SendMail()
        {
    
            //if somehow admin email failed , as a redirect.
            $sendto="noreply@caddcentre.lk";
            $senn="defaultcadd";
            include("../connection.php");
            require '../Emails/smtp/PHPMailer.php';
    
    $mail = new PHPMailer();
    
    $mail->IsSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'box894.bluehost.com'; 
    $mail->Host = 'mail.ectc.lk'; 
    
                    // Specify main and backup server
    $mail->Port = 26;                                // Set the SMTP port
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'noreply@ectc.lk';                // SMTP username
    $mail->Password = 'Admin@2020';     // SMTP password
    //$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted
    
    $mail->From = 'noreply@ectc.lk';
    $mail->FromName = 'ECTC HRM System';
    
    
    $mail->AddAddress($sendto, $senn);  // Add a recipient
    //$mail->AddAddress('myemail@gmail.com');               // Name is optional
    
    $mail->IsHTML(true);                                  // Set email format to HTML

尝试执行此操作时出现错误:

致命错误:无法重新声明类 PHPMailer\PHPMailer\PHPMailer in C:\wamp\www\hrm\Emails\smtp\PHPMailer.php 在第 33 行

【问题讨论】:

  • 请删除此行require '../Emails/smtp/PHPMailer.php';

标签: php email phpmailer


【解决方案1】:

您在 php 脚本中声明 PHPMailer.php 类两次请删除 SendMail 函数中的 require '../Emails/smtp/PHPMailer.php'; 行 ... 因为您已经声明了 PHPMailer 类在脚本的开头。

谢谢

【讨论】:

  • 完全正确。由于提问者是 PHP 新手,我建议学习的下一件事是如何使用 Composer,因为它有助于避免此类问题,尤其是当您的项目变得更大时。
猜你喜欢
  • 2018-02-16
  • 2021-10-23
  • 2019-06-30
  • 2016-06-10
  • 1970-01-01
  • 2020-10-02
  • 2021-05-16
相关资源
最近更新 更多