VanityCrush Posted August 7, 2015 Share Posted August 7, 2015 (edited) Hiya, I am using PHPMailer to send emails from my local host. I have written a function which is supposed to send emails to registered users who have chosen the option to receive them. (I.e. newsletter subscription, etc) 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); } } The code that is calling the function: 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 form otherwise Any idea why I'm getting Fatal error: Cannot redeclare PHPMailerAutoload() ? I would also like to point out that even with this error, the function still works and the emails are being sent... Edited August 7, 2015 by VanityCrush Quote Link to comment https://forums.phpfreaks.com/topic/297682-fatal-error-cannot-redeclare-phpmailerautoload/ Share on other sites More sharing options...
requinix Posted August 8, 2015 Share Posted August 8, 2015 "Fatal error" means your script is crashing. That is not good. The fact that it crashes after sending the email is fortunate but still not acceptable. Whatever file that contains the PHPMailerAutoload function is being included twice. That's a problem because you're telling PHP that you want to define this function twice. Which is bad. Use include_once() or require_once() instead (the latter is better for a dependency) to tell PHP that those files should only be included once. Do that everywhere you include files which define classes or functions or autoloaders. Quote Link to comment https://forums.phpfreaks.com/topic/297682-fatal-error-cannot-redeclare-phpmailerautoload/#findComment-1518243 Share on other sites More sharing options...
VanityCrush Posted August 8, 2015 Author Share Posted August 8, 2015 After much testing, the solution I have found is adding the header redirect into the function and removing it from the calling code: 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'); } } Your suggestion is correct, requinix. require_once is needed in order for this to work, otherwise it will send an email only to the first account found in the database. Without redirecting to the email_users.php?success, this will cause an infinite loop, no matter if I call require_once or require. Would this be the correct approach or is it just a temporary messy fix? Quote Link to comment https://forums.phpfreaks.com/topic/297682-fatal-error-cannot-redeclare-phpmailerautoload/#findComment-1518264 Share on other sites More sharing options...
requinix Posted August 8, 2015 Share Posted August 8, 2015 I'm a little confused now. You agree that require_once() is the best solution, but are doing something else instead? Calling header() will not stop your script. It'll keep executing. So if all you did was add that line then the error would keep happening. 1. email_users() should be only about emailing users. It shouldn't have to do anything with a redirect. 2. The redirect should be in the calling code after all, because it's the calling code that knows that it was a web request in the first place and that the user should be sent to that page when successful. 3. Changing all your include/require()s that are for files which define classes or functions to use include_once/require_once() instead will solve your problem. Though you should still look into why the file was included twice as that could (could) be a symptom of another problem. Quote Link to comment https://forums.phpfreaks.com/topic/297682-fatal-error-cannot-redeclare-phpmailerautoload/#findComment-1518271 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.