diff --git a/README.md b/README.md index 5e06c3d0..8b140781 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 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 jet + + `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. Every time the application sends an email, it will be reflected there. \ No newline at end of file diff --git a/server/composer.json b/server/composer.json index 960dd0ce..fc910636 100644 --- a/server/composer.json +++ b/server/composer.json @@ -2,7 +2,8 @@ "require": { "slim/slim": "~2.0", "gabordemooij/redbean": "~4.2", - "respect/validation": "^1.1" + "respect/validation": "^1.1", + "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 0088858a..26757658 100644 --- a/server/controllers/user/signup.php +++ b/server/controllers/user/signup.php @@ -1,8 +1,10 @@ setRequestData(); - $userId = $this->createNewUserAndRetrieveId($email, $password); + try { + $userId = $this->createNewUserAndRetrieveId(); + $this->sendRegistrationMail(); - Response::respondSuccess(array( - 'userId' => $userId, - 'userEmail' => $email - )); + Response::respondSuccess([ + 'userId' => $userId, + 'userEmail' => $this->userEmail + ]); + } catch (Exception $e) { + Response::respondError($e->getMessage()); + } + + } + + 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 850db116..04edd2e7 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/MailSender.php'; // LOAD MODELS spl_autoload_register(function ($class) { diff --git a/server/libs/MailSender.php b/server/libs/MailSender.php new file mode 100644 index 00000000..e419f07f --- /dev/null +++ b/server/libs/MailSender.php @@ -0,0 +1,46 @@ +mailOptions['from'] = 'noreply@opensupports.com'; + + //SMTP Options + $this->mailOptions['smtp_host'] = 'localhost'; + $this->mailOptions['smtp_port'] = 7070; + $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 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'); + } +}