PHP发送邮件功能。 如何使一个电子邮件地址接收一封包含数据的电子邮件,该数据基于表格中勾选复选框的数量

问题描述:

I am working on a send mail function of my system. I have a table that shows the data from my database. The send mail function of my system goes like this:

enter image description here

The user should fill up the form containing: Person in charge (email address), Comments and Status (Open or Closed) then tick the checkbox in the table. And all values in that row will be sent to the Person in charge. It is successfully working. What I want to do now is if I ticked 2 checkbox and I have the same person in charge, instead of receiving two emails, I want to make it one email, containing data according to number of ticked checkbox. For example I ticked 2 checkbox that will be send to emailadd@yahoo.com. How will I able to do it?:

UPDATE:

I will explain it more coz I think my question is too complex. What I want is if one email address is receiver of multiple issue, instead of receiving it one email per issue, those issues will be put together in one email. so the receiver will only read it in one email.

enter image description here

function InsertEmailMessage() {

    $explode_check=explode(',', $_POST['ticked']);
    //print_r($explode_check); 
    for($i=0;$i<count($explode_check);$i++)
    {
        $Selcheckb=$explode_check[$i];

        $sql6 = "SELECT * FROM invalid_invoice WHERE ID='".$Selcheckb."'";
        $conn = dbConnect();
        $stmt6 = $conn->prepare($sql6); 
        //$stmt6->bindParam(':id6', $_POST['idtxt'], PDO::PARAM_INT);
        $stmt6->execute();
        $data = $stmt6->fetchAll(PDO::FETCH_ASSOC);

        //var_dump($data);    

        foreach ($data as $row6) {

            $invnumb=$row6['Invoice_Number'];
            $partnumb=$row6['Part_Number'];
            $issue=$row6['Issues'];
            $pic=$row6['PIC_Comments'];
            $emailadd= $row6['PersoninCharge'];
            $issuetype=$row6['Issue_Type'];
            $createdate=$row6['Creation_Date'];
            $site=$row6['Site'];
            $vendor=$row6['Vendor_Name'];
            $invdate=$row6['Invoice_Date'];
            $po=$row6['PO'];
            $rr=$row6['RR']; 
            $currency=$row6['Currency'];
            $invamount=$row6['Invoice_Amount'];
            $stat=$row6['Status'];


            if($row6['Status']=="Open") {


                $message = "<html><b>Issue Type: {$issuetype} </b><br><br>";       
                $message .= "<b>Creation Date: {$createdate} </b><br><br>";
                $message .= "<b>Site: {$site} </b><br><br>";
                $message .= "<b>Vendor Name: {$vendor} </b><br><br>";
                $message .= "<b>Invoice Date: {$invdate} </b><br><br>";
                $message .= "<b>Invoice Number: {$invnumb} </b><br><br>";
                $message .= "<b>Part Number:</b><br>{$partnumb}<br><br>";
                $message .= "<b>PO: {$po} </b><br><br>";
                $message .= "<b>RR: {$rr} </b><br><br>";
                $message .= "<b>Currency: {$currency} </b><br><br>";
                $message .= "<b>Invoice Amount: {$invamount} </b><br><br>";
                $message .= "<b>Issues:</b><br>{$issue}<br>";
                $message .= "<b>Status: {$stat} </b><br><br>";  
                $message .= "<b>{$pic}<b><br>";  
                $message .= "</html>";

            }

            if(!empty($emailadd)) {
                dbInsertEmailMessage($emailadd, "Invoice Number: {$invnumb} - {$issue}.", $message);
                echo "<script language='javascript'>alert('Email sent to {$emailadd}.')</script>";
            }

        }
    }

    $conn=null;
}



function dbInsertEmailMessage($send_to, $subject, $message) {

    $sql7 = "INSERT INTO email_queue (send_to, subject, message) VALUES (:send_to, :subject, :message)";    
    $conn = dbConnect();
    $stmt7 = $conn->prepare($sql7); 
    $stmt7->bindParam(':send_to', $send_to);
    $stmt7->bindParam(':subject', $subject);
    $stmt7->bindParam(':message', $message);
    $stmt7->execute();
    $conn=null; 
}

