1

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.

17
  • 3
    Remember, PHP runs on the server, not the client. Here, the "client" is the email client. PHP is used to generate the HTML that is emailed to the client. If there is an issue, then it has nothing to do with the email client. Show us your loop/table code and the exact error you're getting, and we can help. Commented Apr 3, 2014 at 19:52
  • 1
    you run the php, to create the email(html) to be sent Commented Apr 3, 2014 at 19:54
  • 1
    @Drdavidpier: Forget about the email for a second. Just think about the PHP code. All it's doing is creating an HTML string and putting that in a variable. How are you doing that? Once we get that working, you can email it. Commented Apr 3, 2014 at 19:55
  • 1
    You basicaly want a form letter - e.g. you don't send out a Word file that contains the {firstname}, {lastname} etc... placeholders. Word REPLACES those placeholders with the recipient's actual data as part of a mail merge, then sends out the generated document. PHP does the same thing - you provided values for those variables on the server, generate the html with the values filled in, then send out the generated html. Commented Apr 3, 2014 at 19:56
  • 1
    the loop is not in the email, its in the php that creates the email Commented Apr 3, 2014 at 19:57

2 Answers 2

1

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);
?>
Sign up to request clarification or add additional context in comments.

1 Comment

This is the most complete answer to help others so I will mark it as the answer. Thanks
0

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.

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.