|
|
@@ -4,20 +4,16 @@ namespace App\Service;
|
|
|
|
|
|
use App\Entity\Access\Access;
|
|
|
use App\Entity\Organization\Organization;
|
|
|
-use App\Message\Command\SendEmail;
|
|
|
use App\Service\Core\ContactPointUtils;
|
|
|
-use Symfony\Component\Messenger\MessageBusInterface;
|
|
|
+use Symfony\Bridge\Twig\Mime\TemplatedEmail;
|
|
|
+use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
|
|
|
+use Symfony\Component\Mailer\MailerInterface;
|
|
|
use Symfony\Component\Mime\Address;
|
|
|
-use Twig\Environment;
|
|
|
-use Twig\Error\LoaderError;
|
|
|
-use Twig\Error\RuntimeError;
|
|
|
-use Twig\Error\SyntaxError;
|
|
|
|
|
|
class MailHub
|
|
|
{
|
|
|
public function __construct(
|
|
|
- private MessageBusInterface $messageBus,
|
|
|
- private Environment $twig,
|
|
|
+ private MailerInterface $mailer,
|
|
|
private string $opentalentNoReplyEmailAddress,
|
|
|
private ContactPointUtils $contactPointUtils,
|
|
|
private \App\Service\Access\Utils $accessUtils
|
|
|
@@ -32,7 +28,7 @@ class MailHub
|
|
|
* @param string $subject
|
|
|
* @param string $template
|
|
|
* @param array $data
|
|
|
- * @throws \Exception
|
|
|
+ * @throws TransportExceptionInterface
|
|
|
*/
|
|
|
public function sendAutomaticEmailTo(Access $access, string $subject, string $template, array $data): void
|
|
|
{
|
|
|
@@ -40,29 +36,25 @@ class MailHub
|
|
|
if ($contactPoint === null || empty($contactPoint->getEmail()) || $contactPoint->getEmailInvalid()) {
|
|
|
throw new \RuntimeException('Access has no principal email address, abort');
|
|
|
}
|
|
|
+ /** @noinspection NullPointerExceptionInspection */
|
|
|
$to = new Address($contactPoint->getEmail(), $access->getPerson()->getFullName());
|
|
|
- $data['_address'] = $to;
|
|
|
|
|
|
- try {
|
|
|
- $content = $this->twig->render('@templates/mail/' . $template . '.html.twig', $data);
|
|
|
- } catch (LoaderError | RuntimeError | SyntaxError $e) {
|
|
|
- throw new \RuntimeException('Error while rendering the email template ', 0, $e);
|
|
|
- }
|
|
|
+ $context = ['_address' => $to];
|
|
|
+ $context = array_merge($context, $data);
|
|
|
+
|
|
|
+ $email = (new TemplatedEmail())
|
|
|
+ ->from($this->opentalentNoReplyEmailAddress)
|
|
|
+ ->to($to)
|
|
|
+ ->subject($subject)
|
|
|
+ ->htmlTemplate('@templates/mail/' . $template . '.html.twig')
|
|
|
+ ->context($context);
|
|
|
|
|
|
- $this->messageBus->dispatch(
|
|
|
- new SendEmail(
|
|
|
- [$this->opentalentNoReplyEmailAddress],
|
|
|
- [$to],
|
|
|
- $subject,
|
|
|
- null,
|
|
|
- $content
|
|
|
- )
|
|
|
- );
|
|
|
+ $this->mailer->send($email);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Sends an automatic 'do-not-reply'-type email to the admin of the organization
|
|
|
- * @throws \Exception
|
|
|
+ * @throws TransportExceptionInterface
|
|
|
*/
|
|
|
public function sendAutomaticEmailToAdmin(Organization $organization, string $subject, string $template, array $data): void
|
|
|
{
|