问题描述
我正在使用脚本将电子邮件发送给检查用户(批准),但是脚本只发送到第一个检查用户,这里是脚本:
I'm using a script to send email to checked users (approval), but script is sending only to first checked user, here's the script:
if (isset($_POST['approve'])) {
$ticked = $_POST['approve'];
foreach($ticked as $id) {
$SQL = "SELECT * FROM userinfo WHERE id='".intval($id)."'";
$result = mysql_query("set names 'utf8'");
$result = mysql_query($SQL);
if (mysql_num_rows($result) > 0) {
$db_field = mysql_fetch_array($result);
$fname = $db_field['first_name'];
$lname = $db_field['last_name'];
$email = $db_field['email'];
$fon = $db_field['telephone'];
$ad = $db_field['ad'];
$data = json_decode($ad);
$message = 'Approved message';
mail($mail, 'Your Subject', $message);
set_include_path(getcwd()."/../library/");
include_once("Google_Spreadsheet.php");
$u = "[email protected]";
$p = "sdgfdjkgfjg";
$ss = new Google_Spreadsheet($u, $p, "Registracija_nova", "Sheet1");
$row = array(
"id" = > "=ROW() - 2",
"name" = > $fname,
"last_name" = > $lname,
"email" = > $email,
"phone_number" = > $fon,
"address" = > $data - > address,
"prevozbg" = > $data - > prevozbg,
"skipass" = > $data - > skipass
//"json" => $ad
);
$ss - > addRow($row);
mysql_query("DELETE FROM userinfo WHERE `id` = '".intval($id)."'");
/*
$ImePrezimePos = "HP EG NPI - Registracija";
$EmailAdresaPos = "[email protected]";
$pfw_header = "From: ". $ImePrezimePos . " <" . $EmailAdresaPos . ">\r\n";
$pfw_header .= "MIME-Version: 1.0\r\n";
$pfw_header .= "Content-type: text/html; charset=utf-8\r\n";
$pfw_subject = "Potvrdni email";
$pfw_email_to = "$email";
$msg = "Poštovani/a $fname $lname Vaša registracija je odobrena od strane administratora.<br/> Vaš HP tim";
*/
//$msg = "Dear $fname $lname your registration was confirmed by admin.";
//mail($email, 'Confirmation', $msg);
//mail($pfw_email_to, $pfw_subject ,$msg ,$pfw_header ) ;
$pfw_header. = "MIME-Version: 1.0\r\n";
$pfw_header. = "Content-type: text/html; charset=utf-8\r\n";
$fromName = "HP EG NPI - Registracija";
$fromEmail = "[email protected]";
$toEmail = "$email";
$subject = "Prijava prihvacena";
function sendEmail($fromName, $fromEmail, $toEmail, $subject, $emailBody, $pfw_header) {
$mail = new PHPMailer();
$mail - > FromName = $fromName;
$mail - > From = $fromEmail;
$mail - > AddAddress("$toEmail");
$mail - > Subject = $subject;
$mail - > Body = $emailBody;
$mail - > isHTML(true);
$mail - > WordWrap = 150;
if (!$mail - > Send()) {
return false;
} else {
return true;
}
}
function readTemplateFile($FileName) {
$fp = fopen($FileName, "r") or exit("Unable to open File ".$FileName);
$str = "";
while (!feof($fp)) {
$str. = fread($fp, 1024);
}
return $str;
}
//Data to be sent (Ideally fetched from Database)
$name = "$first_name";
$lastname = "$last_name";
$UserEmail = "$email";
//Send email to user containing username and password
//Read Template File
$emailBody = readTemplateFile("../html-email/mail2.html");
//Replace all the variables in template file
$emailBody = str_replace("#username#", $fnames, $emailBody);
$emailBody = str_replace("#password#", $lnames, $emailBody);
//Send email
$emailStatus = sendEmail($fromName, $fromEmail, $UserEmail, $subject, $emailBody, $headers);
}
}
}
和错误:
致命错误:无法重发邮件(/ home / ^在线82上的public_html / ^^ / admin / approve.php
Fatal error: Cannot redeclare sendEmail() (previously declared in /home/^^/public_html/^^/admin/approve.php:82) in /home/^^/public_html/^^/admin/approve.php on line 82
从上一个调用函数的文件:
And from previous file that's calling function:
echo "<input name=\"approve[]\" type=\"checkbox\" value='".$row["id"]."' >";
我可以做什么来发送给我检查的每封电子邮件?提前提醒大家!
What can i do to make it send to each email i have checked? THanks in advance everybody!
推荐答案
将 sendEmail()
foreach
循环。因为它在循环内,所以你得到错误无法重新发送sendEmail()
Move the sendEmail()
function outside the foreach
loop. Since it is inside the loop so you got the error Cannot redeclare sendEmail()
这篇关于PHP发送电子邮件foreach - 发送到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!