Symfony2在命令中发送电子邮件

时间:2013-07-04 15:37:37

标签: php symfony console twig

我正在尝试使用我在命令中创建的服务发送电子邮件。在我对安全性进行了一些更改后,我遇到了一些问题。每次运行命令时,都会收到错误:

[Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException]                                                
The security context contains no authentication token. One possible reason may be that there is no firewall configured for this URL. 

我隔离了错误发生的地方,当我尝试渲染电子邮件的模板时。我尝试使用与服务实例化的模板,并使用

从命令传递模板
$this->getContainer()->get('templating')

但他们俩仍然给我同样的错误。

作为附加信息,它不仅仅是一个命令,而是每个发送电子邮件的命令都会出现此错误。

我使用的功能是:

public function createMessage($subject, $to, $template, array $atributes, array $from = array(), $returnPath = 'dev@domain.br', $replyTo = 'no-reply@domain.com.br', $templating = null)
{
    if (count($from) == 0) {
        $from = array('no-reply@domain.com.br' => 'Contact');
    }

    if ($templating == null) {
        $tpl = $this->templating->render($template, $atributes);
    } else {
        $tpl = $templating->render($template, $atributes);
    }

    $this->message = \Swift_Message::newInstance()
            ->setFrom($from)
            ->setReturnPath($returnPath)
            ->setReplyTo($replyTo)
            ->setContentType('text/html')
            ->setSubject($subject)
            ->setTo($to)
            ->setBody($tpl);

    return $this;
}

当我尝试填充$ tpl时发生错误。

我也尝试过这种方式:

$message = \Swift_Message::newInstance()
        ->setSubject('Você foi bloqueado no sistema')
        ->setFrom('contato@mysite.com.br')
        ->setTo($usuario->getEmail())
        ->setCharset('UTF-8')
        ->setContentType('text/html')
        ->setBody($this->getContainer()->get('templating')->render('MyBundle:Email:template.twig'));
    $this->getContainer()->get('mailer')->send($message);

我的security.yml

jms_security_extra:
    secure_all_services: false
    expressions: true

security:
    role_hierarchy:
        ROLE_ADMIN: [ROLE_USUARIO]
        ROLE_USUARIO:

    providers:
        user_db:
            entity: { class: MyNamespace\UserBundle\Entity\Usuario }

    encoders:
        MyNamespace\UserBundle\Entity\Usuario: plaintext

    firewalls:
        secured_area:
            switch_user: { role: ROLE_ADMIN, parameter: _become_him }
            pattern:    ^/
            anonymous: ~
            form_login:
                check_path: /login_check
                login_path: /login
            logout:
                path:   /logout
                target: /login

    access_control:
      - { path: ^/_wdt/, role: IS_AUTHENTICATED_ANONYMOUSLY }
      - { path: ^/_profiler/, role: IS_AUTHENTICATED_ANONYMOUSLY }

      - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }

      - { path: ^/adm, roles: ROLE_ADMIN }
      - { path: ^/, roles: ROLE_USUARIO }

模板:

<!DOCTYPE html>
<html>
<head>
    <title>
        {% block title '' %}
    </title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
    <table style="width: 680px; font-family: Helvetica, Arial, sans-serif; color: #575756; font-size: 16px;">
        <tr>
            <td style="padding: 0 70px;">
                <img src="/img/email/logo.png" style="width: 160px;"/>
            </td>
        </tr>

        <tr style="background-color: #649cb0; line-height: 7px;">
            <td>&nbsp;</td>
        </tr>

        <tr>
            <td style="padding: 0 70px 20px 70px;">
                {% block content '' %}
            </td>
        </tr>

        <tr style="background-color: #649cb0;">
            <td style="padding: 0 70px;">
                <table style="width: 100%; margin-top: 5px;">
                    <tr>
                        <td style="width: 50%; text-align: left;">
                            <a target="_blank">
                                <img src="/img/email/acessar.png"  title="Acessar o sistema" alt="Acessar o sistema" />
                            </a>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
</body>

使用上述模板:     {%extends&#39; NC8DigitalCRMBundle:Email:layout.html.twig&#39; %}

{% block title %}Report{% endblock %}

{% block content %}
<h3 style="font-size: 20px; font-weight: normal;">Hello,</h3>

{% if qtdNaoDistribuidas > 0 %}
    <h4>Problems found.</h4>
{% else %}
    <h4>Everything is ok.</h4>
{% endif %}

{% endblock %}

这是我的电子邮件服务:

mybundle.email:
    class: MyNameSpace\MyBundle\Services\SendEmail
    arguments: [ "@doctrine.orm.entity_manager", "@templating", "@mailer", "@service_container" ]

0 个答案:

没有答案