Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 带有str_replace的HTML电子邮件模板_Php_Html_Templates_Phpmailer_Str Replace - Fatal编程技术网

Php 带有str_replace的HTML电子邮件模板

Php 带有str_replace的HTML电子邮件模板,php,html,templates,phpmailer,str-replace,Php,Html,Templates,Phpmailer,Str Replace,我有一个表单,我用AngularJs编程。当用户提交 该表单将执行一个php脚本(使用Slim制作),该脚本将formdata插入数据库并生成一封电子邮件(PHPmailer) 现在谈问题: 我从电子邮件中获取结构,其中包含模板中的文件内容,并将占位符替换为str\u replace 模板: <h2>User Information</h2> <table> <tbody> <tr><th>Firstname:

我有一个表单,我用AngularJs编程。当用户提交 该表单将执行一个php脚本(使用Slim制作),该脚本将formdata插入数据库并生成一封电子邮件(PHPmailer)

现在谈问题:

我从电子邮件中获取结构,其中包含模板中的文件内容,并将占位符替换为str\u replace

模板:

<h2>User Information</h2>

<table>
  <tbody>
    <tr><th>Firstname:</th><td>%firstname%</td></tr>
    <tr><th>Lastname:</th><td>%lastname%</td></tr>
  </tbody>
</table>
<br>

<h2>Other Information</h2>

<table>
  <tbody>
    <tr><th>Phone:</th><td>%phone%</td></tr>
    <tr><th>E-Mail:</th><td>%email%</td></tr>
    <tr><th>Organisation:</th><td>%organisation%</td></tr>
  </tbody>
</table>
用户信息
名字:%Firstname%
Lastname:%Lastname%

其他资料 电话:%Phone% 电子邮件:%email% 组织:%Organization%
我的问题是,并非所有这些字段都是必需的,我想删除它们 如果没有这些变量(电话、电子邮件、,
组织)已设置。

如果您创建两个不同的模板(有或没有该表),并根据电话、电子邮件和组织的存在呈现其中一个模板,则效果最佳

比如:

函数呈现($firstname,$lastname,$phone=null,$email=null,$organization=null)
{
$templatePath=(is_null($phone)&&is_null($email)&&is_null($organization))?
“/path/to/your/template/withound/the/contact/table.html”:
“/path/to/your/template/with/the/contact/table.html”;
$templateContent=file\u get\u contents($templatePath);
$占位符=[
“%firstname%”,
“%lastname%”,
“%phone%”,
“%email%”,
“%organization%”,
];
$values=[
$firstname,
$lastname,
$phone,
$email,
美元组织,
];
$rendered=str_replace($placeHolders,$values,$templateContent);
退还已支付的美元;
}
您可以“隐藏”表格,而不是删除

设置参数,如:

<table style="display: %display-other-information%">

如果属性为空,则必须设置为“无”,否则设置为“正常”


更好的解决方案是创建其他模板,但是,如果您不想,可以这样做,就像我说的。

有更简单的解决方案吗?我不想使用这个,因为我必须为这封电子邮件制作大约8个模板。(所示示例不是我真正的模板:)我认为这是三个选项中最简单的一个(我思考)。1.有两个模板(建议)。2.如有需要,将电话、电子邮件和组织的模板和部分内容注入模板(这是另一个简洁的解决方案)3。根据电话、电子邮件和组织状态操纵单个模板的内容(这不是很好)。:)没有这样的小例子,但是如果您的项目要使用更多的模板,您可能需要考虑一些模板引擎,例如