我正在尝试为HTML电子邮件模板设置变量,但我似乎无法访问变量。当我在模板中回显变量名时,在发送的电子邮件中显示错误“未定义变量”。在错误描述中,它甚至显示已设置的变量。
App::uses('CakeEmail', 'Network/Email');
$Email = new CakeEmail();
$Email->from(array('info@example.com' => 'Test'));
$Email->to('myemail@email.com');
$Email->subject('Test email variables');
$Email->template('layout_1');
$Email->emailFormat('html');
$Email->viewVars(array('input_1','test input 1'));
$Email->send();
电子邮件中显示以下错误,我实际上可以看到我设置的“输入1”变量。
NOTICE (8): UNDEFINED VARIABLE: INPUT_1 [APP/VIEW/EMAILS/HTML/LAYOUT_1.CTP, LINE 44]
CODE CONTEXT
<TR>
<TD CLASS="HEADER-BAR" VALIGN="TOP" STYLE="COLOR: #999; FONT-FAMILY: VERDANA; FONT-SIZE: 10PX; TEXT-TRANSFORM: UPPERCASE; PADDING: 0 20PX; HEIGHT: 15PX;" WIDTH="400"
HEIGHT="15">
<?PHP PRINT_R($INPUT_1); ?>
$VIEWFILE = '/APPLICATIONS/XAMPP/XAMPPFILES/HTDOCS/APP/VIEW/EMAILS/HTML/LAYOUT_1.CTP'
$DATAFORVIEW = ARRAY(
'CONTENT' => '',
(INT) 0 => 'INPUT_1',
(INT) 1 => 'TEST INPUT 1'
)
$CONTENT = ''
INCLUDE - APP/VIEW/EMAILS/HTML/LAYOUT_1.CTP, LINE 44
VIEW::_EVALUATE() - CORE/CAKE/VIEW/VIEW.PHP, LINE 945
VIEW::_RENDER() - CORE/CAKE/VIEW/VIEW.PHP, LINE 907
VIEW::RENDER() - CORE/CAKE/VIEW/VIEW.PHP, LINE 471
CAKEEMAIL::_RENDERTEMPLATES() - CORE/CAKE/NETWORK/EMAIL/CAKEEMAIL.PHP, LINE 1582
CAKEEMAIL::_RENDER() - CORE/CAKE/NETWORK/EMAIL/CAKEEMAIL.PHP, LINE 1448
CAKEEMAIL::SEND() - CORE/CAKE/NETWORK/EMAIL/
CAKEEMAIL.PHP, LINE 1073
TEMPLATESCONTROLLER::SEND_NEWSLETTER() - APP/CONTROLLER/TEMPLATESCONTROLLER.PHP, LINE 110
TEMPLATESCONTROLLER::EDIT() - APP/CONTROLLER/TEMPLATESCONTROLLER.PHP, LINE 194
REFLECTIONMETHOD::INVOKEARGS() - [INTERNAL], LINE ??
CONTROLLER::INVOKEACTION() - CORE/CAKE/CONTROLLER/CONTROLLER.PHP, LINE 486
DISPATCHER::_INVOKE() - CORE/CAKE/ROUTING/DISPATCHER.PHP, LINE 187
DISPATCHER::DISPATCH() - CORE/CAKE/ROUTING/DISPATCHER.PHP, LINE 162
[MAIN] - APP/WEBROOT/INDEX.PHP, LINE 111
我还尝试打印显示的$ dataforview变量,但是我得到了同样的错误。
答案 0 :(得分:2)
我很久没有使用过蛋糕了,但很快就看到了他们的文档,他们期待一把钥匙=&gt; value数组传递给viewVars()方法。
我认为你打算这样做:
$Email->viewVars(array('input_1' => 'test input 1'));
其次,看起来您将值定义为“input_1”,但尝试将其作为“INPUT_1”进行访问。在PHP中,变量区分大小写。