PHP html电子邮件,使用html模板 [英] PHP html email, using html template
问题描述
所以我试图做一个html邮件系统和我的html我想成为一个模板,存储在一个单独的文件,如:
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>
当我想发送邮件时,我想要我的邮件内容(从输入表单)进入那个div.content然后发送整个html(模板+提交的文本)。
最好的办法是做什么?我正在考虑如下:
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:
- 将模板导入发送邮件的PHP中
- 找到具有内容类的div,并将提交的文本添加到其中
- 发送邮件
但我不知道如何找到那个div并将提交的文本写入它。
but i don't know how to "find" that div and write the submitted text into it.
推荐答案
如Pekka所说,您可以使用 str_replace
插入您的模板中的数据。只需添加一个占位符:
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, ...);
这篇关于PHP html电子邮件,使用html模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!