Cannot redeclare PHPMailerAutoload()?

坚强是说给别人听的谎言 提交于 2020-01-11 12:19:58

问题


I start implementing a function of Emailing mass users, When I click on post the I get the following error.

Cannot redeclare PHPMailerAutoload() (previously declared in C:\xampp\htdocs\bloodbank2\phpmailer\PHPMailerAutoload.php:24) in C:\xampp\htdocs\bloodbank2\phpmailer\PHPMailerAutoload.php on line 31

However the first user in the database is able to receive the Email and the rest dont.

Here are my codes.
1st one is news.php, which allows person to post the news.

          <?php include 'core/init.php'; ?>
          <?php include 'includes/overall/oheader.php'; 

          if (empty($_POST) === false){

           $require_fields = array('heading', 'information');

           foreach($_POST as $key=>$value){
            if(empty($value) && in_array($key, $require_fields) === true){
                    $errors[] = 'Fields marked with an asterisk are  required';
                    break 1;

            }

            }
            }

            if(isset($_GET['success']) && empty($_GET['success'])){

            echo 'information posted';

            }else{


            if(empty($_POST) === false && empty($errors) === true){
            $insert_data = array(
                'heading'       => $_POST['heading'],
                'information'       => $_POST['information']


            );

            //
            //redirect

                    mail_users($_POST['heading'], $_POST['information']);   
                    header('Location: news.php?success');
                    news_data($insert_data);
                    //exit
                    exit();



        }


        ?>

        <h1>News</h1>
        <p>Just a Template</p>  




           <form action="" method="post">
            <ul>
                <li>
                    Heading*:<br>
                    <textarea rows="2" cols="40"  name="heading"maxlength="50"></textarea>

                </li>

                <li>


                    information*:<br>
                    <textarea rows="10" cols="40"  name="information"> </textarea>

                </li>

                <li>


                    <input type="submit" value="Post" name="save">

                </li>

            <ul>


        <?php 
        }

        include 'includes/overall/ofooter.php'; ?>

2nd code is the functions function mail_user();

         function mail_users($subject, $body){

          $query = mysql_query("SELECT `email`, `first_name` FROM `users`");

          while(($row = mysql_fetch_assoc($query)) !== false){

           $body = "Hello" . $row['first_name'] . ",\n\n" . $body;
           email($row['email'], $subject, $body);


          }

         }

3rd is the email function that a contains the php mailer.

             function email($to, $subject, $body){

              require '/phpmailer/PHPMailerAutoload.php';
              require '/phpmailer/class.phpmailer.php';

              error_reporting(-1);
              $mail = new PHPMailer(true);

              $mail->SMTPDebug = 0;                               // Enable verbose debug output

              $mail->isSMTP(true);                                      // Set mailer to use SMTP
              $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
              $mail->SMTPAuth = true;                               //             Enable SMTP authentication
              $mail->Username = '';                 // SMTP username
              $mail->Password = '';                           // SMTP password
              $mail->SMTPSecure = 'tls';                            //         Enable TLS encryption, `ssl` also accepted
              $mail->Port = 587;                                    // TCP port to connect to

              $mail->setFrom('');
              $mail->addAddress(''.$to.'');     // Add a recipient
              $mail->isHTML(true);                                  // Set Email format to HTML

              $mail->Subject = ''.$subject.'';
                $mail->Body    = ''.$body.'';
               $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

                if(!$mail->send()) {
                 echo 'Message could not be sent.';
                  echo 'Mailer Error: ' . $mail->ErrorInfo;
                  } else {
                echo 'Message has been sent';
                 }

               }

So how will I get rid of the error so that everyone is database receives the mail?


回答1:


You should require only the autoloader as it will load the php mailer class as well:

require_once('/phpmailer/PHPMailerAutoload.php');
// remove this as it will be loaded by the autoloader
// require '/phpmailer/class.phpmailer.php';

Also move it in the top of the php file.. not in the function



来源:https://stackoverflow.com/questions/37294048/cannot-redeclare-phpmailerautoload

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!