Cakephp 4.x Passing Variables to Email Template

701 views Asked by At

In CakePHP 4.x I'm having some trouble with the mailer() function, specifically passing variables from my controller to a template.

I need a user to be able to receive an email with a list of items. I'm able to use ajax to send an array to my controller, but I can't get the controller to pass the variables to the email template. I'm getting the email but not the dynamic content and I just can't get it to work via setViewVars.

My controller function:

public function bar(): void
{
if( $this->request->is('ajax') ) {

$this->request->allowMethod('ajax');
$foo = $this->request->getQuery('data');
// data is a stringified array

$mailer = new Mailer("default");
  $mailer->viewBuilder()->setTemplate('default','default');
  $mailer
   ->setEmailFormat('html')
   ->setFrom(['[email protected]' => 'Comparaciones'])
   ->setTo('[email protected]')
   ->setSubject('About')
   >setViewVars($foo)
  ->send();
// ->deliver() doesn't work either
}
}

My template:

// standard cakephp default template
$content = explode("\n", $content);

foreach ($content as $line) :
    echo '<p> ' . $line . "</p>\n";
endforeach;

I'm probably tripping up with something simple, I just can't get the array into the template. Any help much appreciated!!!

0

There are 0 answers