我开始实现通过电子邮件发送大众用户的功能,当我点击发布时,我收到以下错误。
无法在第31行的C:\ xampp \ htdocs \ bloodbank2 \ phpmailer \ PHPMailerAutoload.php中重新声明PHPMailerAutoload()(之前在C:\ xampp \ htdocs \ bloodbank2 \ phpmailer \ PHPMailerAutoload.php:24中声明)
然而,数据库中的第一个用户能够接收电子邮件,其余的不能。
这是我的代码 第一个是news.php,它允许人们发布新闻。
<?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'; ?>
第二个代码是功能 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是包含php邮件程序的电子邮件功能。
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';
}
}
那么我将如何摆脱错误,以便每个人都是数据库接收邮件?
答案 0 :(得分:11)
你应该只需要自动加载器,因为它也会加载php邮件程序类:
require_once('/phpmailer/PHPMailerAutoload.php');
// remove this as it will be loaded by the autoloader
// require '/phpmailer/class.phpmailer.php';
也将它移到php文件的顶部..不在函数
中