我尝试通过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('[email protected]')
->setTo('[email protected]')
->setBody('test body');
$this->get('mailer')->send($message);
}
}
我有以下错误:
在CommandMail.php第54行:尝试调用类“AppBundle \ Command \ CommandMail”的名为“get”的未定义方法。 你的意思是打电话,例如“getAliases”,“getApplication”,“getDefaultName”,“getDefinition”,“getDescription”,“getHelp”,“getHelper”,“getHelperSet”,“getName”,“getNativeDefin ition”,“getProcessedHelp”,“getSynopsis”或“ getUsages“?
我尝试了很多东西(getContainer()ie和许多其他东西)但没有什么工作。
谢谢你的帮助 !
(Symfony 3,SMTP gmail)
如果您使用的是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('[email protected]')
->setTo('[email protected]')
->setBody('test body');
$this->swiftMailerService->send($message);
}