1

i have a php page that i used to get info from a data base , what i need is to send it in an email but for each result a new email and not all info i one same email. here is my mysql php scrtip

   <?php
include 'myDB.php'; 

 $sql = "this query is working perfectly ";
 $result = mysqli_query($conn, $sql);

//var_dump($result);
 if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
    echo "nombre del paquete: " . $row["nombre_del_paquete"]."
     <srtong>precio total:</strong> " . $row["precio_total"]. " 
    " . $row["nombre"]. "
    " . $row["apellido"]. "
    " . $row["email"]. "

    " . $row["submitted"]. "<br><br>";

        $header ="From: [email protected]" . "\r\n";
    $para    = '[email protected]';
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
    $titulo  = 'Newsletter';

    $message = '<html><body>';
    $message .= '<img src="https://world.bookinghello.com/PromoFB/img/hello_logo.png" alt="HELLO" />';
    $message .= '<br/> <p>El siguiente usuario abandono el la compra de un paquete en booking hello </p><br/>';
    $message .= '<table rules="all" style="border-color: #666;" cellpadding="2">';
    $message .= "<tr><td><strong>Nombre del paquete:</strong> </td><td>" . $row["nombre_del_paquete"]."</td></tr>";
    $message .= "<tr><td><strong>Nombre del paquete:</strong> </td><td>" . $row["precio_total"]."</td></tr>";
    $message .= "<tr><td><strong>Nombre del paquete:</strong> </td><td>" . $row["nombre"]."</td></tr>";
    $message .= "<tr><td><strong>Nombre del paquete:</strong> </td><td>" . $row["apellido"]."</td></tr>";
    $message .= "<tr><td><strong>Nombre del paquete:</strong> </td><td>" . $row["email"]."</td></tr>";

    $message .= "</table>";
    $message .= "</body></html>";



    if(mail($para, $titulo, $message, $header)){
     echo "successful";
                die();
    }else{
        echo "false";
    }
}
 } else {
echo "0 results";
}


?>

when i remove the mail option i get 5 results in echo, when i add the mail option i only get 1 result(first one) and email is sent as well with 1 info, will like to get 5 different emails

2
  • 1
    Construct and send an email within the while loop, so it sends one for every record in the data? Commented Jul 7, 2017 at 17:14
  • yes i need to construct the loop and send an eamail for each record Commented Jul 7, 2017 at 18:19

1 Answer 1

3

Create a method called sendMail and pass required value, which will be used when sending mail.

Then, call sendMail for each iteration of

while($row = mysqli_fetch_assoc($result)) {
    //your code
   // Call sendMail
   // some code
}
Sign up to request clarification or add additional context in comments.

7 Comments

can you please provide me a full explination with an example of passing required values and send email, i still basic with php and mysql, thanks in advance
I'm not sure what explanation you want, it is pretty straight forward. You need to create a method and pass value like to, from and body contents etc. Since, each iteration will have some result assuming in that iteration you will have to and from and body contents. So, same you need to pass through the method
i editted my question so you can see where i'm wrong, what is happening i only get one email witht the 1 result only and actually i have 5 results
if while loop iterating 5 times then you will receive 5 mail otherwise you won't
that is correct, i added foreach ($row as $test) { $message .= $test; } ///// but i keep getting 1 result only i will need 5 in this case since is 5 iteration, and yes if there is non it doesnt sent the email
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.