Php Wordpress-读取存储在文件中的HTML电子邮件模板

Php Wordpress-读取存储在文件中的HTML电子邮件模板,php,wordpress,html-email,Php,Wordpress,Html Email,从我在Wordpress中的自定义主题,我想发送一封HTML电子邮件 而不是像这样填充身体: $message = ""; $message = "<h3>Someone has applied for a job on the site.</h3>" . "<br><br>"; $message .= "Profile Picture URL: " . $Picture . "<br>"; $message .= "Name: " .

从我在Wordpress中的自定义主题,我想发送一封HTML电子邮件

而不是像这样填充身体:

$message = "";
$message = "<h3>Someone has applied for a job on the site.</h3>" . "<br><br>";
$message .= "Profile Picture URL: " . $Picture . "<br>";
$message .= "Name: " . $Name . "<br>";
$message .= "Email: " . $Email . "<br>";
$message .= "Phone: " . $Phone . "<br>";
$message .= "Work Phone: " . $WorkPhone . "<br>";
$message .= "Resume: " . $Resume . "<br>";
$message .= "Preferences: " . sanitize_text_field($Preferences) . "<br><br>";
<?php

$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);

echo $comma_separated; // lastname,email,phone
?>
如何读入该html文件,将其名称和地址放入其中,然后将其存储在$message中

我的例子看起来太简单了,但实际上我已经构建了3封非常全面的HTML电子邮件,我想使用它们,而且每封邮件都有很多代码


谢谢,

您可以使用
str\u replace
完成此操作

首先,您需要获取模板内容作为变量

$template = file_get_contents('/path/to/your/template.html');
然后,需要为放置标记交换现有变量。从示例源代码中,您可以像这样交换名称字段:

$template = str_replace('[NameGoesHere]', $name, $template);
根据需要对任何需要更换的内容重复此操作


然后,当您完成后,只需将结果用作您的电子邮件内容。

我认为您自己最好的书面解决方案就是使用
php
内置方法对其进行解析

要读取文件,请使用:

示例

// From Link
<?php
// <= PHP 5
$file = file_get_contents('./HTML_TEMPLATE1.html', true);
// > PHP 5
$file = file_get_contents('./HTML_TEMPLATE1.html', FILE_USE_INCLUDE_PATH);
?>
然后,将其设置回
$message
并发送邮件:

$message = $value_of_attached_array;
wp_mail($to, $subject, $message, $headers, $attachments);
希望这有帮助:)

<?php
// Example
// Use previous $file-> ($file  = "<html>...";)

$pieces = explode("#", $file);// "#" is YOUR_DELIMITER
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
?>
<?php

$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);

echo $comma_separated; // lastname,email,phone
?>
$message = $value_of_attached_array;
wp_mail($to, $subject, $message, $headers, $attachments);