mirror of
https://github.com/opensupports/opensupports.git
synced 2025-07-31 01:35:15 +02:00
Ivan - Add MailTemplate and MailSender
This commit is contained in:
parent
948b476c12
commit
8b810f7bd5
@ -2,6 +2,9 @@
|
|||||||
|
|
||||||
class SignUpController extends Controller {
|
class SignUpController extends Controller {
|
||||||
const PATH = '/signup';
|
const PATH = '/signup';
|
||||||
|
|
||||||
|
private $userEmail;
|
||||||
|
private $userPassword;
|
||||||
|
|
||||||
public function validations() {
|
public function validations() {
|
||||||
return [
|
return [
|
||||||
@ -11,26 +14,45 @@ class SignUpController extends Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function handler() {
|
public function handler() {
|
||||||
$email = Controller::request('email');
|
$this->setRequestData();
|
||||||
$password = Controller::request('password');
|
|
||||||
|
|
||||||
$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 = new User();
|
||||||
$userInstance->setProperties(array(
|
|
||||||
'email' => $email,
|
$userInstance->setProperties([
|
||||||
'password' => Hashing::hashPassword($password)
|
'email' => $this->userEmail,
|
||||||
));
|
'password' => Hashing::hashPassword($this->userPassword)
|
||||||
|
]);
|
||||||
|
|
||||||
return $userInstance->store();
|
return $userInstance->store();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function sendRegistrationMail() {
|
||||||
|
$mailSender = new MailSender();
|
||||||
|
|
||||||
|
$mailSender->setTemplate(MailTemplate::USER_SIGNUP, [
|
||||||
|
'to' => $this->userEmail
|
||||||
|
]);
|
||||||
|
|
||||||
|
$mailSender->send();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@ $app = new \Slim\Slim();
|
|||||||
include_once 'libs/Controller.php';
|
include_once 'libs/Controller.php';
|
||||||
include_once 'libs/ControllerGroup.php';
|
include_once 'libs/ControllerGroup.php';
|
||||||
include_once 'libs/Hashing.php';
|
include_once 'libs/Hashing.php';
|
||||||
include_once 'libs/EmailSender.php';
|
include_once 'libs/MailSender.php';
|
||||||
|
|
||||||
// LOAD MODELS
|
// LOAD MODELS
|
||||||
spl_autoload_register(function ($class) {
|
spl_autoload_register(function ($class) {
|
||||||
|
@ -1,22 +0,0 @@
|
|||||||
<?php
|
|
||||||
class EmailSender {
|
|
||||||
|
|
||||||
public static function validRegister($mail) {
|
|
||||||
$newMail = new PHPMailer;
|
|
||||||
|
|
||||||
$mail->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";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
46
server/libs/MailSender.php
Normal file
46
server/libs/MailSender.php
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
class MailSender {
|
||||||
|
|
||||||
|
private $mailOptions = [];
|
||||||
|
|
||||||
|
//TODO: Add real initial options when Settings class is available
|
||||||
|
public function __construct() {
|
||||||
|
$this->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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -4,7 +4,9 @@ use RedBeanPHP\Facade as RedBean;
|
|||||||
abstract class DataStore {
|
abstract class DataStore {
|
||||||
protected $_bean;
|
protected $_bean;
|
||||||
|
|
||||||
abstract protected function getDefaultProps();
|
public function getDefaultProps() {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
public static function getDataStore($value, $property = 'id') {
|
public static function getDataStore($value, $property = 'id') {
|
||||||
$bean = RedBean::findOne(static::TABLE, static::validateProp($property) . ' =:value', array(
|
$bean = RedBean::findOne(static::TABLE, static::validateProp($property) . ' =:value', array(
|
||||||
|
46
server/models/MailTemplate.php
Normal file
46
server/models/MailTemplate.php
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
class MailTemplate extends DataStore {
|
||||||
|
const TABLE = 'mailtemplate';
|
||||||
|
|
||||||
|
const USER_SIGNUP = 'USER_SIGNUP';
|
||||||
|
const USER_PASSWORD = 'USER_PASSWORD';
|
||||||
|
|
||||||
|
public static function getTemplate($type) {
|
||||||
|
//TODO: Add initial mails templates to the database
|
||||||
|
//return MailTemplate::getDataStore($type, 'type');
|
||||||
|
|
||||||
|
$template = new MailTemplate();
|
||||||
|
$template->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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user