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

110 lines
3 KiB
PHP
Raw Normal View History

2015-12-29 08:59:46 +00:00
<?php
namespace Wallabag\UserBundle\Mailer;
use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface;
use Scheb\TwoFactorBundle\Mailer\AuthCodeMailerInterface;
2016-01-21 07:53:09 +00:00
use Craue\ConfigBundle\Util\Config;
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
*
* @var \Twig_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;
/**
2016-01-21 11:29:35 +00:00
* 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.
*
2016-01-10 12:01:00 +00:00
* @param \Swift_Mailer $mailer
* @param \Twig_Environment $twig
* @param string $senderEmail
* @param string $senderName
2016-01-21 07:53:09 +00:00
* @param Config $craueConfig Craue\Config instance to get wallabag support url from database
2015-12-29 08:59:46 +00:00
*/
2016-01-21 07:53:09 +00:00
public function __construct(\Swift_Mailer $mailer, \Twig_Environment $twig, $senderEmail, $senderName, Config $craueConfig)
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;
2016-01-21 07:53:09 +00:00
$this->supportUrl = $craueConfig->get('wallabag_support_url');
$this->wallabagUrl = $craueConfig->get('wallabag_url');
2015-12-29 08:59:46 +00:00
}
/**
* Send the auth code to the user via email.
*
* @param TwoFactorInterface $user
*/
public function sendAuthCode(TwoFactorInterface $user)
{
$template = $this->twig->loadTemplate('@WallabagUserBundle/Resources/views/TwoFactor/email_auth_code.html.twig');
2016-01-10 12:01:00 +00:00
$subject = $template->renderBlock('subject', array());
$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
->setTo($user->getEmail())
->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);
}
}