1

Hi I want to send loop data with php mail function here is my code:

foreach ($data['query'] as $row){
    echo $row->name;
    echo "<br>";
    echo $row->time;
    echo "<br>";
    echo $row->dosage;
    echo "<br>";
    echo $row->frequency;
    echo "<br>";
    echo $row->quantity;
    echo "<br>";
}

$to = "[email protected]";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: [email protected]";
mail($to,$subject,$txt,$headers);

Can anybody told me how I can attach foreach loop data to email message body, I mean instead of sending this text (Hello World!) in body I want to send loop data.

0

4 Answers 4

1

Concatenate the $txt:

$txt = "";
foreach ($data['query'] as $row){
    $txt .= $row->name;
    $txt .= "<br>";
    $txt .= $row->time;
    $txt .= "<br>";
    $txt .= $row->dosage;
    $txt .= "<br>";
    $txt .= $row->frequency;
    $txt .= "<br>";
    $txt .= $row->quantity;
    $txt .= "<br>";
}

$to = "[email protected]";
$subject = "My subject";
$headers = "From: [email protected]";
mail($to,$subject,$txt,$headers);
Sign up to request clarification or add additional context in comments.

2 Comments

Only one problem left that <br> is not working in sent mail can you help?
you have to set headers as HTML or add $txt .= "\n"; instead of <br>
1

Purely String Concatenation like below:

   $str = ''; 
   foreach ($data['query'] as $row){
      $str .= $row->name;
      $str .= "<br>";
      $str .= $row->time;
      $str .= "<br>";
      $str .= $row->dosage;
      $str .= "<br>";
      $str .= $row->frequency;
      $str .= "<br>";
      $str .= $row->quantity;
      $str .= "<br>";
    }

    $to = "[email protected]";
    $subject = "My subject";
    $headers = "From: [email protected]";
    mail($to,$subject,$str,$headers);

    $str = ''; // empty the $str again

Comments

0

I think this is what you are after:

    $to = "[email protected]";
    $subject = "My subject";
    $txt = "Hello world!";
    $headers = "From: [email protected]";

    foreach ( $data['query'] as $row ) {
      $txt = $row->name;
      $txt .= "<br>";
      $txt .= $row->time;
      $txt .= "<br>";
      $txt .= $row->dosage;
      $txt .= "<br>";
      $txt .= $row->frequency;
      $txt .= "<br>";
      $txt .= $row->quantity;
      $txt .= "<br>";

      mail( $to, $subject, $txt, $headers );
    }

This sends a mail for every loop. It is unclear from your question if you want that. You can als collect all data, and send it as 1 email:

$to = "[email protected]";
$subject = "My subject";
$txt = "";
$headers = "From: [email protected]";

foreach ( $data['query'] as $row ) {
  $txt .= $row->name;
  $txt .= "<br>";
  $txt .= $row->time;
  $txt .= "<br>";
  $txt .= $row->dosage;
  $txt .= "<br>";
  $txt .= $row->frequency;
  $txt .= "<br>";
  $txt .= $row->quantity;
  $txt .= "-- end of row<br>";

}

mail( $to, $subject, $txt, $headers );

Comments

0
<?php
$dataArray  =   array('name','time','dosage','frequency','quantity');
$txt        =   '';
foreach ($data['query'] as $row)
{
    foreach($dataArray as $key=>$data)
    {
       $txt .= ($row->$data.'</br>');
    }
}

$to = "[email protected]";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: [email protected]";
mail($to,$subject,$txt,$headers);

?>

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.