wallabag/src/Wallabag/UserBundle/Mailer/AuthCodeMailer.php

107 lines
2.7 KiB
PHP
Raw Normal View History

2015-12-29 08:59:46 +00:00
<?php
namespace Wallabag\UserBundle\Mailer;
use Scheb\TwoFactorBundle\Mailer\AuthCodeMailerInterface;
2017-07-01 07:52:38 +00:00
use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface;
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
{
/**
* SwiftMailer.
*
* @var \Swift_Mailer
*/
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
/**
* 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
2015-12-29 08:59:46 +00:00
*/
public function __construct(\Swift_Mailer $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
{
2022-12-15 11:32:16 +00:00
$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,
]);
2015-12-29 08:59:46 +00:00
$message = new \Swift_Message();
$message
2018-12-02 17:47:34 +00:00
->setTo($user->getEmailAuthRecipient())
2015-12-29 08:59:46 +00:00
->setFrom($this->senderEmail, $this->senderName)
->setSubject($subject)
->setBody($bodyText, 'text/plain')
->addPart($bodyHtml, 'text/html')
2015-12-29 08:59:46 +00:00
;
$this->mailer->send($message);
}
}