user3661042
user3661042

Reputation: 167

CakePHP 3 - How to pass object data in email template

How to pass object data in email template? Object data:

    object(App\Model\Entity\NewsletterTemplate) {

        'id' => (int) 1,
        'title' => 'Newsletter 1',
        'publish' => '21.04.2016',
        'box_title1' => 'Aenean id erat ut leo semper viverra',
        'box_text1' => 'Integer eu orci viverra',
...
    }

E-mail function:

private function _send_mail($data,$user,$sender)
{
 $email = new Email('default');
 $email->template('newsletter_template','newsletter_body')
 ->emailFormat('html')
 ->subject('Newsletter')
 ->to($user)
 ->from([$sender => 'Some Name'])
 ->viewVars($data)
 ->send();
}

I tried this but it does not work (E-mail template newsletter_template.ctp):

...
                        <td class="mcnTextContent mcnTextContentLeft" style="padding-top:0; padding-left:18px; padding-bottom:9px; padding-right:18px;" valign="top">                       
                            <?php echo $title; ?>, <?php echo $publish; ?>
...
                        </td>

Upvotes: 2

Views: 2317

Answers (1)

arilia
arilia

Reputation: 9398

as the manual reports

you can set email view variables in this way:

$email->viewVars([
    'title' => $title,
    'publish' => $publish
]);

of course you can pass the whole entity too using this method

Upvotes: 6

Related Questions