diff --git a/app/config/services.yml b/app/config/services.yml index 7006f31fd6ac9d3cbd68b9150432da193b605f73..ca2f82ba3780a8e34141d3bea2e63eb1f1b40e75 100755 --- a/app/config/services.yml +++ b/app/config/services.yml @@ -33,3 +33,7 @@ services: # PotageBundle\Service\ExampleService: # arguments: # $someArgument: 'some_value' + + PotageBundle\Services\Newsletter: + #arguments: ["email", "string", "text"] + diff --git a/src/PotageBundle/Command/SendMailCommand.php b/src/PotageBundle/Command/SendMailCommand.php index f513b8ecdc277a07d4cb1fa2ec4fb5635d681548..6aa6776dae59f68d3b9eb087637e690776069b39 100755 --- a/src/PotageBundle/Command/SendMailCommand.php +++ b/src/PotageBundle/Command/SendMailCommand.php @@ -2,7 +2,7 @@ namespace PotageBundle\Command; - +use PotageBundle\Services\Newsletter; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -19,39 +19,36 @@ class SendMailCommand extends ContainerAwareCommand ->setDescription("Envoie un biess mail !"); } - public $mailer; - + public $newsletter; + /** * SendMailCommand constructor. * - * @param \Swift_Mailer $mailer + * @param Newsletter $newsletter */ - public function __construct(\Swift_Mailer $mailer) + public function __construct(Newsletter $newsletter) { parent::__construct(); - $this->mailer = $mailer; + $this->newsletter = $newsletter; } /** - * Vérifie que les offres en cours n'ont pas expiré. - * Si c'est le cas, le statut est changé et un mail est envoyé aux maraichers. - * * @param InputInterface $input * @param OutputInterface $output */ protected function execute(InputInterface $input, OutputInterface $output) { - $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('un mail envoyé'); + + $ret = $this->newsletter->sendMail( + 'mat@collectifs.net', + '[Potage] envoi depuis la commande', + 'body du mail de test' + ); + $retMsg = ( $ret === 1 ) ? "\e[1;32mDone\e[0m\n" : "\e[1;31mError\e[0m\n"; + + $output->writeln('Sending mail .. '. $retMsg); } } diff --git a/src/PotageBundle/Controller/DefaultController.php b/src/PotageBundle/Controller/DefaultController.php index c9ddb3f2e3a269ec5012d0017faffdd639ff0c4e..f9100d02806cd2dfa0f2acc94852df39c3f03e3c 100755 --- a/src/PotageBundle/Controller/DefaultController.php +++ b/src/PotageBundle/Controller/DefaultController.php @@ -2,7 +2,7 @@ namespace PotageBundle\Controller; - +use PotageBundle\Services\Newsletter; class DefaultController extends MasterController { @@ -16,11 +16,21 @@ class DefaultController extends MasterController } /** + * @param Newsletter $newsletter * @return \Symfony\Component\HttpFoundation\Response */ - public function testAction() + public function testAction(Newsletter $newsletter) { - return $this->render('@Potage/Default/test.html.twig'); + + $ret = $newsletter->sendMail( + 'mat@collectifs.net', + '[Potage] envoyé depuis la page de test', + 'body du mail de test' + ); + + return $this->render('@Potage/Default/test.html.twig', array( + 'retour'=> $ret + )); } /** diff --git a/src/PotageBundle/Resources/views/Default/test.html.twig b/src/PotageBundle/Resources/views/Default/test.html.twig index 501c936b7ea5d172a4d40a177956144f884bf74a..96cf10c26f42bed9c829c13463492882a41b100a 100755 --- a/src/PotageBundle/Resources/views/Default/test.html.twig +++ b/src/PotageBundle/Resources/views/Default/test.html.twig @@ -6,7 +6,7 @@ {% block content %}

{{ block('title') }}

- + {{ retour }} {% endblock %} {% block customJS %} diff --git a/src/PotageBundle/Resources/views/Mail/bodyMail.html.twig b/src/PotageBundle/Resources/views/Mail/bodyMail.html.twig new file mode 100644 index 0000000000000000000000000000000000000000..fa774a7ce750b9f4e29818eeec0ae4913562acc3 --- /dev/null +++ b/src/PotageBundle/Resources/views/Mail/bodyMail.html.twig @@ -0,0 +1,14 @@ + + + + + + + Document + + + Coucou + un mail de test + + \ No newline at end of file diff --git a/src/PotageBundle/Services/Newsletter.php b/src/PotageBundle/Services/Newsletter.php new file mode 100644 index 0000000000000000000000000000000000000000..1d1c09e68f0cd6a4a4de762e2344af24925a6ea1 --- /dev/null +++ b/src/PotageBundle/Services/Newsletter.php @@ -0,0 +1,48 @@ +mailer = $mailer; + } + + /** + * @param $to + * @param $subject + * @param $body + * @return mixed + */ + public function sendMail($to, $subject, $body) + { + + $mailer = $this->mailer; + + $from = ['postmaster@potage.domainepublic.site' => 'Potage']; + $message = (new \Swift_Message()) + + ->setFrom($from) + ->setTo($to) + ->setSubject($subject) + ->setBody($body + //$this->render( + // '@Potage/Mail/bodyMail.html.twig', + // array('name' => 'coucou') + //) + ); + + //$attachment = \Swift_Attachment::fromPath(); + + return $mailer->send($message); + } +} \ No newline at end of file