我有以下发送电子邮件的函数:
function send_email($email){
$subject = "TITLE";
$message = "HERE IS THE MESSAGE";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "'r'n";
$headers .= "Content-type:text/html;charset=UTF-8" . "'r'n";
// More headers
$headers .= 'From: <emaily>' . "'r'n";
mail($email,$subject,$message,$headers);
}
我想在保存模板的文件email.html
中调用,而不是将$message
作为字符串。
我添加了这个:
require 'email.html';
但是我如何调用文件呢?
$message = [call in email.html here]
Require用于在另一个php文件中调用函数,或者在HTTP响应中包含一些数据。
对于这个问题,file_get_contents('email.html')
是首选。这就是我要使用的方法:
function send_email($email){
$subject = "Subject of your email";
$message = "";
if(file_exists("email_template.html")){
$message = file_get_contents('email_template.html');
$parts_to_mod = array("part1", "part2");
$replace_with = array($value1, $value2);
for($i=0; $i<count($parts_to_mod); $i++){
$message = str_replace($parts_to_mod[$i], $replace_with[$i], $message);
}
}else{
$message = "Some Default Message";
/* this likely won't ever be called, but it's good to have error handling */
}
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "'r'n";
$headers .= "Content-type:text/html;charset=UTF-8" . "'r'n";
// More headers
$headers .= 'From: <doNotReply@myDomain.com>' . "'r'n";
$headers .= "To: <$email>'r'n";
$header .= "Reply-To: doNotReply@myDomain.com'r'n";
mail($email,$subject,$message,$headers);
}
我稍微修改了你的代码,并添加了file_get_contents
和file_exists
。file_exists
确认文件在那里。如果不是,则可以避免尝试读入时的潜在错误,并且可以将其更改为使用默认值。下一个添加的是for循环。在"$parts_to_mod
"数组中,输入待替换模板的默认值。在$replace_with
数组中,放入您想要替换模板部分的唯一值。
作为我使用这个的一个例子,我有一个模板URL,我的一个程序说id=IDENTIFIER&hash=THEHASH
,所以在我的程序中,我的parts_to_mod
说$parts_to_mod = array("IDENTIFIER", "THEHASH");
,我的replace_with
说$replace_with = array($theUsersIdentifier, $theUsersHash);
。然后它进入for循环并用replace_with
中的值替换parts_to_modify
中的值。
简单的概念,它们使你的代码更短,更容易维护。
编辑:下面是一些示例代码:
假设模板是:
<span>Dear PUTNAMEHERE,</span><br>
<div>PUTMESSAGEHERE</div>
<div>Sincerely,<br>PUTSENDERHERE</div>
在php代码中,我们写入:
$parts_to_mod = array("PUTNAMEHERE", "PUTMESSAGEHERE", "PUTSENDERHERE");
$replace_with = array($name, $theBodyOfYourEmail, $whoYouAre);
just use file_get_contents('email.html')
这个方法返回一个包含文件内容的字符串
您可以使用此函数调用自定义电子邮件模板。
function email($fields = array(),$name_file, $from, $to) {
if(!empty($name_file)) {
$mail_tem_path = 'templates/mails/mail--'.$name_file.'.php'; //change path of files and type file.
if(file_exists($mail_tem_path)) {
$headers = "MIME-Version: 1.0". "'r'n";
$headers .= "Content-Type: text/html;charset=UTF-8". "'r'n";
// Additional headers
$headers .= "From:".$fields["subject_from"]." <$from>". "'r'n";
$headers .= "Content-Transfer-Encoding: 8Bit". "'r'n";
$message = file_get_contents($mail_tem_path);
$message = preg_replace("/'"/", "'", $message);
foreach ($fields as $field) {
// Replace the % with the actual information
$message = str_replace('%'.$field["name"].'%', $field["value"], $message);
}
$send = mail($to, $fields["subject"], $message, $headers);
} else {
$send = mail($to, "Error read mail template", "Contact with admin to repair this action.");
}
} else {
$send = mail($to, "Error no exist mail template", "Contact with admin to repair this action.");
}
return $send;
}
模板的html <html>
<body>
<a href="/home">TODO write content %value_on_array%</a>
</body>
</html>
数组和执行函数
$fields = array(
"subject" => "tienda_seller_subject",
"subject_from" => "tienda_email_subject_from",
0 => array(
"name" => "value_on_array",
"value" => "result before change"
),
);
//email($fields = array(),$name_file, $from, $to);
email($fields, 'admin', 'owner@email.com', 'client@email.com');
结果