1
0
Fork 0
mirror of https://github.com/wallabag/wallabag.git synced 2025-10-05 19:31:02 +00:00

Replace SwiftMailer by Symfony Mailer

This commit is contained in:
Jeremy Benoist 2022-12-15 12:02:52 +01:00
parent 9c16dd7bd1
commit 32661f380c
No known key found for this signature in database
GPG key ID: 7168D5DD29F38552
13 changed files with 148 additions and 334 deletions

View file

@ -140,7 +140,6 @@
<tr><td>smalot/pdfparser</td><td>GPL-3.0</td></tr>
<tr><td>sonata-project/google-authenticator</td><td>MIT</td></tr>
<tr><td>stof/doctrine-extensions-bundle</td><td>MIT</td></tr>
<tr><td>swiftmailer/swiftmailer</td><td>MIT</td></tr>
<tr><td>symfony/assetic-bundle</td><td>MIT</td></tr>
<tr><td>symfony/monolog-bundle</td><td>MIT</td></tr>
<tr><td>All of Symfony</td><td>MIT-licenced</td></tr>

View file

@ -4,6 +4,9 @@ namespace Wallabag\UserBundle\Mailer;
use Scheb\TwoFactorBundle\Mailer\AuthCodeMailerInterface;
use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Twig\Environment;
/**
@ -13,9 +16,9 @@ use Twig\Environment;
class AuthCodeMailer implements AuthCodeMailerInterface
{
/**
* SwiftMailer.
* Mailer.
*
* @var \Swift_Mailer
* @var MailerInterface
*/
private $mailer;
@ -55,14 +58,12 @@ class AuthCodeMailer implements AuthCodeMailerInterface
private $wallabagUrl;
/**
* Initialize the auth code mailer with the SwiftMailer object.
*
* @param string $senderEmail
* @param string $senderName
* @param string $supportUrl wallabag support url
* @param string $wallabagUrl wallabag instance url
*/
public function __construct(\Swift_Mailer $mailer, Environment $twig, $senderEmail, $senderName, $supportUrl, $wallabagUrl)
public function __construct(MailerInterface $mailer, Environment $twig, $senderEmail, $senderName, $supportUrl, $wallabagUrl)
{
$this->mailer = $mailer;
$this->twig = $twig;
@ -92,15 +93,13 @@ class AuthCodeMailer implements AuthCodeMailerInterface
'support_url' => $this->supportUrl,
]);
$message = new \Swift_Message();
$message
->setTo($user->getEmailAuthRecipient())
->setFrom($this->senderEmail, $this->senderName)
->setSubject($subject)
->setBody($bodyText, 'text/plain')
->addPart($bodyHtml, 'text/html')
;
$email = (new Email())
->from(new Address($this->senderEmail, $this->senderName ?? $this->senderEmail))
->to($user->getEmailAuthRecipient())
->subject($subject)
->text($bodyText)
->html($bodyHtml);
$this->mailer->send($message);
$this->mailer->send($email);
}
}

View file

@ -0,0 +1,78 @@
<?php
namespace Wallabag\UserBundle\Mailer;
use FOS\UserBundle\Mailer\TwigSwiftMailer;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Twig\Environment;
/**
* This replace the default mailer from TwigSwiftMailer by symfony/mailer instead of swiftmailer.
*/
class UserMailer extends TwigSwiftMailer
{
/**
* @var MailerInterface
*/
protected $mailer;
/**
* @var UrlGeneratorInterface
*/
protected $router;
/**
* @var Environment
*/
protected $twig;
/**
* @var array
*/
protected $parameters;
public function __construct(MailerInterface $mailer, UrlGeneratorInterface $router, Environment $twig, array $parameters)
{
$this->mailer = $mailer;
$this->router = $router;
$this->twig = $twig;
$this->parameters = $parameters;
}
/**
* @param string $templateName
* @param array $context
* @param array $fromEmail
* @param string $toEmail
*/
protected function sendMessage($templateName, $context, $fromEmail, $toEmail)
{
$template = $this->twig->load($templateName);
$subject = $template->renderBlock('subject', $context);
$textBody = $template->renderBlock('body_text', $context);
$htmlBody = '';
if ($template->hasBlock('body_html', $context)) {
$htmlBody = $template->renderBlock('body_html', $context);
}
$email = (new Email())
->from(new Address(key($fromEmail), current($fromEmail)))
->to($toEmail)
->subject($subject);
if (!empty($htmlBody)) {
$email
->text($textBody)
->html($htmlBody);
} else {
$email->text($textBody);
}
$this->mailer->send($email);
}
}