vivek
vivek

Reputation: 3

how to email all data into a single email?

I am Currently mailing the data from mysql table in php using the below code, it works perfectly,but the problem is it sends each row of data as a single mail, i want to send all row of data in one mail, how to merge all rows of data and send it as a single mail? please help me!!

<?php
include('header.php');
session_start();
?>
<html>
<head>
<title>Sending email using PHP</title>
</head>
<body>
<?php
  $sql="SELECT * FROM products";
  $result = mysql_query($sql);
 while($row = mysql_fetch_array($result)){    
   $to = "[email protected]";
   $subject = "d2dn-viewcart";
    $id_s = $row["id"];  
    $name_s = $row["description"];
    $price_s = $row["price"] ;
    $message = $id_s . " " . $name_s . " " . $price_s." ";
   $header = "From:d2dn";
   $retval = mail ($to,$subject,$message,$header);
}
   if( $retval == true )  
   {
      echo "Message sent successfully...";
   }
   else
   {
      echo "Message could not be sent...";
   }
?>
</body>
</html>
<?php
include('footer.php'); ?>

Upvotes: 0

Views: 52

Answers (3)

Golden
Golden

Reputation: 1

<?php
include('header.php');
session_start();
?>
    <html>
    <head>
        <title>Sending email using PHP</title>
    </head>
    <body>
    <?php
    $sql="SELECT * FROM products";
    $result = mysql_query($sql);
    $to = "[email protected]";
    $subject = "d2dn-viewcart";
    while($row = mysql_fetch_array($result)){
        $id_s = $row["id"];
        $name_s = $row["description"];
        $price_s = $row["price"] ;
        $message .= $id_s . " " . $name_s . " " . $price_s." \r\n";
    }
    $header = "From:d2dn";
    $retval = mail ($to,$subject,$message,$header);

    if( $retval == true )
    {
        echo "Message sent successfully...";
    }
    else
    {
        echo "Message could not be sent...";
    }
    ?>
    </body>
    </html>
<?php
include('footer.php'); ?>

This should work

Upvotes: 0

Rakesh Sharma
Rakesh Sharma

Reputation: 13728

define first outside loop $message = '';

then you need to append string like

$message .= $id_s . " " . $name_s . " " . $price_s." ";

and then keep your mail() outside loop

$retval = mail ($to,$subject,$message,$header);

Upvotes: 0

machineaddict
machineaddict

Reputation: 3236

You simply concatenate the content and send the email once:

<?php
include('header.php');
session_start();
?>
<html>
<head>
<title>Sending email using PHP</title>
</head>
<body>
<?php
    $sql="SELECT * FROM products";
    $result = mysql_query($sql);
    $to = "[email protected]";
    $subject = "d2dn-viewcart";
    $header = "From:d2dn";
    $message = '';
    while($row = mysql_fetch_array($result)){    

        $id_s = $row["id"];  
        $name_s = $row["description"];
        $price_s = $row["price"] ;
        $message .= $id_s . " " . $name_s . " " . $price_s." ";

    }
    $retval = mail ($to,$subject,$message,$header);
    if( $retval == true )  
    {
    echo "Message sent successfully...";
    }
    else
    {
    echo "Message could not be sent...";
    }
?>
</body>
</html>
<?php
include('footer.php'); ?>

Upvotes: 1

Related Questions