1
我试图发送邮件使用与Symfony的后台任务的邮件,所以我的问题是如何将我执行与我的执行功能使用swiftmailer我的命令?在此先感谢发送从cron任务电子邮件symfony的
我想要的swiftmailer是在我的执行方法,这样我就可以根据后台任务
$mail = \Swift_Message::newInstance();
$mail->setFrom('[email protected]')
->setTo('[email protected]')
->setSubject('Email subject')
->setBody('email body, can be swift template')
$this->get('mailer')->send($mail);
我CronTasksRunCommand
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('<comment>Running Cron Tasks...</comment>');
$this->output = $output;
$em = $this->getContainer()->get('doctrine.orm.entity_manager');
$crontasks = $em->getRepository('AppBundle:CronTask')->findAll();
foreach ($crontasks as $crontask) {
// Get the last run time of this task, and calculate when it should run next
$lastrun = $crontask->getLastRun() ? $crontask->getLastRun()->format('U') : 0;
$nextrun = $lastrun + $crontask->getInterval();
// We must run this task if:
// * time() is larger or equal to $nextrun
$run = (time() >= $nextrun);
if ($run) {
$output->writeln(sprintf('Running Cron Task <info>%s</info>', $crontask));
// Set $lastrun for this crontask
$crontask->setLastRun(new \DateTime());
try {
$commands = $crontask->getCommands();
foreach ($commands as $command) {
$output->writeln(sprintf('Executing command <comment>%s</comment>...', $command));
// Run the command
$this->runCommand($command);
}
$output->writeln('<info>SUCCESS</info>');
} catch (\Exception $e) {
$output->writeln('<error>ERROR</error>');
}
// Persist crontask
$em->persist($crontask);
} else {
$output->writeln(sprintf('Skipping Cron Task <info>%s</info>', $crontask));
}
}
// Flush database changes
$em->flush();
$output->writeln('<comment>Done!</comment>');
}