Commit 9a94e022 authored by Mat's avatar Mat

Lancer les mails en ligne de commande, options: test, single ou mailing

parent e7414e79
......@@ -2,30 +2,33 @@
namespace PotageBundle\Command;
use PotageBundle\Entity\Groupe;
use PotageBundle\Entity\Lettre;
use PotageBundle\Services\LettreToken;
use PotageBundle\Services\Newsletter;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class SendMailCommand extends ContainerAwareCommand
{
private $mailer;
private $newsletter;
private $lettreToken;
/**
* SendMailCommand constructor.
*
* @param \Swift_Mailer $mailer
* @param Newsletter $newsletter
* @param LettreToken $lettreToken
*/
public function __construct(Newsletter $newsletter, LettreToken $lettreToken)
public function __construct(\Swift_Mailer $mailer, Newsletter $newsletter, LettreToken $lettreToken)
{
parent::__construct();
$this->mailer = $mailer;
$this->newsletter = $newsletter;
$this->lettreToken = $lettreToken;
}
......@@ -38,7 +41,10 @@ class SendMailCommand extends ContainerAwareCommand
{
$this
->setName('potage:mail:send')
->setDescription("Test d'envoi d'une infolettre!");
->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" ?');
}
/**
......@@ -51,18 +57,60 @@ class SendMailCommand extends ContainerAwareCommand
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
/**
* @var Lettre $lettre
*/
$lettre = $this->getContainer()->get('doctrine')->getManager()
->getRepository('PotageBundle:Lettre')
->findOneForSend(7);
$retour = $this->newsletter->sendLettreToGroup($lettre, $this->lettreToken);
$what = $input->getArgument('what');
switch ($what) {
for ($i = 0; $i < count($retour); $i++) {
$output->writeln($i .' | '. $retour[$i]['to'] .' | '. $retour[$i]['result']);
case "test":
$message = (new \Swift_Message('Hello Email'))
->setFrom('postmaster@potage.domainepublic.site')
->setTo('mat@collectifs.net')
->setBody(
'coucou, ceci est un test', 'text/html'
)
;
$this->mailer->send($message);
$output->writeln('Mail sent.');
break;
case "single":
$to = array( 'email' => 'mat@collectifs.net', 'fullName' => 'Matla', 'token' => null );
$retour = $this->newsletter->sendMail(
$to,
"[Potage][cli] envoi de la lettre à une seul destinataire",
$lettre
);
$retourMsg = ( $retour === 1 ) ? "\e[1;32mSent\e[0m\n" : "\e[1;31mError\e[0m\n";
$output->writeln('Sending mail .. '. $retourMsg);
break;
case "mailing":
$retour = $this->newsletter->sendLettreToGroup(
$lettre,
$this->lettreToken
);
for ($i = 0; $i < count($retour); $i++) {
$output->writeln($i .' | '. $retour[$i]['to'] .' | '. $retour[$i]['result']);
}
break;
}
}
......
......@@ -23,6 +23,7 @@ class DefaultController extends MasterController
/**
* TEMPORAIRE une page de test
* envoie une infolettre manuellement lorsqu'on déclenche un submit
* TODO déclenchement de l'envoi lorsque le producteur passe la lettre au statut 'Envoyé'
*
* @param Request $request
* @param Newsletter $newsletter
......@@ -78,6 +79,7 @@ class DefaultController extends MasterController
/**
* La page today affiche le(s) offres disponibles pour un user
* identifié sur base du token passé dans le lien
* TODO les offres en cours, pour un utilisateur connecté
*
* @param Request $request
* @param LettreToken $lettreToken
......@@ -112,7 +114,7 @@ class DefaultController extends MasterController
}
/**
* pour debug
* Debug token
*/
$tokenEncoded = $request->query->get('token');
$tokenDecoded = $lettreToken->decodePotageAccess($tokenEncoded);
......
......@@ -105,13 +105,13 @@ class Newsletter
*
* @param array $to
* @param $subject
* @param $body
* @param Lettre $lettre
* @return int
* @throws \Twig_Error_Loader
* @throws \Twig_Error_Runtime
* @throws \Twig_Error_Syntax
*/
public function sendMail($to, $subject, $body)
public function sendMail($to, $subject, Lettre $lettre)
{
$mailer = $this->container->get('mailer');
$twig = $this->container->get('twig');
......@@ -125,7 +125,7 @@ class Newsletter
$twig->render('@Potage/Mail/bodyMail.html.twig', array(
'to' => $to,
'subject' => $subject,
'body' => $body,
'body' => $this->getBody($lettre),
)),
'text/html'
);
......@@ -148,7 +148,6 @@ class Newsletter
public function sendLettreToGroup(Lettre $lettre, LettreToken $lettreToken)
{
$subject = $lettre->getSubject();
$body = $this->getBody($lettre);
/**
* A partir de l'infolettre on récupère les destinataires
......@@ -176,7 +175,7 @@ class Newsletter
$ret[$i] = array(
'to' => $this->parseMail($to),
'result' => $this->sendMail($to, $subject, $body) === 1 ? 'sent' : 'error'
'result' => $this->sendMail($to, $subject, $lettre) === 1 ? 'sent' : 'error'
);
}
return $ret;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment