Reputation: 1231
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.
Upvotes: 1
Views: 6157
Reputation: 3165
<?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);
?>
Upvotes: 0
Reputation: 21856
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 );
Upvotes: 0
Reputation: 87073
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
Upvotes: 1
Reputation: 39704
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);
Upvotes: 1