forked from wallabag/wallabag
Move User mailer to Core
This commit is contained in:
@ -1,105 +0,0 @@
|
||||
<?php
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* Custom mailer for TwoFactorBundle email.
|
||||
* It adds a custom template to the email so user won't get a lonely authentication code but a complete email.
|
||||
*/
|
||||
class AuthCodeMailer implements AuthCodeMailerInterface
|
||||
{
|
||||
/**
|
||||
* Mailer.
|
||||
*
|
||||
* @var MailerInterface
|
||||
*/
|
||||
private $mailer;
|
||||
|
||||
/**
|
||||
* Twig to render the html's email.
|
||||
*
|
||||
* @var Environment
|
||||
*/
|
||||
private $twig;
|
||||
|
||||
/**
|
||||
* Sender email address.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $senderEmail;
|
||||
|
||||
/**
|
||||
* Sender name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $senderName;
|
||||
|
||||
/**
|
||||
* Support URL to report any bugs.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $supportUrl;
|
||||
|
||||
/**
|
||||
* Url for the wallabag instance (only used for image in the HTML email template).
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $wallabagUrl;
|
||||
|
||||
/**
|
||||
* @param string $senderEmail
|
||||
* @param string $senderName
|
||||
* @param string $supportUrl wallabag support url
|
||||
* @param string $wallabagUrl wallabag instance url
|
||||
*/
|
||||
public function __construct(MailerInterface $mailer, Environment $twig, $senderEmail, $senderName, $supportUrl, $wallabagUrl)
|
||||
{
|
||||
$this->mailer = $mailer;
|
||||
$this->twig = $twig;
|
||||
$this->senderEmail = $senderEmail;
|
||||
$this->senderName = $senderName;
|
||||
$this->supportUrl = $supportUrl;
|
||||
$this->wallabagUrl = $wallabagUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the auth code to the user via email.
|
||||
*/
|
||||
public function sendAuthCode(TwoFactorInterface $user): void
|
||||
{
|
||||
$template = $this->twig->load('@WallabagUser/TwoFactor/email_auth_code.html.twig');
|
||||
|
||||
$subject = $template->renderBlock('subject', []);
|
||||
$bodyHtml = $template->renderBlock('body_html', [
|
||||
'user' => $user->getName(),
|
||||
'code' => $user->getEmailAuthCode(),
|
||||
'support_url' => $this->supportUrl,
|
||||
'wallabag_url' => $this->wallabagUrl,
|
||||
]);
|
||||
$bodyText = $template->renderBlock('body_text', [
|
||||
'user' => $user->getName(),
|
||||
'code' => $user->getEmailAuthCode(),
|
||||
'support_url' => $this->supportUrl,
|
||||
]);
|
||||
|
||||
$email = (new Email())
|
||||
->from(new Address($this->senderEmail, $this->senderName ?? $this->senderEmail))
|
||||
->to($user->getEmailAuthRecipient())
|
||||
->subject($subject)
|
||||
->text($bodyText)
|
||||
->html($bodyHtml);
|
||||
|
||||
$this->mailer->send($email);
|
||||
}
|
||||
}
|
||||
@ -1,78 +0,0 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user