Merged in Add-email-sender-class (pull request #22)
Add email sender class
This commit is contained in:
commit
4c075d3821
20
README.md
20
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.
|
||||
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.
|
|
@ -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.*"
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
<?php
|
||||
use RedBeanPHP\Facade as RedBean;
|
||||
|
||||
class SignUpController extends Controller {
|
||||
const PATH = '/signup';
|
||||
|
||||
private $userEmail;
|
||||
private $userPassword;
|
||||
|
||||
public function validations() {
|
||||
return [
|
||||
|
@ -12,24 +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();
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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'] = '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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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(
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
include_once 'tests/__lib__/Mock.php';
|
||||
include_once 'models/MailTemplate.php';
|
||||
|
||||
class MailTemplateTest extends PHPUnit_Framework_TestCase {
|
||||
public function testGetTemplateShouldReturnSpecifiedTemplate() {
|
||||
$mailTemplate = MailTemplate::getTemplate(MailTemplate::USER_SIGNUP);
|
||||
|
||||
$this->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');
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue