繁体   English   中英

电子邮件模板以XML存储。 如何在xml中执行php?

[英]Email templates stored in XML. How to execute php inside the xml?

我有一个像这样的电子邮件xml文件:

<?xml version='1.0' standalone='yes'?>
<emails>
 <email id="contract-expired">
    <description>We send this email when we want to inform a user that a contract is coming up to it's expiration date</description>
    <subject>Example: Contract Expiration</subject>
    <parameters>
        <param>name</param>
        <param>number_of_contracts</param>
    </parameters>
    <content>
        <html>
            <![CDATA[
            Dear %name%, <br />
            <p>
            <?php
                       foreach($contract as $value):
                         echo $value->getTitle()
                       endforeach;
                    ?>
            You have %number_of_contracts% contracts that are due to expire in under 30 days. Please log in to example.com to review the contracts.
            <br /><br />
            This is an automated email. Please do not reply.
            </p>
            ]]>
        </html>
        <plain>
            Dear %name%, You have %number_of_contracts% contracts that are due to expired in under 30 days. Please log in to example.com to review the contracts. This is an automated email. Please do not reply.
        </plain>
    </content>
 </email>
</emails>

我有一个处理电子邮件模板的类。

也就是说,您可以像这样设置参数:

$mail->setParameter("name", "Jamie");

当您调用setContent时,它将对%name%和所有其他参数的内容执行str_replace。 很简单。

然后我意识到,如果我需要在模板中进行循环以列出所有合同等,该怎么办。我必须在xml文件中进行php循环,但是我该如何传递对象然后执行内容作为PHP并接收输出?

有任何想法吗?

谢了,兄弟们!

编辑:只是要澄清一些事情。 我不希望建立html,然后将其作为参数传递。 我正在使用symfony,因此我想将html保留在控制器之外。

在这里被称为:

  public function executeContractExpired(sfWebRequest $request)
  {
    $mail = new Mail("contract-expired");
    $mail->setParameter("name", "Joe Bloggs");
    $mail->setParameter("number_of_contracts", 567);
    $mail->setContent();

    $message = $this->getMailer()->compose();
    $message->setSubject($mail->getSubject());
    $message->setTo("example@example.com");
    $message->setFrom(array("jamie@example.com"=>"Automated Message"));
    $message->setBody($mail->getHtmlContent(), 'text/html');
    $message->addPart($mail->getPlainContent(), 'text/plain');

    $this->getMailer()->send($message);
  }

做类似的事情(在行动中):

$contracts_html = "<ul>";
foreach($contracts as $contract)
{
    $contracts .= "<li>" . $contract->getTitle() . "</li>"
}
$contracts .= "</il>";

然后将其作为参数传递:

$mail->addParameter("contracts", $contracts_html);

这种方法很难看。

我建议模板中不要包含逻辑。 添加一个占位符%contracts%并教您的模板处理类如何从PHP(而非模板)内部替换它。 您可以编写一个辅助类,与Mailer类一起使用。 这样,您可以将其保留在控制器之外。 IMO,配置Mailer的代码也不应该在其中,而是在模型的Service类中。

附带说一句,这似乎是XSLT的不错选择。

$contractTitles = implode(',', $contract);

$mail->setParameter("contract_titles", $contractTitles);

将您的for循环替换为%contract_titles%

            <![CDATA[
            Dear %name%, <br />
            <p>
           %contract_titles%
            You have %number_of_contracts% contracts that are due to expire in under 30 days. Please log in to example.com to review the contracts.
            <br /><br />
            This is an automated email. Please do not reply.
            </p>
            ]]>

因此,建立合同标题列表并将其替换为消息正文。

可能是您可以使用ob_start在发送到电子邮件之前准备输出

对不起,我不在上课模式下做。 但我希望您能为您提供见识。

代码是这样的:

function callback($buffer)
{   
    // bundle of string you want to replace
    return str_replace("name", "Mr.John" , $buffer);
}

function getEmail($email_template_path, $contacts){
    ob_start();
    include($email_template_path);
    $email_to_send = ob_get_contents(); 
    ob_end_clean();

    return callback($email_to_send);
}

echo getEmail("/opt/lampp/htdocs/index.html",array("Mr. Smith", "Mrs.Smith"));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM