电子邮件模板中的Html助手

电子邮件模板中的Html助手

本文介绍了CakePHP 2:电子邮件模板中的Html助手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个艰难的时间试图找出为什么我不能使用$ this-> Html-> url在我的电子邮件CakePHP 2.0的模板。 Helpers数组在视图中为空。这是设计还是我缺少某些东西?



文档和迁移指南中没有提及任何东西。



跟踪类似:

  include  -  APP / View / Emails / html / admin / users_recover.ctp,第1行
View :: _ render() - CORE / Cake / View / View.php,line 598
View :: render() - CORE / Cake / View / View.php, CakeEmail :: _ render() - CORE / Cake / Network / Email / CakeEmail.php,line 1300
CakeEmail :: send() - CORE / Cake / Network / Email / CakeEmail.php,line 933
UsersController :: _ sendRecoverEmail() - APP / Controller / UsersController.php,第186行
UsersController :: admin_login() - APP / Controller / UsersController.php,第101行
ReflectionMethod :: invokeArgs内部],线??
Controller :: invokeAction() - CORE / Cake / Controller / Controller.php,第476行
Dispatcher :: _ invoke() - CORE / Cake / Routing / Dispatcher.php,第106行
Dispatcher :: dispatch() - CORE / Cake / Routing / Dispatcher.php,第88行
[main] - APP / webroot / index.php,第96行
pre>

这是我发送辅助邮箱的函数:

  private function _sendRecoverEmail($ email,$ recoverKey){
App :: uses('CakeEmail','Network / Email');
$ cakeEmail = new CakeEmail();
$ cakeEmail-> config('default');
$ cakeEmail-> to($ email);
$ cakeEmail-> subject('Recover Your'。Configure :: read('Site.title')。'Account');
$ cakeEmail-> template('admin / users_recover');
$ cakeEmail-> viewVars(array(
'recoverKey'=> $ recoverKey,
));
try {
$ cakeEmail-> send();
} catch(Exception $ e){
trigger_error(UsersController :: sendRecoverEmail中的错误:无法发送电子邮件至:{$ email}。
return false;
}
return true;
}

最后,视图:

 < p>您是否在进入您的<?php echo Configure :: read('Site.title') ?>帐户?< / p> 

<?php $ url = $ this-> Html-> url(array('action'=>'recover','controller'=>'users',$ recoveryKey ),true); ?>

< p>如果这是正确的,< a href =<?php echo $ url;?>>点击此处恢复您的访问权限并创建新密码。 ; / a>< / p>

< p>如果您忽略它,您的密码将保持不变。< / p>您是否尝试过设置CakeEmail渲染器帮助器,然后再调用send()方法,然后再尝试设置CakeEmail渲染器帮助器。

  $ cakeEmail-> helpers('Html')
pre>

点击



DRY)选项可以在您的模板中加载HtmlHelper:

 <?php 
$ htmlHelper = > Helpers-> load('Html');

$ url = $ htmlHelper-> url(array('action'=>'recover','controller'=>'users',$ recoveryKey),true);
?>


I'm having a tough time trying to figure out why I can't use $this->Html->url in the templates for my emails for CakePHP 2.0. The Helpers array is empty in the views. Is this by design or am I missing something?

The docs and migration guide don't mention anything around this.

The trace looks like:

include - APP/View/Emails/html/admin/users_recover.ctp, line 1
View::_render() - CORE/Cake/View/View.php, line 598
View::render() - CORE/Cake/View/View.php, line 365
CakeEmail::_render() - CORE/Cake/Network/Email/CakeEmail.php, line 1300
CakeEmail::send() - CORE/Cake/Network/Email/CakeEmail.php, line 933
UsersController::_sendRecoverEmail() - APP/Controller/UsersController.php, line 186
UsersController::admin_login() - APP/Controller/UsersController.php, line 101
ReflectionMethod::invokeArgs() - [internal], line ??
Controller::invokeAction() - CORE/Cake/Controller/Controller.php, line 476
Dispatcher::_invoke() - CORE/Cake/Routing/Dispatcher.php, line 106
Dispatcher::dispatch() - CORE/Cake/Routing/Dispatcher.php, line 88
[main] - APP/webroot/index.php, line 96

Here is the function that I have sending the recovery email:

private function _sendRecoverEmail($email, $recoverKey) {
    App::uses('CakeEmail', 'Network/Email');
    $cakeEmail = new CakeEmail();
    $cakeEmail->config('default');
    $cakeEmail->to($email);
    $cakeEmail->subject('Recover Your '. Configure::read('Site.title') . ' Account');
    $cakeEmail->template('admin/users_recover');
    $cakeEmail->viewVars(array(
        'recoverKey' => $recoverKey,
    ));
    try {
        $cakeEmail->send();
    } catch (Exception $e) {
        trigger_error("ERROR in UsersController::sendRecoverEmail: couldn't send email to: {$email}.");
        return false;
    }
    return true;
}

And finally, the view:

<p>Have you been having difficulty getting into your <?php echo Configure::read('Site.title'); ?> account?</p>

<?php $url = $this->Html->url(array('action' => 'recover', 'controller' => 'users', $recoveryKey), true); ?>

<p>If this is correct, <a href="<?php echo $url; ?>">click here to recover your access and create a new password.</a></p>

<p>Your password will remain the same if you ignore it.</p>
解决方案

Have you tried setting CakeEmail renderer helpers before calling send()?

$cakeEmail->helpers('Html')

Bit more info at http://api20.cakephp.org/file/Cake/Network/Email/CakeEmail.php#method-CakeEmailhelpers

Another (anti-DRY) option could be to load HtmlHelper inside your templates:

<?php
$htmlHelper = $this->Helpers->load('Html');

$url = $htmlHelper->url(array('action' => 'recover', 'controller' => 'users', $recoveryKey), true);
?>

这篇关于CakePHP 2:电子邮件模板中的Html助手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 00:20