Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony2在命令中发送电子邮件_Php_Symfony_Console_Twig - Fatal编程技术网

Php Symfony2在命令中发送电子邮件

Php Symfony2在命令中发送电子邮件,php,symfony,console,twig,Php,Symfony,Console,Twig,我正在尝试使用我在命令中提供的服务发送电子邮件。在对安全性进行了一些更改之后,我遇到了一些问题。每次运行该命令时,都会出现以下错误: [Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException] The security context contains no authentication

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

[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);
我的安全

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>

当您使用该命令时,您既没有请求,也没有安全上下文(因为您没有任何服务器请求)。问题可能出现在您对安全性进行更改的地方。但是你做了什么改变?我刚刚添加了我的安全性。yml@YoannCh。谢谢你可以添加你的模板@pedrochan吗?我添加了父模板tempalte和被调用的模板@约安奇。谢谢嗯,我真的看不出这个错误是从哪里来的。需要对它进行更多的调查。
{% 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" ]