Php mail - A message variable that contain array and conditions [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Php mail - A message variable that contain array and conditions [duplicate]



This question already has an answer here:



I've been trying to send an e-mail that containt html code generated by php.



The context is, I'm using php to generate a webpage, and I'm trying to send this webpage by e-mail by copying the code I used to generate the webpage, and putting in a message using mail function.



I found a way to generate an e-mail that contains php variables like the following example :


[...]
$message = "<address>
<strong>Adresse de livraison:</strong><br>
$prenom $nom<br>
$adress1<br>
$adress2<br>
$postalcode $ville, $country
</address>";
mail($destinataire, $objet, $message, $headers);
[...]



Everything was perfect and the e-mail was send correctly.



But now my code contain php parts like the following example :


if ($disque!= 'No')
<tr>
<td>Disque avant : $disque</td>
<td class='text-center'>

$selected_product = $disqueav;
$data = bdd_select('SELECT Price FROM products WHERE Nom = ?', $selected_product);
echo $data['0']['Price'] . '€';
$subtotalprice = $data['0']['Price'] + $subtotalprice;

</td>
<td class='text-center'>1</td>
<td class='text-right'> $data['0']['Price'] . '€';</td>
</tr>



And it doesn't work because of the array like $data['0'], even if I'm using single comma for it, and double comma for the $message variable.



I get this error :


Parse error: syntax error, unexpected ']', expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)



My question is, is there a way to write this part of code so I don't get any error and I can send the page exactly the way I saw it.



Thank you very much.



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





You can't mix PHP and HTML like that. You have to close your PHP before outputting HTML and then open PHP again to use more PHP code.
– John Conde
Aug 12 at 15:42





I found out that <? $message = "Hello $name"?> was the easiest way.
– Jonathan199
Aug 12 at 16:02




2 Answers
2



Try using $data[0]['price'];
If it still throws error,Please tail Apache logs in comment.





Thank you ! It helps, but it still have problems with the $selected_product = $disqueav. The error for this line is Parse error: syntax error, unexpected ']', expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)
– Jonathan199
Aug 12 at 14:54





Please comment the piece of code where you have defined the variable $disqieav.
– Anshu Kumar
Aug 12 at 15:01





This a variable extract from $_post. But I get the problem is the way $selected_product is writed.
– Jonathan199
Aug 12 at 15:03





extract($_POST);
– Jonathan199
Aug 12 at 15:03





Ok. Instead of using $selected_product = $disqueav; try using array_push($selected_product,$disqueav);
– Anshu Kumar
Aug 12 at 15:04




Just make a message variable embed your message strings to that variable. It also had a problem with $data['0']; Because 0 is integer index not a string so it should be $data[0] not $data['0']. I think this will work for you. I couldn't test properly because I don't have the database.


$message = "<table><tbody>";
if ($disque!= 'No')
$message.="<tr>
<td>Disque avant :". $disque."</td>
<td class='text-center'>";

$selected_product = $disqueav;
$data = bdd_select('SELECT Price FROM products WHERE Nom = ?', $selected_product);
$message .= $data[0]['Price'] . "€";
$subtotalprice = $data[0]['Price'] + $subtotalprice;
$message .= $subtotalprice;
$message .= "</td>
<td class='text-center'>1</td>";
$message .= "<td class='text-right'> ".$data[0]['Price'] . "€</td>
</tr>";



$message .="</tbody></table>";

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS