2013-08-30 81 views
0

我有一个表单。我使用“发布”方法发送并通过电子邮件发送给注册的多个人。然后,电子邮件的$正文基于模板。没有数据库,没有类,简单的形式。是的,我已经读过这些,他们都在那里,我不能把它放在一起,与这种情况有关。从文本文件获取内容并替换关键字PHP

“email_template.txt” 应该是这样的文字:

Hello #firstname# #lastname#. Thank you for registering! Your registered email is #email# 

看起来像这样在处理由PHP

Hello **John Doe**. Thank you registering! Your registered email is **[email protected]**. 

在我的PHP我有类似:

<?php 
//form and validation 
$firstname = "John"; // inputed name on the form, let say 
$lastname = "Doe"; // inputed last name 
$email = "example"; // inputed email 

//email message 
$body = ... some type of a get_file_content.... 
mail($_POST['email'], 'Party Invitation', $body, 'From: [email protected]'); 
?> 

其中$ body是通过此PHP表单提交给注册人的电子邮件。

回答

0

的sprintf将工作良好

您的模板可以是:

你好%s%S。感谢您注册!您注册的邮件为%s

$身体= sprintf的($ fileContents,$名字,姓氏$,$电子邮件);

str_replace(),几乎找到替代。

$ body =“Hello#firstname##lastname#。谢谢注册!您的注册邮箱是#email#”;

$体= str_replace函数( “#姓名#”,$姓名,$体);

$体= str_replace函数( “#姓氏#”,$姓氏,$体); $ body = str_replace(“#email#”,$ email,$ body); $ body = str_replace(“#email#”,$ email,$ body);

+0

这会不会以某种方式工作.... $身体=的file_get_contents(email_template.txt); $ body = str_replace(“#firstname#”,$ firstname,$ body); $ body = str_replace(“#lastname#”,$ lastname,$ body); $ body = str_replace(“#email#”,$ email,$ body); – mythoslife

+0

没关系,我明白了! – mythoslife

+0

好的,你找到了引号:) – surivitna