Commit 11bd0c2f authored by Mat's avatar Mat

Envoi réussi d'un second mail : passe par un service

parent 6bc3f282
...@@ -33,3 +33,7 @@ services: ...@@ -33,3 +33,7 @@ services:
# PotageBundle\Service\ExampleService: # PotageBundle\Service\ExampleService:
# arguments: # arguments:
# $someArgument: 'some_value' # $someArgument: 'some_value'
PotageBundle\Services\Newsletter:
#arguments: ["email", "string", "text"]
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
namespace PotageBundle\Command; namespace PotageBundle\Command;
use PotageBundle\Services\Newsletter;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
...@@ -19,39 +19,36 @@ class SendMailCommand extends ContainerAwareCommand ...@@ -19,39 +19,36 @@ class SendMailCommand extends ContainerAwareCommand
->setDescription("Envoie un biess mail !"); ->setDescription("Envoie un biess mail !");
} }
public $mailer; public $newsletter;
/** /**
* SendMailCommand constructor. * SendMailCommand constructor.
* *
* @param \Swift_Mailer $mailer * @param Newsletter $newsletter
*/ */
public function __construct(\Swift_Mailer $mailer) public function __construct(Newsletter $newsletter)
{ {
parent::__construct(); 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 InputInterface $input
* @param OutputInterface $output * @param OutputInterface $output
*/ */
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
{ {
$message = (new \Swift_Message('Hello Email'))
->setFrom('postmaster@potage.domainepublic.site') $ret = $this->newsletter->sendMail(
->setTo('mat@collectifs.net') 'mat@collectifs.net',
->setBody( '[Potage] envoi depuis la commande',
'coucou, ceci est un test', 'text/html' 'body du mail de test'
) );
; $retMsg = ( $ret === 1 ) ? "\e[1;32mDone\e[0m\n" : "\e[1;31mError\e[0m\n";
$this->mailer->send($message);
$output->writeln('un mail envoyé'); $output->writeln('Sending mail .. '. $retMsg);
} }
} }
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
namespace PotageBundle\Controller; namespace PotageBundle\Controller;
use PotageBundle\Services\Newsletter;
class DefaultController extends MasterController class DefaultController extends MasterController
{ {
...@@ -16,11 +16,21 @@ class DefaultController extends MasterController ...@@ -16,11 +16,21 @@ class DefaultController extends MasterController
} }
/** /**
* @param Newsletter $newsletter
* @return \Symfony\Component\HttpFoundation\Response * @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
));
} }
/** /**
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
{% block content %} {% block content %}
<h1>{{ block('title') }}</h1> <h1>{{ block('title') }}</h1>
{{ retour }}
{% endblock %} {% endblock %}
{% block customJS %} {% block customJS %}
......
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
Coucou
un mail de test
</body>
</html>
\ No newline at end of file
<?php
namespace PotageBundle\Services;
class Newsletter
{
private $mailer;
/**
* Newsletter constructor.
*
* @param \Swift_Mailer $mailer
*/
public function __construct(\Swift_Mailer $mailer)
{
$this->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
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