php - 使用从数据库中选择的不同数据发送组邮件

标签 php mysql email

我在使用下面的代码发送群组邮件时遇到了一些问题。错误是 fatal error :

Cannot redeclare clean_string() (previously declared in /home/content/83/11173683/html/AA/sendcanteen.php:74)

<?php
$d = $_REQUEST['date']; //requesting date from canteendetails.php

include("dboperation.php");
$obj    = new dboperation();
$n      = array();
$e      = array();
$str    = "select * from employee2";
$result = $obj->selectquery($str);
while ($r = mysql_fetch_assoc($result)) {
    $n[] = $r['emp_name'];
    $e[] = $r['email'];
}
for ($i = 0; $i < sizeof($n); $i++) {

    $str1     = "select * from modifycanteen where name='$n[$i]' and date='$d'";
    $result1  = $obj->selectquery($str1);
    $r        = mysql_fetch_assoc($result1);
    $email_to = $r['email'];
    $coffee   = $r['coffee'];
    $tea      = $r['tea'];
    $remarks  = $r['remarks'];

    $email_subject = "CANTEEN DETAILS";

    $email_from = "Catering Services";

    $body    = '
    <html>
    <head>
    <style>

    body, P.msoNormal, LI.msoNormal
    {
    background-position: top;
    background-color: #336699;
    margin-left:  10em;
    margin-top: 1em;
    font-family: "verdana";
    font-size:   10pt;
    font-weight:bold ;
    color:    "000000";
    }

    </style>
    </head>
    </body> 
    ';
    $message = "Mr/Mrs " . $n[$i] . "\nYour Canteen details";
    $c       = "No: of Coffee :" . $coffee . "\n ";
    $t       = "No: of Tea :" . $tea . "\n ";
    $rem     = "Remarks :" . $remarks . "\n ";

    $bodys .= "$message<br>";
    $bodys .= "$c<br>";
    $bodyss .= "$t<br>";
    $bodysss .= "$rem";

    $body = $body . $bodys . $bodyss . $bodysss;

    function clean_string($string)
    {
        $bad = array(
            "content-type",
            "bcc:",
            "to:",
            "cc:",
            "href"
        );
        return str_replace($bad, "", $string);
    }
    $email_message .= "Message: " . clean_string($message) . "\n";    
    $headers = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html;charset=iso-8859-1' . "\r\n";
    $headers .= 'From: Accounttoall.com <http://www.redboat.in/>' . "\r\n";

    @mail($email_to, $email_subject, $body, $headers);
}
if (@mail) {
    header("location:canteendetails.php");
} else {
    echo "MAIL IS NOT SENT";    
}
?>

最佳答案

一个函数只能定义一次

为什么要在 for 循环中定义函数clean string

您必须在 for 循环之外定义函数

试试这个

<?php
function clean_string($string)
    {
        $bad = array(
            "content-type",
            "bcc:",
            "to:",
            "cc:",
            "href"
        );
        return str_replace($bad, "", $string);
    }

$d = $_REQUEST['date']; //requesting date from canteendetails.php

include("dboperation.php");
$obj    = new dboperation();
$n      = array();
$e      = array();
$str    = "select * from employee2";
$result = $obj->selectquery($str);
while ($r = mysql_fetch_assoc($result)) {
    $n[] = $r['emp_name'];
    $e[] = $r['email'];
}
for ($i = 0; $i < sizeof($n); $i++) {

    $str1     = "select * from modifycanteen where name='$n[$i]' and date='$d'";
    $result1  = $obj->selectquery($str1);
    $r        = mysql_fetch_assoc($result1);
    $email_to = $r['email'];
    $coffee   = $r['coffee'];
    $tea      = $r['tea'];
    $remarks  = $r['remarks'];

    $email_subject = "CANTEEN DETAILS";

    $email_from = "Catering Services";

    $body    = '
    <html>
    <head>
    <style>

    body, P.msoNormal, LI.msoNormal
    {
    background-position: top;
    background-color: #336699;
    margin-left:  10em;
    margin-top: 1em;
    font-family: "verdana";
    font-size:   10pt;
    font-weight:bold ;
    color:    "000000";
    }

    </style>
    </head>
    </body> 
    ';
    $message = "Mr/Mrs " . $n[$i] . "\nYour Canteen details";
    $c       = "No: of Coffee :" . $coffee . "\n ";
    $t       = "No: of Tea :" . $tea . "\n ";
    $rem     = "Remarks :" . $remarks . "\n ";

    $bodys .= "$message<br>";
    $bodys .= "$c<br>";
    $bodyss .= "$t<br>";
    $bodysss .= "$rem";

    $body = $body . $bodys . $bodyss . $bodysss;


    $email_message .= "Message: " . clean_string($message) . "\n";    
    $headers = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html;charset=iso-8859-1' . "\r\n";
    $headers .= 'From: Accounttoall.com <http://www.redboat.in/>' . "\r\n";

    @mail($email_to, $email_subject, $body, $headers);
}
if (@mail) {
    header("location:canteendetails.php");
} else {
    echo "MAIL IS NOT SENT";    
}
?>

关于php - 使用从数据库中选择的不同数据发送组邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29911031/

相关文章:

php - Yii framework 2.0 一页多分页

php - 无法在 Laravel 迁移中创建外键

提交表单后 PHP 重定向到另一个 URL

mysql - CakePHP-查询HABTM表然后访问数据

python - 这段代码有什么问题?它不断写入相同的值

ios - 在SKPSMTPMessage中附加视频

javascript - 我可以用图像像素调用多个跟踪像素吗?

php - 已弃用:指令 'allow_url_include' 在第 0 行的 Unknown 中已弃用

node.js - 如何在nodejs邮件程序emailJS中设置回复?

html - 如何禁用先前定义的 CSS 类中定义的颜色