Ivan - Add MailTemplate and MailSender

This commit is contained in:
ivan 2016-07-14 19:06:47 -03:00
parent 948b476c12
commit 8b810f7bd5
6 changed files with 131 additions and 37 deletions

View File

@ -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();
}
}

View File

@ -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) {

View File

@ -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";
}
}
}

View 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();
}
}
}

View File

@ -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(

View 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;
}
}