用于发送电子邮件的PHP / MySQL脚本 - 一封邮件太多

时间:2018-04-27 15:23:25

标签: php mysql mailer

我制作了PHP脚本,将MySQL数据库的报告发送给用户的电子邮件。每个用户必须只接收他们自己的数据(带有他们的ID)。 脚本tabela.php使html表与用户内容。

<?php

//select data
$sql = "SELECT oports.id, oports.handlowiec, oports.data_rozp, oports.data_przed, oports.nazwa, oports.city, oports.nip, oports.inic, db_users.email FROM oports, db_users WHERE db_users.id = oports.user_id and db_users.id = '{$sqlid}'";

//execute query
$wynik = $polaczenie->query($sql);

//make table schema
echo "<p style=\"font-size:14px;\">There is your report:<br></p>";
echo "<p>";
echo "<table boder=\"1\"><tr>";
echo "<td bgcolor=\"#f4df8b\"><strong>ID</strong></td>";
echo "<td bgcolor=\"#f9d74d\"><strong>name</strong></td>";
echo "<td bgcolor=\"#f4df8b\"><strong>Date started</strong></td>";
echo "<td bgcolor=\"#f9d74d\"><strong>Date deadline</strong></td>";
echo "<td bgcolor=\"#f4df8b\"><strong>Company name</strong></td>";
echo "<td bgcolor=\"#f9d74d\"><strong>City</strong></td>";
echo "<td bgcolor=\"#f4df8b\"><strong>NIP</strong></td>";
echo "<td bgcolor=\"#f9d74d\"><strong>Initials</strong></td>";
echo "</tr>";

//loop for show data in table
 while ( $row = mysqli_fetch_row($wynik) ) {
    echo "</tr>";
    echo "<td bgcolor=\"#f7e8ab\">" . $row[0] . "</td>";
    echo "<td bgcolor=\"#fbe383\">" . $row[1] . "</td>";
    echo "<td bgcolor=\"#f7e8ab\">" . $row[2] . "</td>";
    echo "<td bgcolor=\"#fbe383\">" . $row[3] . "</td>";
    echo "<td bgcolor=\"#f7e8ab\">" . $row[4] . "</td>";
    echo "<td bgcolor=\"#fbe383\">" . $row[5] . "</td>";
    echo "<td bgcolor=\"#f7e8ab\">" . $row[6] . "</td>";
    echo "<td bgcolor=\"#fbe383\">" . $row[7] . "</td>";
    echo "</tr>";
 }
 echo "</table>";
 echo "<br>";
 echo "<p style=\"font-size:10px;\">Jest to e-mail wygenerowany z systemu CRM. Prosimy na niego nie odpowiadać</p>";

 ?>

脚本sender.php将数据发送给用户:

<?php

include 'connect.php';

//connect with database
$polaczenie = @new mysqli($host, $db_user, $db_password, $db_name);

//set charset to show polish letters
$polaczenie->set_charset("utf8");

//check connection
if ($polaczenie->connect_errno!=0)
    {
        echo "Error: ".$polaczenie->connect_errno." Opis: ". $polaczenie->connect_error;
    }
    else 
    {
        //define id variable
        $sqlid = 1;

        //select emails for user with id = sqlid
        $zap = "SELECT email from db_users where id = '{$sqlid}'";    

        //make query (for while loop)
        $zapt = $polaczenie->query($zap);

            //while there are some data, make instructions in loop
            while (($zapt -> fetch_assoc()) !== null)
            {
                    //there are results
                    //execute query again (without this loop do not work properly)
                    $zap = "SELECT email from db_users where id = '{$sqlid}'";
                    //show email and save to variable rowxx
                    $zapx = mysqli_query($polaczenie,$zap);
                    while ($rowx = mysqli_fetch_assoc($zapx)) {
                        print_r ($rowx);
                        $rowxx = $rowx["email"];
                    }
                    //include content of tabela.php
                    ob_start();
                    include "tabela.php";
                    $content = ob_get_clean();

                    //define mail headers, subject and message
                    $od  = "From: itest@mail.pl \r\n";
                    $od .= 'MIME-Version: 1.0'."\r\n";
                    $od .= 'Content-type: text/html; charset=utf-8'."\r\n"; 
                    $to = $rowxx;
                    $subject = "Raport szans";
                    $message = $content;

                    if(mail($to, $subject, $message, $od)) 
                    {
                        echo "Mail sent!";
                    } 
                    else 
                    {
                        echo "Error with sending!";
                    }

                    $sqlid++;
                    $zapt = $polaczenie->query($zap);

            }
                //else

                echo 'No results';


        $polaczenie->close(); 
        }

?>

脚本运行正常,但是对于具有最后一个ID的用户,发送的邮件太多了。如果有4个用户,则最后收到两个邮件而不是一个 - 首先是正确数据,第二个是没有数据(空表)。 sender.php脚本的输出是:

Array ( [email] => ika1@mail.pl ) Mail sent!Array ( [email] => pb1@mail.pl ) Mail sent!Array ( [email] => rr1@mail.pl ) Mail sent!Array ( [email] => pr1@mail.pl ) Mail sent!Mail sent!No results

所以我看到最后“发送邮件”没有电子邮件地址,但是我在pr1@mail.pl上收到了。为什么呢?

1 个答案:

答案 0 :(得分:0)

所以我在我的代码中进行了更改(简化时,删除嵌套的while并在while循环中更改查询的顺序,这是发送一封邮件太多的原因),现在它工作得很好,但我还是不知道如何进行SQL注入,因为没有POST或GET。

<?php

include 'connect.php';

//connect with database
$polaczenie = @new mysqli($host, $db_user, $db_password, $db_name);

//set charset to show polish letters
$polaczenie->set_charset("utf8");

//check connection
if ($polaczenie->connect_errno!=0)
    {
        echo "Error: ".$polaczenie->connect_errno." Opis: ". $polaczenie->connect_error;
    }
    else 
    {
        //define id variable
        $sqlid = 1;

        //select emails for user with id = sqlid
        $zap = "SELECT email from db_users where id = '{$sqlid}'";    

        //make query (for while loop)
        $zapt = $polaczenie->query($zap);

            //while there are some data, make instructions in loop
            while ($rowx = $zapt -> fetch_assoc())
            {
                    //show e-mail recipient (for debug only)
                    print_r ($rowx);
                    $rowxx = $rowx["email"];

                    //include content of tabela.php
                    ob_start();
                    include "tabela.php";
                    $content = ob_get_clean();

                    //define mail headers, subject and message
                    $od  = "From: itest@mail.pl \r\n";
                    $od .= 'MIME-Version: 1.0'."\r\n";
                    $od .= 'Content-type: text/html; charset=utf-8'."\r\n"; 
                    $to = $rowxx;
                    $subject = "Raport szans";
                    $message = $content;

                    if(mail($to, $subject, $message, $od)) 
                    {
                        echo "Mail sent!";
                    } 
                    else 
                    {
                        echo "Error with sending!";
                    }

                    //increment sqlid
                    $sqlid++;

                    //execute query again
                    $zap = "SELECT email from db_users where id = '{$sqlid}'";
                    $zapt = $polaczenie->query($zap);

            }
                //else

                echo 'No results';


        $polaczenie->close(); 
        }

?>