【问题标题】:Fatal error: Cannot redeclare PHPMailerAutoload()致命错误:无法重新声明 PHPMailerAutoload()
【发布时间】:2015-08-07 20:21:57
【问题描述】:

我正在使用 PHPMailer 从本地主机发送电子邮件。

我编写了一个函数,它应该向选择接收电子邮件选项的注册用户发送电子邮件。 (即通讯订阅等)

function email_users($subject, $body) {
    include('core/db/db_connection.php');
    $sql = "SELECT email, first_name FROM `_users` WHERE allow_email = 1";
    $query = mysqli_query($dbCon, $sql);
    while (($row = mysqli_fetch_assoc($query)) !== false) {
        $body = "Hello ". $row['first_name'] . ", <br><br>" . $body;
        email($row['email'], $subject, $body);
    }
}

调用函数的代码:

if (isset($_GET['success']) === true && empty($_GET['success']) === true) {
        ?>
            <h3 class="email_success">Emails have been sent</h2>
            <a href="admin.php" class="email_success_a">Go back to the admin page</a>
        <?php 
        } else {
            if (empty($_POST) === false) {
                if (empty($_POST['subject']) === true) {
                    $errors[] = 'A message subject is required.';
                }
                if (empty($_POST['body']) === true) {
                    $errors[] = 'A body message is required.';
                }
                if (empty($errors) === false) {
                    echo output_errors($errors);
                } else {
                    email_users($_POST['subject'], $_POST['body']);
                    header('Location: email_users.php?success');
                    exit();
                }
            }
// generate email form otherwise

知道为什么会出现此错误吗?

致命错误:无法重新声明 PHPMailerAutoload()

我还想指出,即使出现此错误,该功能仍然有效,并且正在发送电子邮件......

编辑:根据要求,请参阅下面使用 PHPMailer 的函数:

function email($user, $subject, $body) {
    require 'phpmailer/PHPMailerAutoload.php';
    $mail = new PHPMailer;

 /* $mail -> Host,username,password and other misc stuff
    $mail->Subject = $subject;
    $mail->Body    = $body;
    $mail->AltBody = $body; etc */
}

【问题讨论】:

  • 你实例化phpmailer类的代码在哪里?
  • 在尝试加载 PHPMailer 时,您很可能在多个地方使用 require 而不是 require_once。要么,要么你在多个地方声明了PHPMailerAutoload()
  • @zlen,请看上面,代码更新
  • @honerlawd 我曾尝试使用 require_once 而不是 require(在 email(); 函数中),但它会导致无限循环
  • header('Location: email_users.php?success'); 是同一页吗?如果是这样,那么您将重定向回同一页面...

标签: php phpmailer


【解决方案1】:

如果你使用

需要'phpmailer/PHPMailerAutoload.php';

在您的函数中,但您调用该函数 2 次,它将重新声明该类。只需改用require_once()

require_once('phpmailer/PHPMailerAutoload.php');

【讨论】:

    【解决方案2】:

    经过大量测试,我找到的解决方案是将标头重定向添加到函数中并将其从调用代码中删除:

    function email_users($subject, $body) {
        include('core/db/db_connection.php');
        $sql = "SELECT email, first_name FROM `_users` WHERE allow_email = 1";
        $query = mysqli_query($dbCon, $sql);
        while (($row = mysqli_fetch_assoc($query)) !== false) {
            $body = "Hello ". $row['first_name'] . ", <br><br>" . $body;
            email($row['email'], $subject, $body);
            header('Location: email_users.php?success');
        }
    }
    

    此外,正如 honerlawd 所指出的,需要 require_once 才能使其正常工作,否则它将仅向数据库中找到的第一个帐户发送电子邮件。如果不重定向到 email_users.php?success,这将导致无限循环,无论我调用 require_once 还是 require。

    这是正确的方法还是只是暂时的混乱修复?

    【讨论】:

      猜你喜欢
      • 2010-12-29
      • 2015-08-29
      • 2012-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多