0

Hi Im using PHP mail to send messages from a DB> However with the code below I get multiple emails from a single query. Hence if a query has 10 results, I get 10 emails. How can sort this out. Thanks

<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "test";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT stimee, pango,kembo FROM layla";

$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
   // output data of each row
   while($row = mysqli_fetch_assoc($result)) {



    $msg = "Time: " . $row["stimee"]. " - pango: " . $row["pango"]. " " . $row["kembo"] . "<br>";


    $msg = wordwrap($msg,70);
    $headers =  'MIME-Version: 1.0' . "\r\n"; 
    $headers .= 'From: Benge Man <[email protected]>' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 


    mail("[email protected]","Notification",$msg,$headers);

  }
  } else {
   echo "0 results";
  }

 mysqli_close($conn);
 ?> 
2
  • Move the mail outside of the while loop. Move the $headers outside the while as well and then have the $msg concatenate. Commented Jul 12, 2015 at 0:26
  • mail command is inside your loop. you need to accumulate your maildata in an array in the while loop and then use mail outside the loop Commented Jul 12, 2015 at 0:26

2 Answers 2

3

Construct the message inside the loop, but send the mail afterwards. That way, you get one mail with all the data in it.

$msg = "";
if (mysqli_num_rows($result) > 0) {

  // append data of each row to $msg.
  while($row = mysqli_fetch_assoc($result)) {
    $msg .= "Time: " . $row["stimee"]. " - pango: " . $row["pango"]. " " . $row["kembo"] . "<br>";
  }

  // After all the rows are fetched, send the message.
  $msg = wordwrap($msg,70);
  $headers =  'MIME-Version: 1.0' . "\r\n"; 
  $headers .= 'From: Benge Man <[email protected]>' . "\r\n";
  $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

  mail("[email protected]","Notification",$msg,$headers);
} else {
  echo "0 results";
}
Sign up to request clarification or add additional context in comments.

Comments

0

You must send the mail outside of your loop like this:

<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "test";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT stimee, pango,kembo FROM layla";

$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
   // output data of each row
  while($row = mysqli_fetch_assoc($result)) {



$msg = "Time: " . $row["stimee"]. " - pango: " . $row["pango"]. " " . $row["kembo"] . "<br>";


$msg = wordwrap($msg,70);
$headers =  'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'From: Benge Man <[email protected]>' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 




  }
mail("[email protected]","Notification",$msg,$headers);
  } else {
   echo "0 results";
  }

 mysqli_close($conn);
 ?> 

Comments

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.