From 8b810f7bd5d323cc98e1fd6f3d5366b0a14630c7 Mon Sep 17 00:00:00 2001 From: ivan Date: Thu, 14 Jul 2016 19:06:47 -0300 Subject: [PATCH] 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