SendMailCommand.php 3.24 KB
Newer Older
Mat's avatar
Mat committed
1
2
3
4
<?php

namespace PotageBundle\Command;

5
use PotageBundle\Entity\Lettre;
6
use PotageBundle\Services\Newsletter;
Mat's avatar
Mat committed
7
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
8
use Symfony\Component\Console\Input\InputArgument;
Mat's avatar
Mat committed
9
10
11
12
13
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class SendMailCommand extends ContainerAwareCommand
{
14
    
15
    private $mailer;
16
    private $newsletter;
17
    
Mat's avatar
Mat committed
18
19
20
    /**
     * SendMailCommand constructor.
     *
21
     * @param \Swift_Mailer $mailer
22
     * @param Newsletter $newsletter
Mat's avatar
Mat committed
23
     */
24
    public function __construct(\Swift_Mailer $mailer, Newsletter $newsletter)
Mat's avatar
Mat committed
25
26
    {
        parent::__construct();
27
        
28
        $this->mailer = $mailer;
29
        $this->newsletter = $newsletter;
Mat's avatar
Mat committed
30
31
32
    }


33
34
35
36
37
38
39
    /**
     * {@inheritdoc}
     */
    protected function configure()
    {
        $this
            ->setName('potage:mail:send')
40
41
42
43
            ->setDescription("Test d'envoi de mail(s) !")
            ->setHelp('Must precise if send a test mail, a single mail or a mailing
            ')
            ->addArgument('what', InputArgument::REQUIRED, '"test", "single" or "mailing" ?');
44
45
    }

Mat's avatar
Mat committed
46
47
48
    /**
     * @param InputInterface $input
     * @param OutputInterface $output
49
50
51
52
     * @return int|null|void
     * @throws \Twig_Error_Loader
     * @throws \Twig_Error_Runtime
     * @throws \Twig_Error_Syntax
Mat's avatar
Mat committed
53
54
55
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
56

57
58
59
60
61
62
        /**
         * @var Lettre $lettre
         */
        $lettre = $this->getContainer()->get('doctrine')->getManager()
            ->getRepository('PotageBundle:Lettre')
            ->findOneForSend(7);
63

64
        
65
66
        $what = $input->getArgument('what');
        switch ($what) {
67
        
68
69
            case "test":
    
70
                $message = (new \Swift_Message('[Docker][Symfony][cli] Mail de test '))
71
72
73
                    ->setFrom('postmaster@potage.domainepublic.site')
                    ->setTo('mat@collectifs.net')
                    ->setBody(
74
75
76
77
                        'coucou, ceci est un test',
                        'text/html'
                    );
                
78
                $this->mailer->send($message);
79
                $output->writeln('[Docker][Symfony][cli] Mail sent.');
80
81
82
83
84
85
86
                
                break;
                

            case "single":
    
                $to = array( 'email' => 'mat@collectifs.net', 'fullName' => 'Matla', 'token' => null );
87
                $retour = $this->newsletter->sendLettre(
88
                    $to,
89
                    "[Docker][Symfony][cli][infolettre][single] Mail de test",
90
                    $this->newsletter->lettreTobody($lettre)
91
92
93
94
95
96
97
98
99
100
                );
                $retourMsg = ( $retour === 1 ) ? "\e[1;32mSent\e[0m\n" : "\e[1;31mError\e[0m\n";
    
                $output->writeln('Sending mail .. '. $retourMsg);
                
                break;
                

            case "mailing":

101
                $retour = $this->newsletter->sendLettreToGroup($lettre);
102
103
104
105
106
107
                
                for ($i = 0; $i < count($retour); $i++) {
                    $output->writeln($i .' | '. $retour[$i]['to'] .' | '. $retour[$i]['result']);
                }
                
                break;
108
109
        }
        
Mat's avatar
Mat committed
110
111
    }
}