I'd like to use a view as an email body.
I tried this one:
$renderer = new PhpRenderer();
$bodyHtml = $renderer->render(
'/user/email/reset-password-email',
[
'passwordResetUrl' => $passwordResetUrl,
]);
$html = new MimePart($bodyHtml);
I get the following error:
Unable to render template "/user/email/reset-password-email"; resolver could not resolve to a file
My view-template is located in:
Any help appreciated.
To render view as string, you need a renderer " ViewRenderer
".
If you can get ServiceManager, then,
$renderer = $serviceManager->get('ViewRenderer');
Now,
$bodyHtml = $renderer->render(
'/user/email/reset-password-email',
[
'passwordResetUrl' => $passwordResetUrl,
]);
$html = new MimePart($bodyHtml);
You need to retrieve the renderer from the service locator in the factory, and not instantiating it directly in the class where you are using it.
Email sender class factory
class EmailSenderFactory {
public function __invoke($services) {
$viewRenderer = $services->get('ViewRenderer');
return new EmailSender($viewRenderer);
}
}
Email sender class
class EmailSender {
private $viewRenderer;
public function __construct($viewRenderer) {
$this->viewRenderer = $viewRenderer;
}
public function createEmail() {
$template = '/user/email/reset-password-email';
$variables = ['passwordResetUrl' => $passwordResetUrl];
$bodyHtml = $renderer->render($template, $variables);
$html = new MimePart($bodyHtml);
}
}
I had the same problem (sending an email for password reset) and I solved this way. Hope this will help you :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.