【问题标题】:Send an email templating during a include in PHP在 PHP 中包含期间发送电子邮件模板
【发布时间】:2015-09-17 19:01:24
【问题描述】:

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

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

<?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); 
}

我的问题:

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

【问题讨论】:

标签: php


【解决方案1】:

很高兴看到您使用my previously suggested 设置。但是,该设置取决于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); 
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-22
    • 1970-01-01
    • 2019-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-11
    相关资源
    最近更新 更多