【问题标题】:Symfony mail on Command class - get undefined method named "get"命令类上的 Symfony 邮件 - 获取名为“get”的未定义方法
【发布时间】:2019-07-21 06:42:02
【问题描述】:

我尝试通过 symfony 命令发送邮件(swiftmail)。 这是我的代码:

class CommandMail extends Command
{
    protected static $defaultName = 'app:send-daily-mail';

    protected function configure() {
        $this
            ->setDescription('Send automatic reminders mail everyday.')

            ->setHelp('This command allows you to send automatic reminder mail to Rhys, everyday...');
    }

    protected function execute(InputInterface $input, OutputInterface $output) {
        $message = (new \Swift_Message('test auto mail cron 12 24 TEST'))
                    ->setFrom('xxxxxx.xxxxxxx@gmail.com')
                    ->setTo('wwwww.wwwwwww@gmail.com')
                    ->setBody('test body');

        $this->get('mailer')->send($message);
    }
}

我有以下错误:

在 CommandMail.php 第 54 行: 试图调用类的名为“get”的未定义方法 “AppBundle\Command\CommandMail”。
你的意思是打电话给例如“getAliases”、“getApplication”、 “getDefaultName”、“getDefinition”、“getDescription”、“getHelp”、 “getHelper”、“getHelperSet”、“getName”、“getNativeDefin ition”、 “getProcessedHelp”、“getSynopsis”还是“getUsages”?

我尝试了很多东西(getContainer() ie 和许多其他方法),但没有任何效果。

感谢您的帮助!

(Symfony 3,SMTP gmail)

【问题讨论】:

  • 我尝试编辑我的消息“嗨!!”不工作!理解我是新人:)
  • 你使用的是哪个版本的 Symfony?
  • Symfony 3.4 正常(symfony 3 肯定)
  • 我使用 swiftmailer 和 gmail smtp

标签: symfony email cron command


【解决方案1】:

如果您使用的是 Symfony 4,请通过构造函数注入依赖项:

private $swiftMailerService;

public function __construct(\Swift_Mailer $swiftMailerService)
{
    parent::__construct();
    $this->swiftMailerService = $swiftMailerService;
}

protected function execute(InputInterface $input, OutputInterface $output) {
    $message = (new \Swift_Message('test auto mail cron 12 24 TEST'))
                ->setFrom('xxxxxx.xxxxxxx@gmail.com')
                ->setTo('wwwww.wwwwwww@gmail.com')
                ->setBody('test body');

    $this->swiftMailerService->send($message);
}

【讨论】:

  • 我得到命令类“AppBundle\Command\CommandMail”未正确初始化。你可能忘记调用父构造函数了。
  • parent::__construct(); 添加到您的构造函数中
  • 谢谢你们!使用@AythaNzt 代码和 Zak 更正,它正在工作:) 帮助很大!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-20
  • 2019-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多