【问题标题】:What is the best way to use Email Template in Zend/PHP在 Zend/PHP 中使用电子邮件模板的最佳方法是什么
【发布时间】:2011-01-25 20:25:15
【问题描述】:

我正在开发一个用户创建帐户的网站。我需要向许多海洋上的用户发送电子邮件。例如,当注册、忘记密码、订单摘要等时。我想为此使用电子邮件模板。我需要你的建议。我想使用一种方式,如果我更改任何电子邮件模板或登录,并且更改时间更短。

我想过以下办法:

我有一个这样的电子邮件模板表:

id
emailSubject
emailBody
emailType

例如当用户忘记密码时:

身份证号:

1

电子邮件主题:

ABC: Link for Password Change

邮件正文:

<table border=0>
<tr><td>
   <b> Dear [[username]] <b/>
</td></tr>

<tr><td>
   This email is sent to you in response of your password recovery request. If you want to change your password, please click the following link:<br />
[[link1]]
<br/>
If you don't want to change your password then click the following link:<br/>
[[link2]]
</tr></td>

<tr><td>
   <b>ABC Team</b>
</td></tr>

</table>

电子邮件类型:

ForgotPassword

准备电子邮件数据:

$emailFields["to"] = "user@abc.com";
$emailFields["username"] = "XYZ";
$emailFields["link1"] = "http://abc.com/changepassword?code='123'";
$emailFields["link2"] = "http://abc.com/ignorechangepasswordreq?code='123'";
$emailFields["emailTemplate"] = "ForgotPassword";

现在将所有字段传递给此函数以发送电子邮件:

sendEmail( $emailFields ){
 // Get email template from database using emailTemplate name.
 // Replace all required fields(username, link1,link2) in body.
 // set to,subject,body
 // send email using zend or php
}

我打算使用上述方法。您能否提出更好的方法或对上述逻辑进行一些更改。

谢谢。

【问题讨论】:

    标签: php zend-framework email-templates


    【解决方案1】:

    如前所述,Zend_View 是一种方式。这是我的做法:

    $template = clone($this->view);
    $template->variable = $value;
    $template->myObject = (object)$array;
    // ...
    $html = $template->render('/path/filename.phtml');
    

    使用 Markdownify 转换为纯文本:

    $converter = new Markdownify_Extra;
    $text = $converter->parserString($html);
    

    设置邮件:

    $mail = new Zend_Mail();
    $mail->setBodyHtml($html);
    $mail->setBodyText($text);
    

    然后设置传输和发送。

    【讨论】:

      【解决方案2】:

      在我的脑海中,如此未经测试......但类似的东西应该可以工作:

      $view = new Zend_View();
      $view->setScriptPath( '/path/to/your/email/templates' );
      $view->assign( $yourArrayOfEmailTemplateVariables );
      $mail = new Zend_Mail();
      // maybe leave out phtml extension here, not sure
      $mail->setBodyHtml( $view->render( 'yourHtmlTemplate.phtml' ) );
      $mail->setBodyText( $view->render( 'yourTextTemplate.phtml' ) );
      

      【讨论】:

        【解决方案3】:

        我会使用Zend_View。将您的模板存储在 /views/email/EMAILNAME.phtml 中,使用所需的电子邮件模板创建一个 Zend_View 对象并将所需的数据传递给它。

        【讨论】:

        猜你喜欢
        • 2021-10-10
        • 1970-01-01
        • 2016-06-28
        • 2017-06-20
        • 1970-01-01
        • 2011-06-18
        • 2018-02-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多