1

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.

Max
  • 135
  • 1
  • 12
  • 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 '18 at 15:42
  • I found out that $message = "Hello $name"?> was the easiest way. – Max Aug 12 '18 at 16:02

2 Answers2

1

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

Anshu Kumar
  • 799
  • 1
  • 7
  • 26
  • 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) – Max Aug 12 '18 at 14:54
  • Please comment the piece of code where you have defined the variable $disqieav. – Anshu Kumar Aug 12 '18 at 15:01
  • This a variable extract from $_post. But I get the problem is the way $selected_product[] is writed. – Max Aug 12 '18 at 15:03
  • extract($_POST); – Max Aug 12 '18 at 15:03
  • Ok. Instead of using $selected_product[] = $disqueav; try using array_push($selected_product,$disqueav); – Anshu Kumar Aug 12 '18 at 15:04
  • Thanks, It seems to work, I don't have this error anymore. Now I'm only have some undefinded variables : subtotalprice, data, selected_product. I'm gonna make some test. – Max Aug 12 '18 at 15:18
  • Your Welcome, If you need further help feel free to comment. If your probem is solved, mark the solution solved. Thanks. – Anshu Kumar Aug 12 '18 at 15:21
1

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>";
root
  • 498
  • 4
  • 20