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();
}
$mailSender = new MailSender();
$mailSender = MailSender::getInstance();
$mailSender->setTemplate(MailTemplate::USER_SYSTEM_DISABLED, [
'to' => $user->email,

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -2,8 +2,18 @@
class MailSender {
private $mailOptions = [];
public function __construct() {
private $mailerInstance;
private static $instance = NULL;
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['fromName'] = 'OpenSupports';
@ -21,32 +31,51 @@ class MailSender {
}
public function send() {
$mailer = new PHPMailer();
$mailerInstance = $this->getMailerInstance();
$mailer->From = $this->mailOptions['from'];
$mailer->FromName = $this->mailOptions['fromName'];
$mailer->addAddress($this->mailOptions['to']);
$mailer->Subject = $this->mailOptions['subject'];
$mailer->Body = $this->mailOptions['body'];
$mailer->isHTML(true);
if( !array_key_exists('to', $this->mailOptions) ||
!array_key_exists('subject', $this->mailOptions) ||
!array_key_exists('body', $this->mailOptions) ) {
throw new Exception('Mail sending data not available');
}
$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->Timeout = 1000;
$mailer->SMTPOptions = [
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
]
];
$mailerInstance->addAddress($this->mailOptions['to']);
$mailerInstance->Subject = $this->mailOptions['subject'];
$mailerInstance->Body = $this->mailOptions['body'];
$mailerInstance->isHTML(true);
if ($mailer->smtpConnect()) {
$mailer->send();
if ($this->isConnected()) {
$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;
}
}