From fad653ed7b2aa770c76230bcfeb98c434a197b9b Mon Sep 17 00:00:00 2001 From: AntonyAntonio Date: Mon, 4 Jul 2016 04:09:32 -0300 Subject: [PATCH 1/7] (Guillermo) Add-email-sender-class --- server/composer.json | 3 ++- server/controllers/user/signup.php | 12 ++++++++++-- server/libs/EmailSender.php | 21 +++++++++++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 server/libs/EmailSender.php diff --git a/server/composer.json b/server/composer.json index bb0af29e..b64cad11 100644 --- a/server/composer.json +++ b/server/composer.json @@ -1,7 +1,8 @@ { "require": { "slim/slim": "~2.0", - "gabordemooij/redbean": "~4.2" + "gabordemooij/redbean": "~4.2", + "phpmailer/phpmailer": "^5.2" }, "require-dev": { "phpunit/phpunit": "5.0.*" diff --git a/server/controllers/user/signup.php b/server/controllers/user/signup.php index a38559d3..990f0a8e 100644 --- a/server/controllers/user/signup.php +++ b/server/controllers/user/signup.php @@ -3,9 +3,11 @@ class SignUpController extends Controller { const PATH = '/signup'; + private $email; + private $password; + public function handler() { - $email = Controller::request('email'); - $password = Controller::request('password'); + $this->requestUserData(); $userId = $this->createNewUserAndRetrieveId($email, $password); @@ -13,6 +15,12 @@ class SignUpController extends Controller { 'userId' => $userId, 'userEmail' => $email )); + + EmailSender::validRegister($email); + } + public function requestUserData(){ + $this->email = Controller::request('email'); + $this->password = Controller::request('password'); } public function createNewUserAndRetrieveId($email, $password) { diff --git a/server/libs/EmailSender.php b/server/libs/EmailSender.php new file mode 100644 index 00000000..895d1f79 --- /dev/null +++ b/server/libs/EmailSender.php @@ -0,0 +1,21 @@ +addAddress($mail); + + $newMail->Subject = "You Have Been register successfully"; + + if(!$newMail->send()) + { + echo "Mailer Error: " . $newMail->ErrorInfo; + } + else + { + echo "Message has been sent successfully"; + } + } +} \ No newline at end of file From 94743f58ee099d02b0c2a4030b980568ca4a8438 Mon Sep 17 00:00:00 2001 From: AntonyAntonio Date: Mon, 4 Jul 2016 15:47:28 -0300 Subject: [PATCH 2/7] (Guillermo) Add-email-sender-class 2 --- server/controllers/user/signup.php | 2 +- server/libs/EmailSender.php | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/server/controllers/user/signup.php b/server/controllers/user/signup.php index 990f0a8e..13d2fded 100644 --- a/server/controllers/user/signup.php +++ b/server/controllers/user/signup.php @@ -16,7 +16,7 @@ class SignUpController extends Controller { 'userEmail' => $email )); - EmailSender::validRegister($email); + EmailSender::validRegister($this->email); } public function requestUserData(){ $this->email = Controller::request('email'); diff --git a/server/libs/EmailSender.php b/server/libs/EmailSender.php index 895d1f79..b29981b1 100644 --- a/server/libs/EmailSender.php +++ b/server/libs/EmailSender.php @@ -1,10 +1,12 @@ From = "Admin@opensupports.com"; + $newMail->addAddress($mail); $newMail->Subject = "You Have Been register successfully"; From 2a5fbae490ba4574b2a80fce18ce92d0f2e5a214 Mon Sep 17 00:00:00 2001 From: ivan Date: Fri, 8 Jul 2016 18:00:52 -0300 Subject: [PATCH 3/7] Ivan - Fix email sender issues [skip ci] --- server/controllers/user/signup.php | 4 ++-- server/index.php | 1 + server/libs/EmailSender.php | 5 ++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/server/controllers/user/signup.php b/server/controllers/user/signup.php index 13d2fded..1b3d7dc5 100644 --- a/server/controllers/user/signup.php +++ b/server/controllers/user/signup.php @@ -9,11 +9,11 @@ class SignUpController extends Controller { public function handler() { $this->requestUserData(); - $userId = $this->createNewUserAndRetrieveId($email, $password); + $userId = $this->createNewUserAndRetrieveId($this->email, $this->password); Response::respondSuccess(array( 'userId' => $userId, - 'userEmail' => $email + 'userEmail' => $this->email )); EmailSender::validRegister($this->email); diff --git a/server/index.php b/server/index.php index d4617e88..8ea3e907 100644 --- a/server/index.php +++ b/server/index.php @@ -14,6 +14,7 @@ $app = new \Slim\Slim(); include_once 'libs/Controller.php'; include_once 'libs/ControllerGroup.php'; include_once 'libs/Hashing.php'; +include_once 'libs/EmailSender.php'; // LOAD MODELS spl_autoload_register(function ($class) { diff --git a/server/libs/EmailSender.php b/server/libs/EmailSender.php index b29981b1..14367856 100644 --- a/server/libs/EmailSender.php +++ b/server/libs/EmailSender.php @@ -2,10 +2,9 @@ class EmailSender { public static function validRegister($mail) { - $newMail = new PHPMailer; - $mail->From = "Admin@opensupports.com"; + $mail->From = "admin@opensupports.com"; $newMail->addAddress($mail); @@ -13,7 +12,7 @@ class EmailSender { if(!$newMail->send()) { - echo "Mailer Error: " . $newMail->ErrorInfo; + Response::respondError("Mailer Error: " . $newMail->ErrorInfo); } else { From 8b810f7bd5d323cc98e1fd6f3d5366b0a14630c7 Mon Sep 17 00:00:00 2001 From: ivan Date: Thu, 14 Jul 2016 19:06:47 -0300 Subject: [PATCH 4/7] Ivan - Add MailTemplate and MailSender --- server/controllers/user/signup.php | 48 ++++++++++++++++++++++-------- server/index.php | 2 +- server/libs/EmailSender.php | 22 -------------- server/libs/MailSender.php | 46 ++++++++++++++++++++++++++++ server/models/DataStore.php | 4 ++- server/models/MailTemplate.php | 46 ++++++++++++++++++++++++++++ 6 files changed, 131 insertions(+), 37 deletions(-) delete mode 100644 server/libs/EmailSender.php create mode 100644 server/libs/MailSender.php create mode 100644 server/models/MailTemplate.php diff --git a/server/controllers/user/signup.php b/server/controllers/user/signup.php index 947b2fb9..26757658 100644 --- a/server/controllers/user/signup.php +++ b/server/controllers/user/signup.php @@ -2,6 +2,9 @@ class SignUpController extends Controller { const PATH = '/signup'; + + private $userEmail; + private $userPassword; public function validations() { return [ @@ -11,26 +14,45 @@ class SignUpController extends Controller { } public function handler() { - $email = Controller::request('email'); - $password = Controller::request('password'); + $this->setRequestData(); - $userId = $this->createNewUserAndRetrieveId($email, $password); + try { + $userId = $this->createNewUserAndRetrieveId(); + $this->sendRegistrationMail(); - EmailSender::validRegister($email); + Response::respondSuccess([ + 'userId' => $userId, + 'userEmail' => $this->userEmail + ]); + } catch (Exception $e) { + Response::respondError($e->getMessage()); + } - Response::respondSuccess(array( - 'userId' => $userId, - 'userEmail' => $email - )); + } + + public function setRequestData() { + $this->userEmail = Controller::request('email'); + $this->userPassword = Controller::request('password'); } - public function createNewUserAndRetrieveId($email, $password) { + public function createNewUserAndRetrieveId() { $userInstance = new User(); - $userInstance->setProperties(array( - 'email' => $email, - 'password' => Hashing::hashPassword($password) - )); + + $userInstance->setProperties([ + 'email' => $this->userEmail, + 'password' => Hashing::hashPassword($this->userPassword) + ]); return $userInstance->store(); } + + public function sendRegistrationMail() { + $mailSender = new MailSender(); + + $mailSender->setTemplate(MailTemplate::USER_SIGNUP, [ + 'to' => $this->userEmail + ]); + + $mailSender->send(); + } } diff --git a/server/index.php b/server/index.php index 5ce7a36f..04edd2e7 100644 --- a/server/index.php +++ b/server/index.php @@ -14,7 +14,7 @@ $app = new \Slim\Slim(); include_once 'libs/Controller.php'; include_once 'libs/ControllerGroup.php'; include_once 'libs/Hashing.php'; -include_once 'libs/EmailSender.php'; +include_once 'libs/MailSender.php'; // LOAD MODELS spl_autoload_register(function ($class) { diff --git a/server/libs/EmailSender.php b/server/libs/EmailSender.php deleted file mode 100644 index 14367856..00000000 --- a/server/libs/EmailSender.php +++ /dev/null @@ -1,22 +0,0 @@ -From = "admin@opensupports.com"; - - $newMail->addAddress($mail); - - $newMail->Subject = "You Have Been register successfully"; - - if(!$newMail->send()) - { - Response::respondError("Mailer Error: " . $newMail->ErrorInfo); - } - else - { - echo "Message has been sent successfully"; - } - } -} \ No newline at end of file diff --git a/server/libs/MailSender.php b/server/libs/MailSender.php new file mode 100644 index 00000000..51026512 --- /dev/null +++ b/server/libs/MailSender.php @@ -0,0 +1,46 @@ +mailOptions['from'] = ''; + + //SMTP Options + $this->mailOptions['smtp_host'] = ''; + $this->mailOptions['smtp_port'] = 0; + $this->mailOptions['smtp_user'] = ''; + $this->mailOptions['smtp_pass'] = ''; + } + + public function setTemplate($type, $config) { + $mailTemplate = MailTemplate::getTemplate($type); + $compiledMailContent = $mailTemplate->compile($config); + + $this->mailOptions = array_merge($this->mailOptions, $compiledMailContent); + } + + public function send() { + $mailer = new PHPMailer(); + + $mailer->From = $this->mailOptions['from']; + $mailer->addAddress($this->mailOptions['to']); + $mailer->Subject = $this->mailOptions['subject']; + $mailer->Body = $this->mailOptions['body']; + + //$mailer->SMTPDebug = 3; + $mailer->isSMTP(); + $mailer->SMTPAuth = true; + $mailer->Host = $this->mailOptions['smtp_host']; + $mailer->Port = $this->mailOptions['smtp_port']; + $mailer->Username = $this->mailOptions['smtp_user']; + $mailer->Password = $this->mailOptions['smtp_pass']; + $mailer->SMTPSecure = "tls"; + $mailer->Timeout = 1000; + + if ($mailer->smtpConnect()) { + $mailer->send(); + } + } +} \ No newline at end of file diff --git a/server/models/DataStore.php b/server/models/DataStore.php index 7fe921f1..b316ad84 100644 --- a/server/models/DataStore.php +++ b/server/models/DataStore.php @@ -4,7 +4,9 @@ use RedBeanPHP\Facade as RedBean; abstract class DataStore { protected $_bean; - abstract protected function getDefaultProps(); + public function getDefaultProps() { + return []; + } public static function getDataStore($value, $property = 'id') { $bean = RedBean::findOne(static::TABLE, static::validateProp($property) . ' =:value', array( diff --git a/server/models/MailTemplate.php b/server/models/MailTemplate.php new file mode 100644 index 00000000..27742437 --- /dev/null +++ b/server/models/MailTemplate.php @@ -0,0 +1,46 @@ +setProperties([ + 'type' => $type, + 'subject' => 'Test Subject for {{to}}', + 'body' => 'Test body for {{to}}' + ]); + return $template; + } + + public static function getProps() { + return [ + 'type', + 'subject', + 'body' + ]; + } + + public function compile($config) { + return [ + 'body' => $this->compileString($this->body, $config), + 'subject' => $this->compileString($this->subject, $config), + 'to' => $config['to'] + ]; + } + + public function compileString($string, $config) { + $compiledString = $string; + + foreach ($config as $configName => $configValue) { + $compiledString = str_replace("{{{$configName}}}", $configValue, $compiledString); + } + + return $compiledString; + } +} \ No newline at end of file From e6fb52791ef5244c5f7ee4b74cd5be1fa7f8cdf4 Mon Sep 17 00:00:00 2001 From: ivan Date: Thu, 14 Jul 2016 19:31:23 -0300 Subject: [PATCH 5/7] Ivan - Add Unit testing for MailTemplate [skip ci] --- server/tests/models/MailTemplateTest.php | 27 ++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 server/tests/models/MailTemplateTest.php diff --git a/server/tests/models/MailTemplateTest.php b/server/tests/models/MailTemplateTest.php new file mode 100644 index 00000000..95cdd476 --- /dev/null +++ b/server/tests/models/MailTemplateTest.php @@ -0,0 +1,27 @@ +assertEquals(MailTemplate::USER_SIGNUP, $mailTemplate->type); + } + + public function testCompilation() { + $mailTemplate = new MailTemplate(); + $mailTemplate->setProperties([ + 'subject' => 'Welcoming to {{to}}', + 'body' => 'Welcome, {{userName}} to our team' + ]); + + $result = $mailTemplate->compile([ + 'to' => 'cersei@opensupports.com', + 'userName' => 'Cersei Lannister', + ]); + + $this->assertEquals($result['subject'], 'Welcoming to cersei@opensupports.com'); + $this->assertEquals($result['body'], 'Welcome, Cersei Lannister to our team'); + } +} From 9a25e7b7aeffce8641a793cf78e9217f7eb9178b Mon Sep 17 00:00:00 2001 From: ivan Date: Thu, 14 Jul 2016 19:53:24 -0300 Subject: [PATCH 6/7] Ivan - Add FakeSMTP support [skip ci] --- README.md | 20 +++++++++++++++++++- server/libs/MailSender.php | 8 ++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5e06c3d0..5c59ebd6 100644 --- a/README.md +++ b/README.md @@ -77,4 +77,22 @@ Just as there is the `gulp dev` task for development, there is also a `gulp prod 3. Install bundle `sudo gem install bundler` 4. Go to test folder `cd os4-react/tests` 5. Install project dependencies `sudo gem install bundler` -Test can run by using executing `run-tests.sh` file. \ No newline at end of file +Test can run by using executing `run-tests.sh` file. + +##### BACKEND SMTP SERVER TESTING +An email SMTP server can be configured in by EmailSender class. If you're doing development, you can use a fakeSMTP server. + +1. Install java if you don't have it + + `sudo apt-get install default-jre` + `sudo apt-get install default-jdk` + +2. Download FakeSMTP: https://nilhcem.github.io/FakeSMTP/download.html + +3. Extract the file from the zip and run it + + `java -jar fakeSMTP-2.0.jar` + +4. Set the port to 7070 and start the SMTP server + +5. Now every time that the application sends an email, it will be reflected there. \ No newline at end of file diff --git a/server/libs/MailSender.php b/server/libs/MailSender.php index 51026512..e419f07f 100644 --- a/server/libs/MailSender.php +++ b/server/libs/MailSender.php @@ -5,11 +5,11 @@ class MailSender { //TODO: Add real initial options when Settings class is available public function __construct() { - $this->mailOptions['from'] = ''; + $this->mailOptions['from'] = 'noreply@opensupports.com'; //SMTP Options - $this->mailOptions['smtp_host'] = ''; - $this->mailOptions['smtp_port'] = 0; + $this->mailOptions['smtp_host'] = 'localhost'; + $this->mailOptions['smtp_port'] = 7070; $this->mailOptions['smtp_user'] = ''; $this->mailOptions['smtp_pass'] = ''; } @@ -36,7 +36,7 @@ class MailSender { $mailer->Port = $this->mailOptions['smtp_port']; $mailer->Username = $this->mailOptions['smtp_user']; $mailer->Password = $this->mailOptions['smtp_pass']; - $mailer->SMTPSecure = "tls"; + //$mailer->SMTPSecure = "tls"; $mailer->Timeout = 1000; if ($mailer->smtpConnect()) { From 9aca25c076a752d9c190603bfdd2c35fac9cf4ff Mon Sep 17 00:00:00 2001 From: ivan Date: Thu, 14 Jul 2016 19:58:54 -0300 Subject: [PATCH 7/7] Ivan - Add FakeSMTP support --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5c59ebd6..8b140781 100644 --- a/README.md +++ b/README.md @@ -79,15 +79,15 @@ Just as there is the `gulp dev` task for development, there is also a `gulp prod 5. Install project dependencies `sudo gem install bundler` Test can run by using executing `run-tests.sh` file. -##### BACKEND SMTP SERVER TESTING -An email SMTP server can be configured in by EmailSender class. If you're doing development, you can use a fakeSMTP server. +##### BACKEND FAKE SMTP SERVER +If you're doing development, you can use a FakeSMTP server to see the mails that are being sent. -1. Install java if you don't have it +1. Install java if you don't have it jet `sudo apt-get install default-jre` `sudo apt-get install default-jdk` -2. Download FakeSMTP: https://nilhcem.github.io/FakeSMTP/download.html +2. [Download FakeSMTP](https://nilhcem.github.io/FakeSMTP/download.html) 3. Extract the file from the zip and run it @@ -95,4 +95,4 @@ An email SMTP server can be configured in by EmailSender class. If you're doing 4. Set the port to 7070 and start the SMTP server -5. Now every time that the application sends an email, it will be reflected there. \ No newline at end of file +5. Every time the application sends an email, it will be reflected there. \ No newline at end of file