PHP发送电子邮件foreach - 发送到

时间:2014-01-10 13:11:11

标签: php email

我正在使用脚本向已检查用户发送电子邮件(审批),但脚本只发送给第一个已检查用户,这是脚本:

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 = "hjgfhjgfh@gmail.com";
            $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 = "hp_eg_npi@eventregistration.rs";
            $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 = "hp_eg_npi@eventregistration.rs";
            $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 / ^^ / public_html / ^^ / admin / approve中重新声明sendEmail()(之前在/home/^^/public_html/^^/admin/approve.php:82中声明)。第82行的PHP

从以前调用函数的文件:

echo "<input name=\"approve[]\" type=\"checkbox\" value='".$row["id"]."' >";

如何将其发送到我检查过的每封电子邮件?大家提前谢谢!

2 个答案:

答案 0 :(得分:1)

您正在循环中定义sendEmail()函数。该函数在循环的第一次迭代中定义,在第二次迭代中抛出错误(因为函数已存在)并且脚本停止。将函数定义移出循环。

您应该将所有函数定义移出循环,函数只能定义一次。

答案 1 :(得分:1)

sendEmail()函数移到foreach循环之外。因为它在循环内部所以你得到错误无法重新声明sendEmail()