【发布时间】: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');是同一页吗?如果是这样,那么您将重定向回同一页面...