[英]Conditional resetting email templates in FosUserBundle Symfony Framework
我有一個使用FosUserBundle和PugxMultiUserBundle的symfony項目,因為我需要2個用戶類型。 有CmsUsers和Platform播放器。
我需要一種按用戶類型擁有Fos電子郵件模板(重置模板,注冊模板等)的方法。 一個重置模板用於CmsUser,另一個模板用於Player。 注冊也一樣。
發生問題是因為這些模板是在config.yaml中配置的
fos_user:
db_driver: orm
firewall_name: api
user_class: PanelBundle\Entity\User
from_email:
address: '%fos_from_address%'
sender_name: '%fos_from_name%'
service:
mailer: api.custom_mailer
user_manager: pugx_user_manager
registration:
confirmation:
enabled: true
template: 'ApiBundle:Email:confirm.email.twig'
resetting:
retry_ttl: 1800 # After how much seconds (30 min) user can request again pass reset
token_ttl: 604800 # After how much seconds (1 week) user token is valid (inactive in user mailbox)
email:
template: 'ApiBundle:Email:resetting.email.twig'
我需要一種有條件地配置或實現此方法的方法。 如果用戶類型為CmsUser,則加載此模板,否則加載另一個。
<?php
namespace ApiBundle\Mailer;
use FOS\UserBundle\Mailer\TwigSwiftMailer as BaseMailer;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class CustomUserMailer extends BaseMailer
{
public function __construct(\Swift_Mailer $mailer, UrlGeneratorInterface $router, \Twig_Environment $twig, array $parameters)
{
parent::__construct($mailer, $router, $twig, $parameters);
}
/**
* @param string $templateName
* @param array $context
* @param string $fromEmail
* @param string $toEmail
*/
protected function sendMessage($templateName, $context, $fromEmail, $toEmail)
{
// Create a new mail message.
$message = \Swift_Message::newInstance();
$context['images']['top']['src'] = $message->embed(\Swift_Image::fromPath(
__DIR__.'/../../../web/assets/img/email/header.jpg'
));
$context['images']['bottom']['src'] = $message->embed(\Swift_Image::fromPath(
__DIR__.'/../../../web/assets/img/email/footer.jpg'
));
$context = $this->twig->mergeGlobals($context);
$template = $this->twig->loadTemplate($templateName);
$subject = $template->renderBlock('subject', $context);
$textBody = $template->renderBlock('body_text', $context);
$htmlBody = $template->renderBlock('body_html', $context);
$message->setSubject($subject);
$message->setFrom($fromEmail);
$message->setTo($toEmail);
$message->setBody($htmlBody, 'text/html');
$message->addPart($textBody.'text/plain');
$this->mailer->send($message);
}
}
HY,
如果您可以停用發送電子郵件的FosUserBundle並創建做同樣事情的自定義偵聽器,請獲取FosUserBundle偵聽器的代碼並對其進行修改(例如:添加if($ user instanceof CmsUser等),以便按類型發送自定義電子郵件。
檢查/ vendor / friendsofsymfony / user-bundle / EventListener中的偵聽器(EmailConfirmationListener和ResetListener)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.