2

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

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

我的安全.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 'NC8DigitalCRMBundle:Email:layout.html.twig' %}

{% 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" ]
4

0 回答 0