在PHP中包含期间发送电子邮件模板

在PHP中包含期间发送电子邮件模板,php,Php,我尝试发送带有模板的电子邮件。我有几个模板(一个重置密码,一个升级帐户,…) 所有这些模板都存储在邮件目录中,如下所示: <?php // Template to reset password $message = "Hello Mr, ..."; $subject = "Your new password"; $email = "mr@company.com"; ?> 现在我有一个函数: function sendMail($template,

我尝试发送带有模板的电子邮件。我有几个模板(一个重置密码,一个升级帐户,…)

所有这些模板都存储在
邮件
目录中,如下所示:

<?php
    // Template to reset password
    $message = "Hello Mr, ...";
    $subject = "Your new password";
    $email = "mr@company.com";
?>

现在我有一个函数:

function sendMail($template, $USR_Id) {

    ob_start();
    include 'mail/'.$template.'.php';
    $message = ob_get_contents();
    ob_end_clean();

    // Start configuring the email
    $headers .= 'From: company <noreply@company.com>' . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=UTF-8\r\n";

    mail($email, $subject, $message, $headers); 
}
函数sendMail($template,$USR\u Id){
ob_start();
包括“邮件/”.$template..php”;
$message=ob_get_contents();
ob_end_clean();
//开始配置电子邮件
$headers.='发件人:公司'。“\r\n”;
$headers.=“MIME版本:1.0\r\n”;
$headers.=“内容类型:text/html;字符集=UTF-8\r\n”;
邮件($email、$subject、$message、$headers);
}
我的问题是:

如何在我的模板文件(即:
$subject
$email
)中取回信息存储?

很高兴看到您使用安装程序。但是,该设置取决于
echo
,如果您不使用它,您可以使功能更加简单。如果你的模板是这样的;您可以简单地执行以下操作:

function sendMail($template, $USR_Id) {

    include 'mail/'.$template.'.php';

    // Start configuring the email
    $headers .= 'From: company <noreply@company.com>' . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=UTF-8\r\n";

    mail($email, $subject, $message, $headers); 
}
函数sendMail($template,$USR\u Id){
包括“邮件/”.$template..php”;
//开始配置电子邮件
$headers.='发件人:公司'。“\r\n”;
$headers.=“MIME版本:1.0\r\n”;
$headers.=“内容类型:text/html;字符集=UTF-8\r\n”;
邮件($email、$subject、$message、$headers);
}

如果你使用;您可以跨多个文件使用所有变量

只需包含模板即可。输出缓冲是不需要的。你说的“返回”是什么意思?从数组中的函数返回这些变量。我想使用模板中包含的这些变量。可能重复