Ivan - Avoid verification if smtp is not set. Implement singleton pattern in MailSender class

This commit is contained in:
Ivan Diaz 2017-06-05 11:41:52 -03:00
parent 466d264d17
commit ec34acc080
11 changed files with 69 additions and 37 deletions

View File

@ -62,7 +62,7 @@ class DisableUserSystemController extends Controller {
$ticket->store(); $ticket->store();
} }
$mailSender = new MailSender(); $mailSender = MailSender::getInstance();
$mailSender->setTemplate(MailTemplate::USER_SYSTEM_DISABLED, [ $mailSender->setTemplate(MailTemplate::USER_SYSTEM_DISABLED, [
'to' => $user->email, 'to' => $user->email,

View File

@ -86,7 +86,7 @@ class EnableUserSystemController extends Controller {
$userInstance->store(); $userInstance->store();
$mailSender = new MailSender(); $mailSender = MailSender::getInstance();
$mailSender->setTemplate(MailTemplate::USER_SYSTEM_ENABLED, [ $mailSender->setTemplate(MailTemplate::USER_SYSTEM_ENABLED, [
'to' => $email, 'to' => $email,
'name' => $name, 'name' => $name,

View File

@ -91,7 +91,7 @@ class CloseController extends Controller {
} }
private function sendMail() { private function sendMail() {
$mailSender = new MailSender(); $mailSender = MailSender::getInstance();
$mailSender->setTemplate(MailTemplate::TICKET_CLOSED, [ $mailSender->setTemplate(MailTemplate::TICKET_CLOSED, [
'to' => $this->ticket->author->email, 'to' => $this->ticket->author->email,

View File

@ -119,7 +119,7 @@ class CommentController extends Controller {
} }
private function sendMail() { private function sendMail() {
$mailSender = new MailSender(); $mailSender = MailSender::getInstance();
$mailSender->setTemplate(MailTemplate::TICKET_RESPONDED, [ $mailSender->setTemplate(MailTemplate::TICKET_RESPONDED, [
'to' => ($this->ticket->author) ? $this->ticket->author->email : $this->ticket->authorEmail, 'to' => ($this->ticket->author) ? $this->ticket->author->email : $this->ticket->authorEmail,

View File

@ -136,7 +136,7 @@ class CreateController extends Controller {
} }
private function sendMail() { private function sendMail() {
$mailSender = new MailSender(); $mailSender = MailSender::getInstance();
$mailSender->setTemplate(MailTemplate::TICKET_CREATED, [ $mailSender->setTemplate(MailTemplate::TICKET_CREATED, [
'to' => $this->email, 'to' => $this->email,

View File

@ -44,7 +44,7 @@ class EditEmail extends Controller{
$user->email = $newEmail; $user->email = $newEmail;
$user->store(); $user->store();
$mailSender = new MailSender(); $mailSender = MailSender::getInstance();
$mailSender->setTemplate('USER_EMAIL', [ $mailSender->setTemplate('USER_EMAIL', [
'to'=>$oldEmail, 'to'=>$oldEmail,
'newemail'=>$user->email, 'newemail'=>$user->email,

View File

@ -48,7 +48,7 @@ class EditPassword extends Controller {
$user->password = Hashing::hashPassword($newPassword); $user->password = Hashing::hashPassword($newPassword);
$user->store(); $user->store();
$mailSender = new MailSender(); $mailSender = MailSender::getInstance();
$mailSender->setTemplate('USER_PASSWORD', [ $mailSender->setTemplate('USER_PASSWORD', [
'to'=>$user->email, 'to'=>$user->email,
'name'=>$user->name 'name'=>$user->name

View File

@ -85,7 +85,7 @@ class RecoverPasswordController extends Controller {
} }
} }
public function sendMail() { public function sendMail() {
$mailSender = new MailSender(); $mailSender = MailSender::getInstance();
$mailSender->setTemplate(MailTemplate::USER_PASSWORD, [ $mailSender->setTemplate(MailTemplate::USER_PASSWORD, [
'to' => $this->user->email, 'to' => $this->user->email,

View File

@ -70,7 +70,7 @@ class SendRecoverPasswordController extends Controller {
} }
public function sendEmail() { public function sendEmail() {
$mailSender = new MailSender(); $mailSender = MailSender::getInstance();
$mailSender->setTemplate(MailTemplate::PASSWORD_FORGOT, [ $mailSender->setTemplate(MailTemplate::PASSWORD_FORGOT, [
'to' => $this->user->email, 'to' => $this->user->email,

View File

@ -101,7 +101,10 @@ class SignUpController extends Controller {
} }
$userId = $this->createNewUserAndRetrieveId(); $userId = $this->createNewUserAndRetrieveId();
$this->sendRegistrationMail();
if(MailSender::getInstance()->isConnected()) {
$this->sendRegistrationMail();
}
Response::respondSuccess([ Response::respondSuccess([
'userId' => $userId, 'userId' => $userId,
@ -127,14 +130,14 @@ class SignUpController extends Controller {
'tickets' => 0, 'tickets' => 0,
'email' => $this->userEmail, 'email' => $this->userEmail,
'password' => Hashing::hashPassword($this->userPassword), 'password' => Hashing::hashPassword($this->userPassword),
'verificationToken' => $this->verificationToken 'verificationToken' => (MailSender::getInstance()->isConnected()) ? $this->verificationToken : null
]); ]);
return $userInstance->store(); return $userInstance->store();
} }
public function sendRegistrationMail() { public function sendRegistrationMail() {
$mailSender = new MailSender(); $mailSender = MailSender::getInstance();
$mailSender->setTemplate(MailTemplate::USER_SIGNUP, [ $mailSender->setTemplate(MailTemplate::USER_SIGNUP, [
'to' => $this->userEmail, 'to' => $this->userEmail,

View File

@ -2,8 +2,18 @@
class MailSender { class MailSender {
private $mailOptions = []; private $mailOptions = [];
private $mailerInstance;
private static $instance = NULL;
public function __construct() { public static function getInstance() {
if(MailSender::$instance === NULL) {
MailSender::$instance = new MailSender();
}
return MailSender::$instance;
}
private function __construct() {
$this->mailOptions['from'] = Setting::getSetting('no-reply-email')->value; $this->mailOptions['from'] = Setting::getSetting('no-reply-email')->value;
$this->mailOptions['fromName'] = 'OpenSupports'; $this->mailOptions['fromName'] = 'OpenSupports';
@ -21,32 +31,51 @@ class MailSender {
} }
public function send() { public function send() {
$mailer = new PHPMailer(); $mailerInstance = $this->getMailerInstance();
$mailer->From = $this->mailOptions['from']; if( !array_key_exists('to', $this->mailOptions) ||
$mailer->FromName = $this->mailOptions['fromName']; !array_key_exists('subject', $this->mailOptions) ||
$mailer->addAddress($this->mailOptions['to']); !array_key_exists('body', $this->mailOptions) ) {
$mailer->Subject = $this->mailOptions['subject']; throw new Exception('Mail sending data not available');
$mailer->Body = $this->mailOptions['body']; }
$mailer->isHTML(true);
$mailer->isSMTP(); $mailerInstance->addAddress($this->mailOptions['to']);
$mailer->SMTPAuth = true; $mailerInstance->Subject = $this->mailOptions['subject'];
$mailer->Host = $this->mailOptions['smtp-host']; $mailerInstance->Body = $this->mailOptions['body'];
$mailer->Port = $this->mailOptions['smtp-port']; $mailerInstance->isHTML(true);
$mailer->Username = $this->mailOptions['smtp-user'];
$mailer->Password = $this->mailOptions['smtp-pass'];
$mailer->Timeout = 1000;
$mailer->SMTPOptions = [
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
]
];
if ($mailer->smtpConnect()) { if ($this->isConnected()) {
$mailer->send(); $mailerInstance->send();
} }
} }
public function isConnected() {
return $this->getMailerInstance()->smtpConnect();
}
private function getMailerInstance() {
if(!($this->mailerInstance instanceof PHPMailer)) {
$this->mailerInstance = new PHPMailer();
$this->mailerInstance->From = $this->mailOptions['from'];
$this->mailerInstance->FromName = $this->mailOptions['fromName'];
$this->mailerInstance->isSMTP();
$this->mailerInstance->SMTPAuth = true;
$this->mailerInstance->Host = $this->mailOptions['smtp-host'];
$this->mailerInstance->Port = $this->mailOptions['smtp-port'];
$this->mailerInstance->Username = $this->mailOptions['smtp-user'];
$this->mailerInstance->Password = $this->mailOptions['smtp-pass'];
$this->mailerInstance->Timeout = 1000;
$this->mailerInstance->SMTPOptions = [
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
]
];
}
return $this->mailerInstance;
}
} }