Skip to content

Commit

Permalink
PUSH
Browse files Browse the repository at this point in the history
-> Email Template
  • Loading branch information
NaysKutzu committed Sep 8, 2024
1 parent 399d2a8 commit 2162d88
Show file tree
Hide file tree
Showing 11 changed files with 837 additions and 2 deletions.
5 changes: 5 additions & 0 deletions app/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,10 @@ public static function checkForRequirements(): void
if (version_compare(PHP_VERSION, '8.1.0', '<')) {
Installer::showError('This application requires at least PHP 8.1.0');
}

$url = Settings::getSetting('app', 'url');
if ($url == "http://example.com") {
Settings::updateSetting('app', 'url', "https://".$_SERVER['SERVER_NAME'],true);
}
}
}
64 changes: 64 additions & 0 deletions app/Mail/MailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace MythicalSystemsFramework\Mail;

use MythicalSystemsFramework\Managers\Settings as setting;
use MythicalSystemsFramework\User\UserHelper;

class MailService
{
Expand All @@ -17,4 +18,67 @@ public static function isEnabled(): bool
return false;
}
}
/**
* Process the template at a user level
*
* @param string $template
* @return string
*/
public static function processTemplateUserLevel(string $template,string $token) : string {
$user = new UserHelper($token);
$template = str_replace("{username}", $user->getInfo("username",false), $template);
$template = str_replace("{email}", $user->getInfo("email",false), $template);
$template = str_replace("{first_name}", $user->getInfo("first_name",true), $template);
$template = str_replace("{last_name}", $user->getInfo("last_name",true), $template);
return $template;
}

/**
* Process the template at a system level (app_name, app_logo, app_url, support_mail)
* @param string $template
* @return string
*/
public static function processTemplateSystemLevel(string $template) : string {
$template = str_replace("{app_name}", setting::getSetting('app', 'name'), $template);
$template = str_replace("{app_logo}", setting::getSetting('app', 'logo'), $template);
$template = str_replace("{app_url}", setting::getSetting('app', 'url'), $template);
$template = str_replace("{support_mail}", setting::getSetting('smtp', 'fromMail'), $template);
return $template;
}

/**
* Get the template
*
* @param string $template The template you want to get
*
* @return string
*/
public static function getTemplate(string $template): string
{
$template_dir = __DIR__ . "/../../storage/mails/";
$template_file = $template_dir . $template . ".html";
if (self::doesTemplateExist($template)) {
return file_get_contents($template_file);
} else {
return "";
}
}

/**
* Does the template exist?
*
* @param string $template The template you want to check if it exists
*
* @return bool
*/
public static function doesTemplateExist(string $template): bool
{
$template_dir = __DIR__ . "/../../storage/mails/";
$template_file = $template_dir . $template . ".html";
if (file_exists($template_file)) {
return true;
} else {
return false;
}
}
}
19 changes: 19 additions & 0 deletions app/Mail/Templates/Verification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace MythicalSystemsFramework\Mail\Templates;
use MythicalSystemsFramework\Mail\EmailTemplate;
use MythicalSystemsFramework\Mail\MailService;
use MythicalSystemsFramework\User\UserDataHandler;

class Verification extends MailService {
public static function sendMail(string $uuid) : void {
if (self::doesTemplateExist("verify")) {
$template = self::getTemplate("verify");
$template = self::processTemplateSystemLevel($template);
$template = self::processTemplateUserLevel($template, UserDataHandler::getTokenByUserID($uuid));
die($template);
} else {
return;
}
}
}
3 changes: 1 addition & 2 deletions app/Managers/DBSettingsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,9 @@ public static function update(string $category, string $key, string $value): boo
$mysql = new MySQL();
$conn = $mysql->connectMYSQLI();
$stmt = $conn->prepare('UPDATE framework_settings SET `svalue` = ? WHERE `skey` = ? AND `scategory` = ?');
$stmt->bind_param('sss', $scategory, $value, $key);
$stmt->bind_param('sss', $value, $key, $category);
$success = $stmt->execute();
$stmt->close();

