Reputation: 1033
This is probably a very stupid question but I can't seem to find an answer anywhere. Can php code be used inside the body of a html email?
I have only every used variable names inside an email before i.e.
'<p>Dear'.$name.'</p>
But what if I want to make a table of data and use a loop? Can I put the php loop code in the email html? I have tried but it does not seem to like my syntax no matter what I try.
Just to be clear I have no problems sending the email.
Upvotes: 1
Views: 132
Reputation: 1792
PHP is a server-side language. You can use PHP on your server to create a HTML code for your e-mail. But you cannot include the PHP code in your email.
Example of creating a table with PHP for an email.
<?php
$my_data = Array(
Array("John", "32", "Male"),
Array("Casey", "28", "Male"),
Array("Peter", "43", "Male"),
Array("Michael", "19", "Male"),
Array("Samantha", "22", "Female")
);
$table = "<table>";
$table .= " <tr>";
$table .= " <td>Name</td>";
$table .= " <td>Age</td>";
$table .= " <td>Gender</td>";
$table .= " </tr>";
foreach($my_data AS $value)
{
$table .="<tr>";
$table .= " <td>".$value[0]."</td>";
$table .= " <td>".$value[1]."</td>";
$table .= " <td>".$value[2]."</td>";
$table .="</tr>";
}
$table .= "</table>";
mail("[email protected]", "Our users", $table);
?>
Upvotes: 1
Reputation: 13
You can use php and variables to generate a custom message, using a loop, but you should then send each email as plain text or html. If you insert some php code in the content sent, the receiver will see it as plain text, it won't be executed.
Upvotes: 0