Skip to content

Commit

Permalink
Added option to set From email on smtp settings
Browse files Browse the repository at this point in the history
  • Loading branch information
ellite committed Nov 16, 2023
1 parent c70859e commit 0b6246b
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/db/
/images/uploads/logos/
3 changes: 2 additions & 1 deletion endpoints/cronjobs/sendnotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
$smtpPort = $row["smtp_port"];
$smtpUsername = $row["smtp_username"];
$smtpPassword = $row["smtp_password"];
$fromEmail = $row["fromEmail"] ?? "[email protected]";
} else {
echo "Notifications are disabled. No need to run.";
}
Expand Down Expand Up @@ -70,7 +71,7 @@
$email = $user['email'];
$name = $user['username'];

$mail->setFrom('[email protected]', 'Wallos App');
$mail->setFrom($fromEmail, 'Wallos App');
$mail->addAddress($email, $name);

$mail->Subject = 'Wallos Notification';
Expand Down
3 changes: 3 additions & 0 deletions endpoints/db/migrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ function errorHandler($severity, $message, $file, $line) {
}

$allMigrations = glob('migrations/*.php');
if (count($allMigrations) == 0) {
$allMigrations = glob('../../migrations/*.php');
}
$requiredMigrations = array_diff($allMigrations, $completedMigrations);

if (count($requiredMigrations) === 0) {
Expand Down
10 changes: 6 additions & 4 deletions endpoints/notifications/save.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
) {
$response = [
"success" => false,
"errorMessage" => "Please fill all fields"
"errorMessage" => "Please fill all mandatory fields"
];
echo json_encode($response);
} else {
Expand All @@ -25,6 +25,7 @@
$smtpPort = $data["smtpport"];
$smtpUsername = $data["smtpusername"];
$smtpPassword = $data["smtppassword"];
$fromEmail = $data["fromemail"];

$query = "SELECT COUNT(*) FROM notifications";
$result = $db->querySingle($query);
Expand All @@ -37,12 +38,12 @@
echo json_encode($response);
} else {
if ($result == 0) {
$query = "INSERT INTO notifications (enabled, days, smtp_address, smtp_port, smtp_username, smtp_password)
VALUES (:enabled, :days, :smtpAddress, :smtpPort, :smtpUsername, :smtpPassword)";
$query = "INSERT INTO notifications (enabled, days, smtp_address, smtp_port, smtp_username, smtp_password, from_email)
VALUES (:enabled, :days, :smtpAddress, :smtpPort, :smtpUsername, :smtpPassword, :fromEmail)";
} else {
$query = "UPDATE notifications
SET enabled = :enabled, days = :days, smtp_address = :smtpAddress, smtp_port = :smtpPort,
smtp_username = :smtpUsername, smtp_password = :smtpPassword";
smtp_username = :smtpUsername, smtp_password = :smtpPassword, from_email = :fromEmail";
}

$stmt = $db->prepare($query);
Expand All @@ -52,6 +53,7 @@
$stmt->bindValue(':smtpPort', $smtpPort, SQLITE3_INTEGER);
$stmt->bindValue(':smtpUsername', $smtpUsername, SQLITE3_TEXT);
$stmt->bindValue(':smtpPassword', $smtpPassword, SQLITE3_TEXT);
$stmt->bindValue(':fromEmail', $fromEmail, SQLITE3_TEXT);

if ($stmt->execute()) {
$response = [
Expand Down
3 changes: 2 additions & 1 deletion endpoints/notifications/sendtestmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
$smtpPort = $data["smtpport"];
$smtpUsername = $data["smtpusername"];
$smtpPassword = $data["smtppassword"];
$fromEmail = $data["fromEmail"] ?? "[email protected]";

$mail = new PHPMailer(true);
$mail->isSMTP();
Expand All @@ -47,7 +48,7 @@
$email = $user['email'];
$name = $user['username'];

$mail->setFrom('[email protected]', 'Wallos App');
$mail->setFrom($fromEmail, 'Wallos App');
$mail->addAddress($email, $name);

$mail->Subject = 'Wallos Notification';
Expand Down
10 changes: 10 additions & 0 deletions migrations/000003.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
// This migration adds a "from_email" column to the notifications table.

/** @noinspection PhpUndefinedVariableInspection */
$columnQuery = $db->query("SELECT * FROM pragma_table_info('notifications') where name='from_email'");
$columnRequired = $columnQuery->fetchArray(SQLITE3_ASSOC) === false;

if ($columnRequired) {
$db->exec('ALTER TABLE notifications ADD COLUMN from_email VARCHAR(255);');
}
8 changes: 6 additions & 2 deletions scripts/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,14 +507,16 @@ function saveNotificationsButton() {
const smtpPort = document.getElementById("smtpport").value;
const smtpUsername = document.getElementById("smtpusername").value;
const smtpPassword = document.getElementById("smtppassword").value;
const fromEmail = document.getElementById("fromemail").value;

const data = {
enabled: enabled,
days: days,
smtpaddress: smtpAddress,
smtpport: smtpPort,
smtpusername: smtpUsername,
smtppassword: smtpPassword
smtppassword: smtpPassword,
fromemail: fromEmail
};

fetch('/endpoints/notifications/save.php', {
Expand Down Expand Up @@ -547,12 +549,14 @@ function testNotificationButton() {
const smtpPort = document.getElementById("smtpport").value;
const smtpUsername = document.getElementById("smtpusername").value;
const smtpPassword = document.getElementById("smtppassword").value;
const fromEmail = document.getElementById("fromemail").value;

const data = {
smtpaddress: smtpAddress,
smtpport: smtpPort,
smtpusername: smtpUsername,
smtppassword: smtpPassword
smtppassword: smtpPassword,
fromemail: fromEmail
};

fetch('/endpoints/notifications/sendtestmail.php', {
Expand Down
4 changes: 4 additions & 0 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
$notifications['smtp_port'] = "";
$notifications['smtp_username'] = "";
$notifications['smtp_password'] = "";
$notifications['from_email'] = "";
}

?>
Expand Down Expand Up @@ -190,6 +191,9 @@
<div class="form-group-inline">
<input type="password" name="smtppassword" id="smtppassword" placeholder="SMTP Password" value="<?= $notifications['smtp_password'] ?>" />
</div>
<div class="form-group-inline">
<input type="text" name="fromemail" id="fromemail" placeholder="From email (Optional)" value="<?= $notifications['from_email'] || "" ?>" />
</div>
<div class="settings-notes">
<p>
<i class="fa-solid fa-circle-info"></i> SMTP Password is transmitted and stored in plaintext.
Expand Down

0 comments on commit 0b6246b

Please sign in to comment.