wallabag/src/Mailer/AuthCodeMailer.php

106 lines
2.7 KiB
PHP
Raw Normal View History

2015-12-29 08:59:46 +00:00
<?php
2024-02-19 00:30:12 +00:00
namespace Wallabag\Mailer;
2015-12-29 08:59:46 +00:00
use Scheb\TwoFactorBundle\Mailer\AuthCodeMailerInterface;
2017-07-01 07:52:38 +00:00
use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface;
2022-12-15 11:02:52 +00:00
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Twig\Environment;
2015-12-29 08:59:46 +00:00
/**
* 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
{
/**
2022-12-15 11:02:52 +00:00
* Mailer.
2015-12-29 08:59:46 +00:00
*
2022-12-15 11:02:52 +00:00
* @var MailerInterface
2015-12-29 08:59:46 +00:00
*/
private $mailer;
/**
* Twig to render the html's email.
2015-12-29 08:59:46 +00:00
*
2019-06-21 10:54:52 +00:00
* @var Environment
2015-12-29 08:59:46 +00:00
*/
private $twig;
2015-12-29 08:59:46 +00:00
/**
* 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;
2015-12-29 08:59:46 +00:00
/**
* @param string $senderEmail
* @param string $senderName
* @param string $supportUrl wallabag support url
* @param string $wallabagUrl wallabag instance url
2015-12-29 08:59:46 +00:00
*/
2022-12-15 11:02:52 +00:00
public function __construct(MailerInterface $mailer, Environment $twig, $senderEmail, $senderName, $supportUrl, $wallabagUrl)
2015-12-29 08:59:46 +00:00
{
$this->mailer = $mailer;
$this->twig = $twig;
2015-12-29 08:59:46 +00:00
$this->senderEmail = $senderEmail;
$this->senderName = $senderName;
$this->supportUrl = $supportUrl;
$this->wallabagUrl = $wallabagUrl;
2015-12-29 08:59:46 +00:00
}
/**
* Send the auth code to the user via email.
*/
public function sendAuthCode(TwoFactorInterface $user): void
2015-12-29 08:59:46 +00:00
{
2024-02-18 23:03:14 +00:00
$template = $this->twig->load('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,
]);
2022-12-15 11:02:52 +00:00
$email = (new Email())
->from(new Address($this->senderEmail, $this->senderName ?? $this->senderEmail))
->to($user->getEmailAuthRecipient())
->subject($subject)
->text($bodyText)
->html($bodyHtml);
2015-12-29 08:59:46 +00:00
2022-12-15 11:02:52 +00:00
$this->mailer->send($email);
2015-12-29 08:59:46 +00:00
}
}