Added way to send emails through sendgrid

This commit is contained in:
2022-01-29 12:10:52 -05:00
parent 27fc9d0937
commit b2b73842bc
9 changed files with 135 additions and 4 deletions

5
.gitignore vendored
View File

@@ -13,4 +13,7 @@ error.log
/python/fix2.sql
/html/updateInfo.log
/html/restore.json
/python/update.sh
/python/update.sh
!/html/composer.json
/html/composer.lock
/html/vendor/

35
html/api/mail.php Executable file
View File

@@ -0,0 +1,35 @@
<?php
require_once "api.php";
if (! $PRIVILEGE["mail"]) { // Makes sure that the person has the right privilege
missingPrivilege($USERNAME);
exit();
}
use PHPMailer\PHPMailer\PHPMailer;
if (array_key_exists("mail", $OGPOST) and array_key_exists("sender", $OGPOST) and array_key_exists("subject", $OGPOST) and array_key_exists("body", $OGPOST)) {
$mail = new PHPMailer(true);
// Server Settings
$mail->SMTPDebug = 0; // Prevents debugging
$mail->isSMTP(); // Enables SMTP
$mail->Host = 'smtp.sendgrid.net'; // Specify SMTP server
$mail->SMTPAuth = true; // Enable SMTP authentication
// Gets the neccessary password for the credentials
$jsonInfo = file_get_contents($_SERVER["DOCUMENT_ROOT"] . "/config.json");
$jsonData = json_decode($jsonInfo, true);
$password = $jsonData["mail"];
$mail->Username = 'apikey'; // SMTP username
$mail->Password = $password; // SMTP password
$mail->SMTPSecure = 'tls';//PHPMailer::ENCRYPTION_STARTTLS; Enable TLS encryption, `PHPMailer::ENCRYPTION_SMTPS` also accepted
$mail->Port = 587; // TCP port to connect to
// Message content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $OGPOST["subject"]; // The subject
$mail->Body = $OGPOST["body"]; // The body of the email
// Recipients
$mail->setFrom($OGPOST["sender"]); // Who to send it from
$mail->addAddress($OGPOST["mail"]); // Add a recipient
$mail->send();
} else {
http_response_code(400);
echo "Invalid command";
}

5
html/composer.json Executable file
View File

@@ -0,0 +1,5 @@
{
"require": {
"phpmailer/phpmailer": "6.5.*"
}
}

View File