return $success;
} else {
return false;
Expand Down
30 changes: 30 additions & 0 deletions app/Web/Routes/emails.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

global $router;

$router->add('/emails/welcome', function () {
$file = file_get_contents(__DIR__ . '/../../../storage/mails/welcome.html');
echo die($file);
});


$router->add('/emails/reset-password', function () {
$file = file_get_contents(__DIR__ . '/../../../storage/mails/reset-password.html');
echo die($file);
});

$router->add('/emails/user-banned', function () {
$file = file_get_contents(__DIR__ . '/../../../storage/mails/user-banned.html');
echo die($file);
});


$router->add('/emails/login', function () {
$file = file_get_contents(__DIR__ . '/../../../storage/mails/login.html');
echo die($file);
});

$router->add('/emails/notification', function () {
$file = file_get_contents(__DIR__ . '/../../../storage/mails/notification.html');
echo die($file);
});
141 changes: 141 additions & 0 deletions storage/mails/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<!DOCTYPE html>
<html lang="en" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">

<head>
<meta charset="utf-8">
<meta name="x-apple-disable-message-reformatting">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="format-detection" content="telephone=no, date=no, address=no, email=no">
<!--[if mso]>
<xml><o:officedocumentsettings><o:pixelsperinch>96</o:pixelsperinch></o:officedocumentsettings></xml>
<![endif]-->
<title>New Account Login</title>
<link
href="https://fonts.googleapis.com/css?family=Montserrat:ital,wght@0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,200;1,300;1,400;1,500;1,600;1,700"
rel="stylesheet" media="screen">
<style>
.hover-underline:hover {
text-decoration: underline !important;
}

@media (max-width: 600px) {
.sm-w-full {
width: 100% !important;
}

.sm-px-24 {
padding-left: 24px !important;
padding-right: 24px !important;
}

.sm-py-32 {
padding-top: 32px !important;
padding-bottom: 32px !important;
}

.sm-leading-32 {
line-height: 32px !important;
}
}
</style>
</head>

<body
style="margin: 0; width: 100%; padding: 0; word-break: break-word; -webkit-font-smoothing: antialiased; background-color: #eceff1;">
<div style="font-family: 'Montserrat', sans-serif; display: none;">New Account Login</div>
<div role="article" aria-roledescription="email" aria-label="New Account Login" lang="en"
style="font-family: 'Montserrat', sans-serif;">
<table style="width: 100%; font-family: Montserrat, -apple-system, 'Segoe UI', sans-serif;" cellpadding="0"
cellspacing="0" role="presentation">
<tr>
<td align="center"
style="background-color: #eceff1; font-family: Montserrat, -apple-system, 'Segoe UI', sans-serif;">
<table class="sm-w-full" style="width: 600px;" cellpadding="0" cellspacing="0" role="presentation">
<tr>
<td class="sm-py-32 sm-px-24"
style="padding: 48px; text-align: center; font-family: Montserrat, -apple-system, 'Segoe UI', sans-serif;">

</td>
</tr>
<tr>
<td align="center" class="sm-px-24"
style="font-family: 'Montserrat', sans-serif;">
<table style="width: 100%;" cellpadding="0" cellspacing="0" role="presentation">
<tr>
<td class="sm-px-24"
style="border-radius: 4px; background-color: #ffffff; padding: 48px; text-align: left; font-family: Montserrat, -apple-system, 'Segoe UI', sans-serif; font-size: 16px; line-height: 24px; color: #626262;">
<p
style="font-family: 'Montserrat', sans-serif; margin-bottom: 0; font-size: 20px; font-weight: 600;">
Hello</p>
<p
style="font-family: 'Montserrat', sans-serif; margin-top: 0; font-size: 24px; font-weight: 700; color: #ff5850;">
{first_name} {last_name} ({username})!</p>
<p class="sm-leading-32"
style="font-family: 'Montserrat', sans-serif; margin: 0; margin-bottom: 16px; font-size: 24px; font-weight: 600; color: #263238;">
New Account Login Detected
</p>
<p
style="font-family: 'Montserrat', sans-serif; margin: 0; margin-bottom: 24px;">
We noticed a new login to your account from a different location. If this was you, you can ignore this email. If you didn't initiate this login, please take immediate action to secure your account.
</p>
<p
style="font-family: 'Montserrat', sans-serif; margin: 0; margin-bottom: 24px;">
If you need any assistance or have any concerns, please contact our support team at
<a href="mailto:{support_mail}" class="hover-underline"
style="font-family: 'Montserrat', sans-serif; color: #7367f0; text-decoration: none;">{support_mail}</a>
</p>
<table cellpadding="0" cellspacing="0" role="presentation">
<tr>
<td
style="border-radius: 4px; background-color: #7367f0; font-family: Montserrat, -apple-system, 'Segoe UI', sans-serif;">
<a href="https://{app_url}/account"
style="font-family: 'Montserrat', sans-serif; display: block; padding-left: 24px; padding-right: 24px; padding-top: 16px; padding-bottom: 16px; font-size: 16px; font-weight: 600; line-height: 100%; color: #ffffff; text-decoration: none;">Secure Your Account &rarr;</a>
</td>
</tr>
</table>
<table style="width: 100%;" cellpadding="0" cellspacing="0"
role="presentation">
<tr>
<td
style="font-family: 'Montserrat', sans-serif; padding-top: 32px; padding-bottom: 32px;">
<div
style="font-family: 'Montserrat', sans-serif; height: 1px; background-color: #eceff1; line-height: 1px;">
&zwnj;</div>
</td>
</tr>
</table>
<p
style="font-family: 'Montserrat', sans-serif; margin: 0; margin-bottom: 16px;">
If you believe this login was unauthorized, please
<a href="mailto:{support_mail}" class="hover-underline"
style="font-family: 'Montserrat', sans-serif; color: #7367f0; text-decoration: none;">contact us immediately</a>.
</p>
<p
style="font-family: 'Montserrat', sans-serif; margin: 0; margin-bottom: 16px;">
Thank you, <br>The {app_name} Team</p>
</td>
</tr>
<tr>
<td style="font-family: 'Montserrat', sans-serif; height: 20px;"></td>
</tr>
<tr>
<td
style="padding-left: 48px; padding-right: 48px; font-family: Montserrat, -apple-system, 'Segoe UI', sans-serif; font-size: 14px; color: #eceff1;">

</td>
</tr>
<tr>
<td style="font-family: 'Montserrat', sans-serif; height: 16px;"></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</body>

</html>
Loading

0 comments on commit 2162d88

Please sign in to comment.