We can't create mailer directly in DI container using the factory because it may fail in case the plugin is not configured properly and prevent plugin from start. The Mailer was previously injected because it contains lazy initialization. The lazy initialization in the singleton Mailer service was dangerous, because when used with some custom parameters it could cause that all services that inject the Mailer from DI would use the custom config instead the default. [MAILPOET-4115]
44 lines
1.2 KiB
PHP
44 lines
1.2 KiB
PHP
<?php declare(strict_types = 1);
|
|
|
|
namespace MailPoet\Services;
|
|
|
|
use MailPoet\Config\Renderer;
|
|
use MailPoet\Mailer\MailerFactory;
|
|
use MailPoet\Mailer\MetaInfo;
|
|
|
|
class CongratulatoryMssEmailController {
|
|
/** @var MailerFactory */
|
|
private $mailerFactory;
|
|
|
|
/** @var MetaInfo */
|
|
private $mailerMetaInfo;
|
|
|
|
/** @var Renderer */
|
|
private $renderer;
|
|
|
|
public function __construct(
|
|
MailerFactory $mailerFactory,
|
|
MetaInfo $mailerMetaInfo,
|
|
Renderer $renderer
|
|
) {
|
|
$this->mailerFactory = $mailerFactory;
|
|
$this->mailerMetaInfo = $mailerMetaInfo;
|
|
$this->renderer = $renderer;
|
|
}
|
|
|
|
public function sendCongratulatoryEmail(string $toEmailAddress) {
|
|
$renderedNewsletter = [
|
|
'subject' => _x('Sending with MailPoet works!', 'Subject of an email confirming that MailPoet Sending Service works', 'mailpoet'),
|
|
'body' => [
|
|
'html' => $this->renderer->render('emails/congratulatoryMssEmail.html'),
|
|
'text' => $this->renderer->render('emails/congratulatoryMssEmail.txt'),
|
|
],
|
|
];
|
|
|
|
$extraParams = [
|
|
'meta' => $this->mailerMetaInfo->getSendingTestMetaInfo(),
|
|
];
|
|
$this->mailerFactory->getDefaultMailer()->send($renderedNewsletter, $toEmailAddress, $extraParams);
|
|
}
|
|
}
|