@@ -143,5 +143,11 @@ input[type="number"] {
.center {
display:flex;
justify-content:center;
justify-content:center;
}
textarea {
background-color: black;
color: white;
border-radius: 4px;
}

25
html/email/index.js Executable file
View File

@@ -0,0 +1,25 @@
function render() {
$("#render").html($("#body").val());
}
$(document).ready(function() {
$("#send").button()
setInterval(render, 500);
$("#send").click(function() {
const ajax = new XMLHttpRequest;
$("#send").text("Sending");
$("#send").button("disable");
ajax.onload = function() {
if (ajax.status != 200) {
JQerror(this.responseText);
$("#send").text("Failed To Send");
} else {
$("#send").text("Sent");
}
setTimeout(function() {$("#send").text("Send");$("#send").button("enable");}, 1000)
}
ajax.open("POST", `/api/mail.php`);
ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajax.send(`mail=${encodeURI($("#reciever").val())}&sender=${encodeURI($("#sender").val())}&subject=${encodeURI($("#subject").val())}&body=${encodeURI($("#body").val())}&key='${getCookie('user')}'`);
});
});

54
html/email/index.php Executable file
View File

@@ -0,0 +1,54 @@
<!DOCTYPE html>
<html>
<head>
<title>
Schaefer Family
</title>
<?php
$DESCRIPTION = "A way to easily send emails through sendgrid.";
require_once '../include/all.php';
?>
</head>
<body>
<?php
include '../include/menu.php';
echo "<div class='main'>";
if (! $USERNAME) {
echo "<h2>You are not logged in redirecting...</h2>";
header("Refresh:3; url=/login.php", true);
http_response_code(401);
} else if (! $PRIVILEGE["docker"]) {
http_response_code(403);
header("Refresh:3; url=/index.php", true);
echo "<h2>Forbidden redirecting...</h2>";
} else {
?>
<script type="text/javascript" src="index.js"></script>
<h1>Send Emails</h1>
<label for="sender">Sending Email: </label>
<input name="sender" id="sender">
<br>
<label for="reciever">Recipient's Email: </label>
<input name="reciever" id="reciever">
<br>
<label for="subject">Subject: </label>
<input name="subject" id="subject">
<br>
<p style="color: red;">Warning do not paste unsafe input into the textbox below</p>
<label for="body">Body: </label>
<br>
<textarea name="body" id="body" style="width: 100%;" rows="10"></textarea>
<h3>HTML Render of Email</h3>
<div style="border:1px solid white;border-radius: 4px;padding: 5px;" id="render"></div>
<br>
<button id="send">Send</button>
<?php
}
?>
</div>
</body>
</html>

View File

@@ -1,4 +1,6 @@
<?php
require __DIR__ . '/../vendor/autoload.php'; # Loads all composer files
$jsonInfo = file_get_contents($_SERVER["DOCUMENT_ROOT"] . "/config.json");
$jsonData = json_decode($jsonInfo, true);
$developer = $jsonData["developer"];
@@ -275,7 +277,7 @@ foreach ($_COOKIE as $pointer => $value) {
// Removes all expired cookies from the database
$Time = time();
dbCommand("DELETE FROM cookies WHERE expire < $Time and expire != 0");
$PRIVILEGELIST = ["root", "internet", "editUser", "deleteUser", "deleteElectricity", "deleteLog", "viewLog", "changeCredintials", "deleteElectricity", "deleteError", "restartServer", "updateServer", "serverStatus", "viewBackup", "restore", "docker", "dockerAdmin"]; // A List of all possible privileges
$PRIVILEGELIST = ["root", "internet", "editUser", "deleteUser", "deleteElectricity", "deleteLog", "viewLog", "changeCredintials", "deleteElectricity", "deleteError", "restartServer", "updateServer", "serverStatus", "viewBackup", "restore", "docker", "dockerAdmin", "mail"]; // A List of all possible privileges
function noUser() { # Used to set everything up as if no yser is logged in
global $USERNAME, $PRIVILEGE, $PRIVILEGELIST;
$USERNAME = "";

View File

@@ -1,5 +1,5 @@
<?php
$MENUITEMS = [["Main Page", "/index.php"], ["Internet", "/internet/index.php"], ["Dice Game", "/diceGame/index.php"], ["Space 3", "/space3/index.php"], ["Electricity Log", "/electricity.php"], ["Floppy", "/floppy.php"], ["Soccer", "/soccer.php"], ["Golf", "/golf/index.php"], ["privilege", "docker", "/docker/index.php", "Docker Containers"], ["privilege", "dockerAdmin", "/docker/admin.php", "Docker Admin"], ["privilege", "viewLog", "/log/index.php", "Server Log"], ["privilege", "viewBackup", "/backup/index.php", "Backups"], ["Cookie Clicker Addon", "/cookieClicker/index.php"], ["user", "/usermenu/index.php", "User Menu"], ["user", "/usermenu/key.php", "Session Manager"], ["notUser", "/login.php", "Login/Signup"], ["user", "/login.php", "Logout"]];
$MENUITEMS = [["Main Page", "/index.php"], ["Internet", "/internet/index.php"], ["Dice Game", "/diceGame/index.php"], ["Space 3", "/space3/index.php"], ["Golf", "/golf/index.php"], ["Cookie Clicker Addon", "/cookieClicker/index.php"], ["Floppy", "/floppy.php"], ["Soccer", "/soccer.php"], ["privilege", "docker", "/docker/index.php", "Docker Containers"], ["privilege", "dockerAdmin", "/docker/admin.php", "Docker Admin"], ["Electricity Log", "/electricity.php"], ["privilege", "viewLog", "/log/index.php", "Server Log"], ["privilege", "viewBackup", "/backup/index.php", "Backups"], ["privilege", "mail", "/email/index.php", "Email"], ["user", "/usermenu/index.php", "User Menu"], ["user", "/usermenu/key.php", "Session Manager"], ["notUser", "/login.php", "Login/Signup"], ["user", "/login.php", "Logout"]];
echo "<div class='vertical-menu'>";
function menuItem($link, $name)
{

View File

@@ -65,6 +65,7 @@ try:
developmentMachine = False
configuration = {
"api": os.getenv("WEBSITE_API"),
"mail": os.getenv("MAIL_PASSWORD"),
"database": { "username": os.getenv("WEBSITE_USER", "admin"), "name": os.getenv("WEBSITE_DATABASE_TABLE", "website"), "password": os.getenv("WEBSITE_PASSWORD", "password"), "backupLocation": os.getenv("WEBSITE_BACKUP_LOCATION", "/backup"), "backupLength" : int(os.getenv("WEBSITE_BACKUP_LENGTH", "604800")) },
"developer": developmentMachine,
"throttle": int(os.getenv("WEBSITE_THROTTLE", "5")),