You just need to move some stuff around. Instead of sending in the foreach loop just build up an array of $messages keyed with with email address. After all messages are built then loop the messages and send.

Also, your code is open to SQL Injection which allows anyone to run any query they want. You were on the right track with ->bindParam method of PDO.

Here is the code:

function InsertEmailMessage() {
    $explode_check=explode(',', $_POST['ticked']);
    //print_r($explode_check);

    $messages = array();

    for($i=0;$i<count($explode_check);$i++)
    {
        $Selcheckb=$explode_check[$i];

        $conn = dbConnect();
        $stmt6 = $conn->prepare("SELECT * FROM invalid_invoice WHERE ID=:cb");
        $stmt6->bindParam(':cb', $Selcheckb);
        $stmt6->execute();
        $data = $stmt6->fetchAll(PDO::FETCH_ASSOC);

        //var_dump($data);

        foreach ($data as $row6) {

            $invnumb=$row6['Invoice_Number'];
            $partnumb=$row6['Part_Number'];
            $issue=$row6['Issues'];
            $pic=$row6['PIC_Comments'];
            $emailadd= $row6['PersoninCharge'];
            $issuetype=$row6['Issue_Type'];
            $createdate=$row6['Creation_Date'];
            $site=$row6['Site'];
            $vendor=$row6['Vendor_Name'];
            $invdate=$row6['Invoice_Date'];
            $po=$row6['PO'];
            $rr=$row6['RR'];
            $currency=$row6['Currency'];
            $invamount=$row6['Invoice_Amount'];
            $stat=$row6['Status'];

            if($row6['Status']=="Open") {
                if(!isset($messages[$emailadd])) {
                  $messages[$emailadd] = '';
                }

                $messages[$emailadd] .= "<b>Issue Type: {$issuetype} </b><br><br>";
                $messages[$emailadd] .= "<b>Creation Date: {$createdate} </b><br><br>";
                $messages[$emailadd] .= "<b>Site: {$site} </b><br><br>";
                $messages[$emailadd] .= "<b>Vendor Name: {$vendor} </b><br><br>";
                $messages[$emailadd] .= "<b>Invoice Date: {$invdate} </b><br><br>";
                $messages[$emailadd] .= "<b>Invoice Number: {$invnumb} </b><br><br>";
                $messages[$emailadd] .= "<b>Part Number:</b><br>{$partnumb}<br><br>";
                $messages[$emailadd] .= "<b>PO: {$po} </b><br><br>";
                $messages[$emailadd] .= "<b>RR: {$rr} </b><br><br>";
                $messages[$emailadd] .= "<b>Currency: {$currency} </b><br><br>";
                $messages[$emailadd] .= "<b>Invoice Amount: {$invamount} </b><br><br>";
                $messages[$emailadd] .= "<b>Issues:</b><br>{$issue}<br>";
                $messages[$emailadd] .= "<b>Status: {$stat} </b><br><br>";
                $messages[$emailadd] .= "<b>{$pic}<b><br>";
                $messages[$emailadd] .= "<br><br>";
            }
        }
    }

    foreach($messages as $email=>$message) {
        dbInsertEmailMessage($email, "Invoice Number: {$invnumb} - {$issue}.", "<html>{$message}</html>");
        echo "<script language='javascript'>alert('Email sent to {$email}.')</script>";
    }

    $conn=null;
}

function dbInsertEmailMessage($send_to, $subject, $message) {
    $sql7 = "INSERT INTO email_queue (send_to, subject, message) VALUES (:send_to, :subject, :message)";
    $conn = dbConnect();
    $stmt7 = $conn->prepare($sql7);
    $stmt7->bindParam(':send_to', $send_to);
    $stmt7->bindParam(':subject', $subject);
    $stmt7->bindParam(':message', $message);
    $stmt7->execute();
    $conn=null;
}