2012-02-13 2 views
2

Кто-нибудь знает, как вы можете расширить класс Mailer в FOSUserBundle?Расширение класса почтовой программы Symfony в FOSUserBundle

Я выполняю очень простой родительский контроль электронной почты (все проверки выполняются в форме, чтобы принудительно ввести адрес электронной почты родителя), если родительское поле электронной почты заполнено на пользовательском объекте, тогда оно shoudl отправит электронное письмо это адрес не по электронной почте пользователя.

Я попытался следующим до сих пор:

namespace SSERugby\UserBundle\Mailer; 

use FOS\UserBundle\Mailer\Mailer as BaseMailer; 
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; 
use Symfony\Component\Routing\RouterInterface; 

class Mailer extends BaseMailer 
{ 
public function sendConfirmationEmailMessage(UserInterface $user) 
{ 
    $email = $user->getEmail(); 

    $parentEmail = $user->getParentEmail(); 

    if(isset($parentEmail)&&(trim($parentEmail)!='')){ 
     $email = $parentEmail; 
    } 

    $template = $this->parameters['confirmation.template']; 
    $url = $this->router->generate('fos_user_registration_confirm', array('token' => $user->getConfirmationToken()), true); 
    $rendered = $this->templating->render($template, array(
     'user' => $user, 
     'confirmationUrl' => $url 
    )); 
    $this->sendEmailMessage($rendered, $this->parameters['from_email']['confirmation'], $email); 
} 
} 

кажется просто игнорировать переопределенный класс, хотя и использовать по умолчанию, я очистил кэш перед испытанием.

Благодаря

ответ

5

Вы должны создать новый сервис с расширенным классом почты (в ЦСИ \ SSERugby \ UserBundle \ Resources \ Config \ services.xml) как:

<service id="my_mailer" class="SSERugby\UserBundle\Mailer\Mailer" public="true"> 
    <argument type="service" id="mailer" /> 
    <argument type="service" id="router" /> 
    <argument type="service" id="templating" /> 
    <argument type="collection"> 
     <argument key="confirmation.template">%fos_user.registration.confirmation.template%</argument> 
     <argument key="resetting.template">%fos_user.resetting.email.template%</argument> 
     <argument key="from_email" type="collection"> 
      <argument key="confirmation">%fos_user.registration.confirmation.from_email%</argument> 
      <argument key="resetting">%fos_user.resetting.email.from_email%</argument> 
     </argument> 
    </argument> 
</service> 

, а затем в приложение/Config/config.yml использовать эту услугу по умолчанию:

fos_user: 
    # ... 
    service: 
     mailer: my_mailer 

FYI: Я скопировал аргументы все услуги от FOSUserBundle почтовой программы по умолчанию конфигурации. Вы можете добавить к нему свои собственные параметры. Также вы можете прочитать о пользовательских почтовых отправлениях по адресу https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/emails.md

Смежные вопросы