douhui1630 2018-09-23 13:17
浏览 74
已采纳

在命令中使用SwiftMailer - Symfon 4.1

I created a simple command witch takes url and look if website is up if not send email to me, but the problem is i don`t know how to add SwiftMailer to Command execute.

    <?php

namespace App\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use App\Entity\Products;
use App\Entity\Settings;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;

class CheckwebsitesCommand extends ContainerAwareCommand
{
    protected static $defaultName = 'checkwebsites';

    protected function configure()
    {
        $this
            ->setName('app:checkwebsites')
            ->setDescription('Add a short description for your command')

        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output, \Swift_Mailer $mailer)
    {

if i add a:

protected function execute(InputInterface $input, OutputInterface $output, \Swift_Mailer $mailer)
{

then i have a error:

 Warning: Declaration of App\Command\CheckwebsitesCommand::execute(Symfony\Component\Console\Input\InputInterface $input, Symfony\Component\Console\Output\OutputInterface $output, $id)
  should be compatible with Symfony\Component\Console\Command\Command::execute(Symfony\Component\Console\Input\InputInterface $input, Symfony\Component\Console\Output\OutputInterface $ou
  tput)

i cant understand this "Commands" thing, why i cany just attach bundles witch, as i want ? I wanna use Commands to setup crons.

******EDIT

   foreach($websites as $website) {
                       $www = $website->getJson();
                       $online = url_test($www['website']['www']);
                       if(!$online) {
                         $message = (new \Swift_Message('Nie działa strona '.$www['website']['www'].' !'))
                         ->setFrom(''.$emailform.'')
                         ->setTo(''.$emailform.'')
                         ->setBody(
                           $this->templating->renderView(
                             'emails/websitenoworking.html.twig',
                             array(
                               'www' => $website['website']['www'],
                             )
                           ),
                           'text/html'
                         );
                         $mailer->send($message);
                       }
                     }
  • 写回答

2条回答 默认 最新

  • dongtun1683 2018-09-23 14:16
    关注

    Just to follow up a bit on my injection comment, given that you are using S4.1 you should probably avoid using the ContainerAwareCommand as well. Using the container as a service locator is so last year. Strict dependency injection is the recommended approach.

    namespace App\Command;
    
    use Twig\Environment;
    use Doctrine\ORM\EntityManagerInterface;
    use Symfony\Component\Console\Command\Command;
    use Symfony\Component\Console\Input\InputInterface;
    use Symfony\Component\Console\Output\OutputInterface;
    
    class MailerCommand extends Command
    {
        private $entityManager;
        private $mailer;
        private $twig;
    
        public function __construct(
            EntityManagerInterface $entityManager, 
            \Swift_Mailer $mailer,
            Environment $twig
        )
        {
            parent::__construct();
            $this->entityManager = $entityManager;
            $this->mailer = $mailer;
            $this->twig = $twig;
        }
        protected function configure()
        {
            $this
                ->setName('app:checkwebsites')
                ->setDescription('Add a short description for your command')
            ;
        }
        protected function execute(InputInterface $input, OutputInterface $output)
        {
            echo "Hello there
    ";
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