Refactor project structure
This commit is contained in:
195
core/src/Classes/Email/EmailSender.php
Normal file
195
core/src/Classes/Email/EmailSender.php
Normal file
@@ -0,0 +1,195 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Thilina
|
||||
* Date: 8/20/17
|
||||
* Time: 9:38 AM
|
||||
*/
|
||||
|
||||
namespace Classes\Email;
|
||||
|
||||
use Classes\Crypt\AesCtr;
|
||||
use Classes\UIManager;
|
||||
use Employees\Common\Model\Employee;
|
||||
use Users\Common\Model\User;
|
||||
|
||||
abstract class EmailSender
|
||||
{
|
||||
/* @var \Classes\SettingsManager $settings */
|
||||
public $settings = null;
|
||||
public function __construct($settings)
|
||||
{
|
||||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
public function sendEmailFromNotification($notification)
|
||||
{
|
||||
$toEmail = null;
|
||||
$user = new User();
|
||||
$user->Load("id = ?", array($notification->toUser));
|
||||
|
||||
if (!empty($user->email)) {
|
||||
$name = "User";
|
||||
$employee = new Employee();
|
||||
$employee->Load("id = ?", array($user->employee));
|
||||
if ($employee->id == $user->employee && !empty($employee->id)) {
|
||||
$name = $employee->first_name;
|
||||
}
|
||||
|
||||
$action = json_decode($notification->action);
|
||||
|
||||
$emailBody = file_get_contents(APP_BASE_PATH.'/templates/email/notificationEmail.html');
|
||||
$emailBody = str_replace("#_user_#", $name, $emailBody);
|
||||
$emailBody = str_replace("#_message_#", $notification->message, $emailBody);
|
||||
if ($action->type == "url") {
|
||||
$emailBody = str_replace("#_url_#", CLIENT_BASE_URL."?".$action->url, $emailBody);
|
||||
}
|
||||
$this->sendEmail(
|
||||
'IceHrm Notification from '.$notification->type,
|
||||
$user->email,
|
||||
$emailBody,
|
||||
array(),
|
||||
array(),
|
||||
array()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function sendEmailFromDB($email)
|
||||
{
|
||||
$params = array();
|
||||
if (!empty($email->params)) {
|
||||
$params = json_decode($email->params, true);
|
||||
}
|
||||
|
||||
$cclist = array();
|
||||
if (!empty($email->cclist)) {
|
||||
$cclist = json_decode($email->cclist, true);
|
||||
}
|
||||
|
||||
$bcclist = array();
|
||||
if (!empty($email->bcclist)) {
|
||||
$bcclist = json_decode($email->bcclist, true);
|
||||
}
|
||||
|
||||
return $this->sendEmail($email->subject, $email->toEmail, $email->template, $params, $cclist, $bcclist);
|
||||
}
|
||||
|
||||
public function sendEmail($subject, $toEmail, $template, $params, $ccList = array(), $bccList = array())
|
||||
{
|
||||
|
||||
$body = $template;
|
||||
|
||||
foreach ($params as $k => $v) {
|
||||
$body = str_replace("#_".$k."_#", $v, $body);
|
||||
}
|
||||
|
||||
$fromEmail = $this->settings->getSetting("Email: Email From");
|
||||
|
||||
//Convert to an html email
|
||||
$emailBody = file_get_contents(APP_BASE_PATH.'/templates/email/emailBody.html');
|
||||
|
||||
$emailBody = str_replace("#_emailBody_#", $body, $emailBody);
|
||||
$emailBody = str_replace(
|
||||
"#_logourl_#",
|
||||
UIManager::getInstance()->getCompanyLogoUrl(),
|
||||
$emailBody
|
||||
);
|
||||
|
||||
$user = new User();
|
||||
$user->Load("username = ?", array('admin'));
|
||||
|
||||
if (empty($user->id)) {
|
||||
$users = $user->Find("user_level = ?", array('Admin'));
|
||||
$user = $users[0];
|
||||
}
|
||||
|
||||
$emailBody = str_replace("#_adminEmail_#", $user->email, $emailBody);
|
||||
$emailBody = str_replace("#_url_#", CLIENT_BASE_URL, $emailBody);
|
||||
foreach ($params as $k => $v) {
|
||||
$emailBody = str_replace("#_".$k."_#", $v, $emailBody);
|
||||
}
|
||||
|
||||
return $this->sendMail($subject, $emailBody, $toEmail, $fromEmail, $user->email, $ccList, $bccList, APP_NAME);
|
||||
}
|
||||
|
||||
public function sendEmailWithoutWrap($subject, $toEmail, $template, $params, $ccList = array(), $bccList = array())
|
||||
{
|
||||
|
||||
$body = $template;
|
||||
|
||||
foreach ($params as $k => $v) {
|
||||
$body = str_replace("#_".$k."_#", $v, $body);
|
||||
}
|
||||
|
||||
$fromEmail = APP_NAME." <".$this->settings->getSetting("Email: Email From").">";
|
||||
|
||||
//Convert to an html email
|
||||
$emailBody = $body;
|
||||
$emailBody = str_replace(
|
||||
"#_logourl_#",
|
||||
UIManager::getInstance()->getCompanyLogoUrl(),
|
||||
$emailBody
|
||||
);
|
||||
|
||||
$user = new User();
|
||||
$user->Load("username = ?", array('admin'));
|
||||
|
||||
if (empty($user->id)) {
|
||||
$users = $user->Find("user_level = ?", array('Admin'));
|
||||
$user = $users[0];
|
||||
}
|
||||
|
||||
$emailBody = str_replace("#_adminEmail_#", $user->email, $emailBody);
|
||||
$emailBody = str_replace("#_url_#", CLIENT_BASE_URL, $emailBody);
|
||||
foreach ($params as $k => $v) {
|
||||
$emailBody = str_replace("#_".$k."_#", $v, $emailBody);
|
||||
}
|
||||
|
||||
$this->sendMail($subject, $emailBody, $toEmail, $fromEmail, $user->email, $ccList, $bccList);
|
||||
}
|
||||
|
||||
abstract protected function sendMail(
|
||||
$subject,
|
||||
$body,
|
||||
$toEmail,
|
||||
$fromEmail,
|
||||
$replyToEmail = null,
|
||||
$ccList = array(),
|
||||
$bccList = array(),
|
||||
$fromName = null
|
||||
);
|
||||
|
||||
public function sendResetPasswordEmail($emailOrUserId)
|
||||
{
|
||||
$user = new User();
|
||||
$user->Load("email = ?", array($emailOrUserId));
|
||||
if (empty($user->id)) {
|
||||
$user = new User();
|
||||
$user->Load("username = ?", array($emailOrUserId));
|
||||
if (empty($user->id)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$params = array();
|
||||
//$params['user'] = $user->first_name." ".$user->last_name;
|
||||
$params['url'] = CLIENT_BASE_URL;
|
||||
|
||||
$newPassHash = array();
|
||||
$newPassHash["CLIENT_NAME"] = CLIENT_NAME;
|
||||
$newPassHash["oldpass"] = $user->password;
|
||||
$newPassHash["email"] = $user->email;
|
||||
$newPassHash["time"] = time();
|
||||
$json = json_encode($newPassHash);
|
||||
|
||||
$encJson = AesCtr::encrypt($json, $user->password, 256);
|
||||
$encJson = urlencode($user->id."-".$encJson);
|
||||
$params['passurl'] = CLIENT_BASE_URL."service.php?a=rsp&key=".$encJson;
|
||||
|
||||
$emailBody = file_get_contents(APP_BASE_PATH.'/templates/email/passwordReset.html');
|
||||
|
||||
$this->sendEmail("[".APP_NAME."] Password Change Request", $user->email, $emailBody, $params);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
61
core/src/Classes/Email/PHPMailer.php
Normal file
61
core/src/Classes/Email/PHPMailer.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Thilina
|
||||
* Date: 8/20/17
|
||||
* Time: 9:40 AM
|
||||
*/
|
||||
|
||||
namespace Classes\Email;
|
||||
|
||||
use Utils\LogManager;
|
||||
|
||||
class PHPMailer extends EmailSender
|
||||
{
|
||||
|
||||
public function __construct($settings)
|
||||
{
|
||||
parent::__construct($settings);
|
||||
}
|
||||
|
||||
protected function sendMail(
|
||||
$subject,
|
||||
$body,
|
||||
$toEmail,
|
||||
$fromEmail,
|
||||
$replyToEmail = null,
|
||||
$ccList = array(),
|
||||
$bccList = array(),
|
||||
$fromName = null
|
||||
) {
|
||||
|
||||
try {
|
||||
if ($fromName) {
|
||||
$fromEmail = $fromName." <".$fromEmail.">";
|
||||
}
|
||||
|
||||
if (empty($replyToEmail)) {
|
||||
$replyToEmail = $fromEmail;
|
||||
}
|
||||
|
||||
LogManager::getInstance()->info("Sending email to: " . $toEmail . "/ from: " . $fromEmail);
|
||||
|
||||
$headers = 'MIME-Version: 1.0' . "\r\n";
|
||||
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
|
||||
$headers .= 'From: ' . $fromEmail . "\r\n";
|
||||
if (!empty($ccList)) {
|
||||
$headers .= 'CC: ' . implode(",", $ccList) . "\r\n";
|
||||
}
|
||||
if (!empty($bccList)) {
|
||||
$headers .= 'BCC: ' . implode(",", $bccList) . "\r\n";
|
||||
}
|
||||
$headers .= 'ReplyTo: ' . $replyToEmail . "\r\n";
|
||||
$headers .= 'Ice-Mailer: PHP/' . phpversion();
|
||||
|
||||
return mail($toEmail, $subject, $body, $headers);
|
||||
} catch (\Exception $e) {
|
||||
LogManager::getInstance()->error("Error sending email:" . $e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
89
core/src/Classes/Email/SMTPEmailSender.php
Normal file
89
core/src/Classes/Email/SMTPEmailSender.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Thilina
|
||||
* Date: 8/20/17
|
||||
* Time: 9:39 AM
|
||||
*/
|
||||
|
||||
namespace Classes\Email;
|
||||
|
||||
use Utils\LogManager;
|
||||
|
||||
class SMTPEmailSender extends EmailSender
|
||||
{
|
||||
|
||||
public function __construct($settings)
|
||||
{
|
||||
parent::__construct($settings);
|
||||
}
|
||||
|
||||
protected function sendMail(
|
||||
$subject,
|
||||
$body,
|
||||
$toEmail,
|
||||
$fromEmail,
|
||||
$replyToEmail = null,
|
||||
$ccList = array(),
|
||||
$bccList = array(),
|
||||
$fromName = ''
|
||||
) {
|
||||
|
||||
try {
|
||||
if (empty($replyToEmail)) {
|
||||
$replyToEmail = $fromEmail;
|
||||
}
|
||||
|
||||
LogManager::getInstance()->info("Sending email to: " . $toEmail . "/ from: " . $fromEmail);
|
||||
|
||||
$host = $this->settings->getSetting("Email: SMTP Host");
|
||||
$username = $this->settings->getSetting("Email: SMTP User");
|
||||
$password = $this->settings->getSetting("Email: SMTP Password");
|
||||
$port = $this->settings->getSetting("Email: SMTP Port");
|
||||
|
||||
if (empty($port)) {
|
||||
$port = '25';
|
||||
}
|
||||
|
||||
if ($this->settings->getSetting("Email: SMTP Authentication Required") == "0") {
|
||||
$auth = array('host' => $host,
|
||||
'auth' => false);
|
||||
} else {
|
||||
$auth = array('host' => $host,
|
||||
'auth' => true,
|
||||
'username' => $username,
|
||||
'port' => $port,
|
||||
'password' => $password);
|
||||
}
|
||||
|
||||
$smtp = \Mail::factory('smtp', $auth);
|
||||
|
||||
$headers = array('MIME-Version' => '1.0',
|
||||
'Content-type' => 'text/html',
|
||||
'charset' => 'iso-8859-1',
|
||||
'From' => $fromEmail,
|
||||
'To' => $toEmail,
|
||||
'Reply-To' => $replyToEmail,
|
||||
'Subject' => $subject);
|
||||
|
||||
if (!empty($ccList)) {
|
||||
$headers['Cc'] = implode(",", $ccList);
|
||||
}
|
||||
|
||||
if (!empty($bccList)) {
|
||||
$headers['Bcc'] = implode(",", $bccList);
|
||||
}
|
||||
|
||||
$mail = $smtp->send($toEmail, $headers, $body);
|
||||
if (\PEAR::isError($mail)) {
|
||||
LogManager::getInstance()->info("SMTP Error Response:" . $mail->getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
LogManager::getInstance()->error("Error sending email:" . $e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
83
core/src/Classes/Email/SNSEmailSender.php
Normal file
83
core/src/Classes/Email/SNSEmailSender.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Thilina
|
||||
* Date: 8/20/17
|
||||
* Time: 9:39 AM
|
||||
*/
|
||||
namespace Classes\Email;
|
||||
|
||||
use Aws\Ses\SesClient;
|
||||
use Utils\LogManager;
|
||||
|
||||
class SNSEmailSender extends EmailSender
|
||||
{
|
||||
private $ses = null;
|
||||
public function __construct($settings)
|
||||
{
|
||||
parent::__construct($settings);
|
||||
$arr = array(
|
||||
'key' => $this->settings->getSetting('Email: Amazon Access Key ID'),
|
||||
'secret' => $this->settings->getSetting('Email: Amazon Secret Access Key'),
|
||||
'region' => AWS_REGION
|
||||
);
|
||||
$this->ses = SesClient::factory($arr);
|
||||
}
|
||||
|
||||
protected function sendMail(
|
||||
$subject,
|
||||
$body,
|
||||
$toEmail,
|
||||
$fromEmail,
|
||||
$replyToEmail = null,
|
||||
$ccList = array(),
|
||||
$bccList = array(),
|
||||
$fromName = null
|
||||
) {
|
||||
|
||||
try {
|
||||
if ($fromName) {
|
||||
$fromEmail = $fromName." <".$fromEmail.">";
|
||||
}
|
||||
|
||||
if (empty($replyToEmail)) {
|
||||
$replyToEmail = $fromEmail;
|
||||
}
|
||||
|
||||
LogManager::getInstance()->info("Sending email to: " . $toEmail . "/ from: " . $fromEmail);
|
||||
|
||||
$toArray = array('ToAddresses' => array($toEmail),
|
||||
'CcAddresses' => $ccList,
|
||||
'BccAddresses' => $bccList);
|
||||
$message = array(
|
||||
'Subject' => array(
|
||||
'Data' => $subject,
|
||||
'Charset' => 'UTF-8'
|
||||
),
|
||||
'Body' => array(
|
||||
'Html' => array(
|
||||
'Data' => $body,
|
||||
'Charset' => 'UTF-8'
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
$response = $this->ses->sendEmail(
|
||||
array(
|
||||
'Source' => $fromEmail,
|
||||
'Destination' => $toArray,
|
||||
'Message' => $message,
|
||||
'ReplyToAddresses' => array($replyToEmail),
|
||||
'ReturnPath' => $fromEmail
|
||||
)
|
||||
);
|
||||
|
||||
LogManager::getInstance()->info("SES Response:" . print_r($response, true));
|
||||
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
LogManager::getInstance()->error("Error sending email:" . $e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
63
core/src/Classes/Email/SwiftMailer.php
Normal file
63
core/src/Classes/Email/SwiftMailer.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
namespace Classes\Email;
|
||||
|
||||
use Utils\LogManager;
|
||||
|
||||
class SwiftMailer extends EmailSender
|
||||
{
|
||||
|
||||
public function __construct($settings)
|
||||
{
|
||||
parent::__construct($settings);
|
||||
}
|
||||
|
||||
protected function sendMail(
|
||||
$subject,
|
||||
$body,
|
||||
$toEmail,
|
||||
$fromEmail,
|
||||
$replyToEmail = null,
|
||||
$ccList = array(),
|
||||
$bccList = array(),
|
||||
$fromName = null
|
||||
) {
|
||||
|
||||
try {
|
||||
if (empty($replyToEmail)) {
|
||||
$replyToEmail = $fromEmail;
|
||||
}
|
||||
|
||||
LogManager::getInstance()->info("Sending email to: " . $toEmail . "/ from: " . $fromEmail);
|
||||
|
||||
$host = $this->settings->getSetting("Email: SMTP Host");
|
||||
$username = $this->settings->getSetting("Email: SMTP User");
|
||||
$password = $this->settings->getSetting("Email: SMTP Password");
|
||||
$port = $this->settings->getSetting("Email: SMTP Port");
|
||||
|
||||
if (empty($port)) {
|
||||
$port = '25';
|
||||
}
|
||||
$transport = new \Swift_SmtpTransport($host, $port);
|
||||
$mail = new \Swift_Message();
|
||||
|
||||
if ($this->settings->getSetting("Email: SMTP Authentication Required") === "1") {
|
||||
$transport->setUsername($username);
|
||||
$transport->setPassword($password);
|
||||
}
|
||||
|
||||
$mail->addFrom($fromEmail, $fromName);
|
||||
$mail->addReplyTo($replyToEmail);
|
||||
$mail->addTo($toEmail);
|
||||
$mail->setSubject($subject);
|
||||
$mail->setCc($ccList);
|
||||
$mail->setBcc($bccList);
|
||||
$mail->setBody($body);
|
||||
|
||||
$mailer = new \Swift_Mailer($transport);
|
||||
return $mailer->send($mail);
|
||||
} catch (\Exception $e) {
|
||||
LogManager::getInstance()->error("Error sending email:" . $e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user