kmunky kmunky - 6 months ago 9
HTML Question

PHP html email, using html template

so i'm trying to do a do a html mail sytem and my html i want to be a template, stored in a separate file like :

<div clas="headr"></div>
<div class="content"></div>
<div class="footer"></div>


when i want to send the mail i want my mail content(from the input form) to go in that div.content and then to send the whole html(template + submitted text).
what is the best way to do that? i'm thinking to something like:


  1. import the template into my php that sends the mail

  2. find the div with a "content" class and add the submitted text into it

  3. send mail



but i don't know how to "find" that div and write the submitted text into it.

Answer

As Pekka was saying, you could simply use str_replace to insert data in your template. Just add a placeholder:

<div clas="header"></div>
<div class="content">{{content}}</div>
<div class="footer"></div>

Then replace the placeholder with your content:

$content = 'Whatever you want to insert...';
$tpl = file_get_contents('yourtemplate.html');
$tpl = str_replace('{{content}}', $content, $tpl);
mail($tpl, ...);
Comments