Actualización

This commit is contained in:
Xes
2025-04-10 12:24:57 +02:00
parent 8969cc929d
commit 45420b6f0d
39760 changed files with 4303286 additions and 0 deletions

View File

@@ -0,0 +1,107 @@
<?php
/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\UserBundle\Mailer;
use FOS\UserBundle\Model\UserInterface;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/**
* @author Thibault Duplessis <thibault.duplessis@gmail.com>
*/
class Mailer implements MailerInterface
{
/**
* @var \Swift_Mailer
*/
protected $mailer;
/**
* @var UrlGeneratorInterface
*/
protected $router;
/**
* @var EngineInterface
*/
protected $templating;
/**
* @var array
*/
protected $parameters;
/**
* Mailer constructor.
*
* @param \Swift_Mailer $mailer
* @param UrlGeneratorInterface $router
* @param EngineInterface $templating
* @param array $parameters
*/
public function __construct($mailer, UrlGeneratorInterface $router, EngineInterface $templating, array $parameters)
{
$this->mailer = $mailer;
$this->router = $router;
$this->templating = $templating;
$this->parameters = $parameters;
}
/**
* {@inheritdoc}
*/
public function sendConfirmationEmailMessage(UserInterface $user)
{
$template = $this->parameters['confirmation.template'];
$url = $this->router->generate('fos_user_registration_confirm', array('token' => $user->getConfirmationToken()), UrlGeneratorInterface::ABSOLUTE_URL);
$rendered = $this->templating->render($template, array(
'user' => $user,
'confirmationUrl' => $url,
));
$this->sendEmailMessage($rendered, $this->parameters['from_email']['confirmation'], (string) $user->getEmail());
}
/**
* {@inheritdoc}
*/
public function sendResettingEmailMessage(UserInterface $user)
{
$template = $this->parameters['resetting.template'];
$url = $this->router->generate('fos_user_resetting_reset', array('token' => $user->getConfirmationToken()), UrlGeneratorInterface::ABSOLUTE_URL);
$rendered = $this->templating->render($template, array(
'user' => $user,
'confirmationUrl' => $url,
));
$this->sendEmailMessage($rendered, $this->parameters['from_email']['resetting'], (string) $user->getEmail());
}
/**
* @param string $renderedTemplate
* @param array|string $fromEmail
* @param array|string $toEmail
*/
protected function sendEmailMessage($renderedTemplate, $fromEmail, $toEmail)
{
// Render the email, use the first line as the subject, and the rest as the body
$renderedLines = explode("\n", trim($renderedTemplate));
$subject = array_shift($renderedLines);
$body = implode("\n", $renderedLines);
$message = (new \Swift_Message())
->setSubject($subject)
->setFrom($fromEmail)
->setTo($toEmail)
->setBody($body);
$this->mailer->send($message);
}
}

View File

@@ -0,0 +1,34 @@
<?php
/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\UserBundle\Mailer;
use FOS\UserBundle\Model\UserInterface;
/**
* @author Thibault Duplessis <thibault.duplessis@gmail.com>
*/
interface MailerInterface
{
/**
* Send an email to a user to confirm the account creation.
*
* @param UserInterface $user
*/
public function sendConfirmationEmailMessage(UserInterface $user);
/**
* Send an email to a user to confirm the password reset.
*
* @param UserInterface $user
*/
public function sendResettingEmailMessage(UserInterface $user);
}

View File

@@ -0,0 +1,40 @@
<?php
/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\UserBundle\Mailer;
use FOS\UserBundle\Model\UserInterface;
/**
* This mailer does nothing.
* It is used when the 'email' configuration is not set,
* and allows to use this bundle without swiftmailer.
*
* @author Thibault Duplessis <thibault.duplessis@gmail.com>
*/
class NoopMailer implements MailerInterface
{
/**
* @param UserInterface $user
*/
public function sendConfirmationEmailMessage(UserInterface $user)
{
// nothing happens.
}
/**
* @param UserInterface $user
*/
public function sendResettingEmailMessage(UserInterface $user)
{
// nothing happens.
}
}

View File

@@ -0,0 +1,122 @@
<?php
/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\UserBundle\Mailer;
use FOS\UserBundle\Model\UserInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/**
* @author Christophe Coevoet <stof@notk.org>
*/
class TwigSwiftMailer implements MailerInterface
{
/**
* @var \Swift_Mailer
*/
protected $mailer;
/**
* @var UrlGeneratorInterface
*/
protected $router;
/**
* @var \Twig_Environment
*/
protected $twig;
/**
* @var array
*/
protected $parameters;
/**
* TwigSwiftMailer constructor.
*
* @param \Swift_Mailer $mailer
* @param UrlGeneratorInterface $router
* @param \Twig_Environment $twig
* @param array $parameters
*/
public function __construct(\Swift_Mailer $mailer, UrlGeneratorInterface $router, \Twig_Environment $twig, array $parameters)
{
$this->mailer = $mailer;
$this->router = $router;
$this->twig = $twig;
$this->parameters = $parameters;
}
/**
* {@inheritdoc}
*/
public function sendConfirmationEmailMessage(UserInterface $user)
{
$template = $this->parameters['template']['confirmation'];
$url = $this->router->generate('fos_user_registration_confirm', array('token' => $user->getConfirmationToken()), UrlGeneratorInterface::ABSOLUTE_URL);
$context = array(
'user' => $user,
'confirmationUrl' => $url,
);
$this->sendMessage($template, $context, $this->parameters['from_email']['confirmation'], (string) $user->getEmail());
}
/**
* {@inheritdoc}
*/
public function sendResettingEmailMessage(UserInterface $user)
{
$template = $this->parameters['template']['resetting'];
$url = $this->router->generate('fos_user_resetting_reset', array('token' => $user->getConfirmationToken()), UrlGeneratorInterface::ABSOLUTE_URL);
$context = array(
'user' => $user,
'confirmationUrl' => $url,
);
$this->sendMessage($template, $context, $this->parameters['from_email']['resetting'], (string) $user->getEmail());
}
/**
* @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);
}
$message = (new \Swift_Message())
->setSubject($subject)
->setFrom($fromEmail)
->setTo($toEmail);
if (!empty($htmlBody)) {
$message->setBody($htmlBody, 'text/html')
->addPart($textBody, 'text/plain');
} else {
$message->setBody($textBody);
}
$this->mailer->send($message);
}
}