[Zend Framework 2]Send mail with view template

module.config.php

    'view_manager' => array(
        'template_map' => array(
            'mail/foo'            => __DIR__ . '/../view/MODULE_NAME/mail/foo.phtml
        ),

 

Controller


$data = ('name'=>'tkawanaka', 'address'=>'Japan', 'email'=>''foo@example.com);
$this->renderer = $this->getServiceLocator()->get('ViewRenderer'); 
$content = $this->renderer->render('mail/foo', $data);

$mail = new Mail\Message();
$mail->setBody($content);
$mail->setFrom($data['email'], $data['name']);
$mail->setTo('to@example.com');
$mail->setSubject('send mail with view template');
$transport = new Mail\Transport\Sendmail();
$transport->send($mail);

 

foo.phtml


name: $this->name
address: $this->address
email: $this->email