尝试使用 SwiftMailer 通过 Gmail 发送电子邮件时出错 [英] Error trying to send email through Gmail using SwiftMailer

查看:39
本文介绍了尝试使用 SwiftMailer 通过 Gmail 发送电子邮件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 Symfony 3 文档 从我的控制器发送电子邮件,但我遇到了一个错误.我确保正确执行上述页面中的每个步骤,但没有用.

I'm following Symfony 3 Documentation to send an email from my controller, but I'm getting an error. I made sure to follow each step from the above page correctly, but no use.

这是我的控制器的样子:

This is how my controller looks like:

<?php

namespace MyBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;

class DefaultController extends Controller
{
    /**
     * @Route("/")
     */
    public function indexAction(\Swift_Mailer $mailer)
    {
        $message = (new \Swift_Message('Hello Email'))
            ->setFrom('send@example.com')
            ->setTo('my.address@gmail.com')
            ->setBody(
                $this->renderView(
                    // app/Resources/views/Emails/registration.html.twig
                    'MyBundle::Emails/registration.html.twig',
                    array('name' => 'John Doe')
                ),
                'text/html'
            );

        $mailer->send($message);

        return new Response('<html<body>Sent</body></html>');
    }
}

这是我得到的错误:

控制器MyBundle\Controller\DefaultController::indexAction()"要求您为$mailer"参数提供一个值.要么参数可为空且未提供空值、未提供默认值或因为在此参数之后存在非可选参数.

Controller "MyBundle\Controller\DefaultController::indexAction()" requires that you provide a value for the "$mailer" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one.

与文档页面相比,我很确定一切都应该如此,所以任何人都可以帮我找到这个错误的根源吗?

Comparing to the documentation page, I'm pretty sure everything is as it should be, so can anyone please help me find the source of this error?

推荐答案

Simply remove $mailer 从你的构造函数中(你不是在服务中,您在控制器中),并且使用 $this->get('mailer')->send($message); 发送.(同样,您在控制器中,您可以访问邮件程序服务,无需尝试将其注入构造函数)

Simply remove the $mailer from your constructor (you are not in a service, you are in a controller) , and use $this->get('mailer')->send($message); to send. (again, you are in a controller, you have access to the mailer service, no need to try and inject it into the constructor)

这篇关于尝试使用 SwiftMailer 通过 Gmail 发送电子邮件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