2012-02-13 31 views
2

有誰知道如何在FOSUserBundle中擴展Mailer類?在FOSUserBundle中擴展symfony郵件類

我正在實施一個非常基本的父母電子郵件檢查(所有驗證都在表單上完成,以強制父母的電子郵件被輸入),如果父郵件字段填充在用戶實體上,那麼它應該發送電子郵件到該地址不是用戶的電子郵件。

我已經試過到目前爲止以下:

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

您應該擴展的郵件類來創建新的服務(在SRC \ SSERugby \ UserBundle \資源\ 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.yml使用該服務的默認:

fos_user: 
    # ... 
    service: 
     mailer: my_mailer 

FYI:我複製從FOSUserBundle默認郵件配置所有服務的論點。你可以添加你自己的參數。你也可以在https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/emails.md

閱讀關於定製郵寄者