在FOSUserBundle中扩展symfony邮件程序类

时间:2012-02-13 15:00:39

标签: symfony extending fosuserbundle

有谁知道如何在FOSUserBundle中扩展Mailer类?

我正在实施一个非常基本的父母电子邮件检查(所有验证都在表单上完成,以强制输入父母的电子邮件),如果在用户实体上填充了父电子邮件字段,那么它应该将电子邮件发送给不是用户的电子邮件地址。

到目前为止,我已尝试过以下内容:

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);
}
}

它似乎只是忽略了被覆盖的类并使用了默认值,我在测试之前已经清除了缓存。

由于

1 个答案:

答案 0 :(得分:5)

您应该使用扩展邮件类(在src \ 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>

然后在app / config / config.yml中使用此服务作为默认值:

fos_user:
    # ...
    service:
        mailer: my_mailer

仅供参考:我从FOSUserBundle默认邮件程序配置中复制了所有服务的参数。您可以添加自己的参数。您还可以在https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/emails.md

上阅读有关自定义邮件的信息