diff options
author | marcinzelent <zelent.marcin@gmail.com> | 2017-12-01 11:06:29 +0100 |
---|---|---|
committer | marcinzelent <zelent.marcin@gmail.com> | 2017-12-01 11:06:29 +0100 |
commit | 7d09b3a6201a175128421d117d4c63495813fa05 (patch) | |
tree | e014a83d47801ea13ce5ee9259082dde0fe43ce0 /src/AppBundle | |
parent | 491eb42cd125b76f96d4f906c3822202d55395a1 (diff) |
Refactored sending of e-mails and made it work.
Diffstat (limited to 'src/AppBundle')
-rw-r--r-- | src/AppBundle/Controller/EmailController.php | 50 | ||||
-rw-r--r-- | src/AppBundle/Controller/HomeController.php | 5 | ||||
-rw-r--r-- | src/AppBundle/Controller/NewDataController.php | 7 | ||||
-rw-r--r-- | src/AppBundle/Utils/EmailSender.php | 42 |
4 files changed, 74 insertions, 30 deletions
diff --git a/src/AppBundle/Controller/EmailController.php b/src/AppBundle/Controller/EmailController.php index 7b6e864..982234e 100644 --- a/src/AppBundle/Controller/EmailController.php +++ b/src/AppBundle/Controller/EmailController.php @@ -9,8 +9,10 @@ namespace AppBundle\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; -use Symfony\Component\HttpFoundation\Response; use Symfony\Bundle\FrameworkBundle\Controller\Controller; +use Swift_SmtpTransport; +use Swift_Mailer; +use Swift_Message; class EmailController extends Controller { @@ -19,31 +21,31 @@ class EmailController extends Controller */ public function sendEmail() { - $message = (new \Swift_Message('Hello Email')) - ->setFrom('***REMOVED***') - ->setTo('***REMOVED***@edu.easj.dk') - ->setBody( - $this->renderView( -// app/Resources/views/Emails/registration.html.twig - 'emails/registration.html.twig', - array('name' => "Test") - ), - 'text/html' - )/* -* If you also want to include a plaintext version of the message -->addPart( -$this->renderView( -'Emails/registration.txt.twig', -array('name' => $name) -), -'text/plain' -) -*/ +// Create the Transport + $transport = (new Swift_SmtpTransport('mail.cock.li', 465, 'ssl')) + ->setUsername('***REMOVED***') + ->setPassword('***REMOVED***') ; -//$mailer->send($message); +// Create the Mailer using your created Transport + $mailer = new Swift_Mailer($transport); -// or, you can also fetch the mailer service this way - $this->get('mailer')->send($message); +// Create a message + $message = (new Swift_Message('Pollutometer warning ' . date('d/m/Y h:i:s'))) + ->setFrom(['***REMOVED***' => 'Pollutometer']) + ->setTo(['***REMOVED***@edu.easj.dk' => 'A name']) + ->setBody($this->renderView( + // templates/emails/warning.html.twig + 'warning.html.twig', + array('name' => 'Test') + ), + 'text/html') + ; + +// Send the message + $result = $mailer->send($message); + + return $this->render('warning.html.twig', array('name' => 'Test')); } + } diff --git a/src/AppBundle/Controller/HomeController.php b/src/AppBundle/Controller/HomeController.php index d8234cf..606d666 100644 --- a/src/AppBundle/Controller/HomeController.php +++ b/src/AppBundle/Controller/HomeController.php @@ -9,13 +9,9 @@ namespace AppBundle\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; -use Symfony\Component\HttpFoundation\Response; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use AppBundle\Utils\Aqi; - - - class HomeController extends Controller { /** @@ -60,7 +56,6 @@ class HomeController extends Controller $max = max($arr); $data['Aqi'] = $max; - return $this->render('default/index.html.twig', $data); } diff --git a/src/AppBundle/Controller/NewDataController.php b/src/AppBundle/Controller/NewDataController.php index f63a7fa..9722cbd 100644 --- a/src/AppBundle/Controller/NewDataController.php +++ b/src/AppBundle/Controller/NewDataController.php @@ -11,6 +11,8 @@ namespace AppBundle\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Symfony\Component\HttpFoundation\Response; use Symfony\Bundle\FrameworkBundle\Controller\Controller; +use AppBundle\Utils\Aqi; +use AppBundle\Utils\EmailSender; class NewDataController extends Controller { @@ -18,7 +20,7 @@ class NewDataController extends Controller * @Route("/latest") */ - public function getLatestData() + public function getLatestData(Aqi $aqi, EmailSender $emailSender) { // Get cURL resource $curl = curl_init(); @@ -37,6 +39,9 @@ class NewDataController extends Controller $response = new Response($data); $response->headers->set('Content-Type', 'application/json'); + $data = json_decode($resp, true); + if($aqi >= 151) $emailSender->sendEmail($data); + return $response; } diff --git a/src/AppBundle/Utils/EmailSender.php b/src/AppBundle/Utils/EmailSender.php new file mode 100644 index 0000000..e6f74c2 --- /dev/null +++ b/src/AppBundle/Utils/EmailSender.php @@ -0,0 +1,42 @@ +<?php +/** + * Created by PhpStorm. + * User: marcin + * Date: 01/12/17 + * Time: 10:47 + */ + +namespace AppBundle\Utils; + +use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; +use Symfony\Bundle\FrameworkBundle\Controller\Controller; +use Swift_SmtpTransport; +use Swift_Mailer; +use Swift_Message; + +class EmailSender extends Controller +{ + public function sendEmail(array $data) + { +// Create the Transport + $transport = (new Swift_SmtpTransport('mail.cock.li', 465, 'ssl')) + ->setUsername('***REMOVED***') + ->setPassword('***REMOVED***') + ; + +// Create the Mailer using your created Transport + $mailer = new Swift_Mailer($transport); + +// Create a message + $message = (new Swift_Message('Pollutometer warning ' . date('d/m/Y h:i:s'))) + ->setFrom(['***REMOVED***' => 'Pollutometer']) + ->setTo(['***REMOVED***@edu.easj.dk' => 'A name']) + ->setBody($this->renderView( + 'emails/warning.html.twig', $data), + 'text/html') + ; + +// Send the message + $result = $mailer->send($message); + } +}
\ No newline at end of file |