forked from netgen/LiveVotingBundle
/
ActivationEmailsCommand.php
87 lines (80 loc) · 3.21 KB
/
ActivationEmailsCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
namespace Netgen\LiveVotingBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class ActivationEmailsCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('email:send')
->setDescription('Send activation emails to all users')
->addOption(
'activation',
null,
InputOption::VALUE_NONE,
'activation'
)
->addOption(
'questions',
null,
InputOption::VALUE_NONE,
'Questionnaire'
)
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$users = $this->getContainer()->get('doctrine')->getRepository('LiveVotingBundle:User')->findAll();
$num = 0;
if ($input->getOption('activation'))
{
foreach ($users as $user) {
$user_email = $user->getEmail();
$emailHash = md5($this->getContainer()->getParameter('email_hash_prefix') . $user_email);
$message = \Swift_Message::newInstance()
->setSubject('Say hello to Summer Camp 2015!')
->setFrom(array('info@netgen.hr' => 'PHP/eZ Publish Summer Camp 2015'))
->setTo($user_email)
->setBody(
$this->getContainer()->get('templating')->render(
'LiveVotingBundle:Email:login.html.twig',
array('emailHash' => $emailHash)
),
'text/html'
);
$this->getContainer()->get('mailer')->send($message);
$num++;
}
$output->writeln('Activation mails have been sent to '.$num.' users');
}
else if ($input->getOption('questions'))
{
foreach ($users as $user) {
$user_email = $user->getEmail();
$emailHash = md5($this->getContainer()->getParameter('email_hash_prefix') . $user_email);
$message = \Swift_Message::newInstance()
->setSubject('Questionnaire')
->setFrom(array('info@netgen.hr' => 'PHP/eZ Publish Summer Camp 2015'))
->setTo($user_email)
->setBody(
$this->getContainer()->get('templating')->render(
'LiveVotingBundle:Email:questions.html.twig',
array('emailHash' => $emailHash)
),
'text/html'
);
$this->getContainer()->get('mailer')->send($message);
$num++;
}
$output->writeln('Questionnaire mails have been sent to '.$num.' users');
}
else
{
$output->writeln('Must choose one optional argument: --activation or --questions');
}
}
}