V1.0 add email notifications
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
require_once '../../includes/connect_endpoint.php';
|
||||
session_start();
|
||||
|
||||
if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
||||
$postData = file_get_contents("php://input");
|
||||
$data = json_decode($postData, true);
|
||||
|
||||
if (
|
||||
!isset($data["days"]) || $data['days'] == "" ||
|
||||
!isset($data["smtpaddress"]) || $data["smtpaddress"] == "" ||
|
||||
!isset($data["smtpport"]) || $data["smtpport"] == "" ||
|
||||
!isset($data["smtpusername"]) || $data["smtpusername"] == "" ||
|
||||
!isset($data["smtppassword"]) || $data["smtppassword"] == ""
|
||||
) {
|
||||
$response = [
|
||||
"success" => false,
|
||||
"errorMessage" => "Please fill all fields"
|
||||
];
|
||||
echo json_encode($response);
|
||||
} else {
|
||||
$enabled = $data["enabled"];
|
||||
$days = $data["days"];
|
||||
$smtpAddress = $data["smtpaddress"];
|
||||
$smtpPort = $data["smtpport"];
|
||||
$smtpUsername = $data["smtpusername"];
|
||||
$smtpPassword = $data["smtppassword"];
|
||||
|
||||
$query = "SELECT COUNT(*) FROM notifications";
|
||||
$result = $db->querySingle($query);
|
||||
|
||||
if ($result === false) {
|
||||
$response = [
|
||||
"success" => false,
|
||||
"errorMessage" => "Error saving notifications data"
|
||||
];
|
||||
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)";
|
||||
} else {
|
||||
$query = "UPDATE notifications
|
||||
SET enabled = :enabled, days = :days, smtp_address = :smtpAddress, smtp_port = :smtpPort,
|
||||
smtp_username = :smtpUsername, smtp_password = :smtpPassword";
|
||||
}
|
||||
|
||||
$stmt = $db->prepare($query);
|
||||
$stmt->bindValue(':enabled', $enabled, SQLITE3_INTEGER);
|
||||
$stmt->bindValue(':days', $days, SQLITE3_INTEGER);
|
||||
$stmt->bindValue(':smtpAddress', $smtpAddress, SQLITE3_TEXT);
|
||||
$stmt->bindValue(':smtpPort', $smtpPort, SQLITE3_INTEGER);
|
||||
$stmt->bindValue(':smtpUsername', $smtpUsername, SQLITE3_TEXT);
|
||||
$stmt->bindValue(':smtpPassword', $smtpPassword, SQLITE3_TEXT);
|
||||
|
||||
if ($stmt->execute()) {
|
||||
$response = [
|
||||
"success" => true
|
||||
];
|
||||
echo json_encode($response);
|
||||
} else {
|
||||
$response = [
|
||||
"success" => false,
|
||||
"errorMessage" => "Error saving notification data"
|
||||
];
|
||||
echo json_encode($response);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
use PHPMailer\PHPMailer\PHPMailer;
|
||||
use PHPMailer\PHPMailer\SMTP;
|
||||
use PHPMailer\PHPMailer\Exception;
|
||||
|
||||
require_once '../../includes/connect_endpoint.php';
|
||||
session_start();
|
||||
|
||||
if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
||||
$postData = file_get_contents("php://input");
|
||||
$data = json_decode($postData, true);
|
||||
|
||||
if (
|
||||
!isset($data["smtpaddress"]) || $data["smtpaddress"] == "" ||
|
||||
!isset($data["smtpport"]) || $data["smtpport"] == "" ||
|
||||
!isset($data["smtpusername"]) || $data["smtpusername"] == "" ||
|
||||
!isset($data["smtppassword"]) || $data["smtppassword"] == ""
|
||||
) {
|
||||
$response = [
|
||||
"success" => false,
|
||||
"errorMessage" => "Please fill all fields"
|
||||
];
|
||||
echo json_encode($response);
|
||||
} else {
|
||||
require '../../libs/PHPMailer/PHPMailer.php';
|
||||
require '../../libs/PHPMailer/SMTP.php';
|
||||
require '../../libs/PHPMailer/Exception.php';
|
||||
|
||||
$smtpAddress = $data["smtpaddress"];
|
||||
$smtpPort = $data["smtpport"];
|
||||
$smtpUsername = $data["smtpusername"];
|
||||
$smtpPassword = $data["smtppassword"];
|
||||
|
||||
$mail = new PHPMailer(true);
|
||||
$mail->isSMTP();
|
||||
|
||||
$mail->Host = $smtpAddress;
|
||||
$mail->SMTPAuth = true;
|
||||
$mail->Username = $smtpUsername;
|
||||
$mail->Password = $smtpPassword;
|
||||
$mail->SMTPSecure = 'tls';
|
||||
$mail->Port = $smtpPort;
|
||||
|
||||
$getUser = "SELECT * FROM user WHERE id = 1";
|
||||
$user = $db->querySingle($getUser, true);
|
||||
$email = $user['email'];
|
||||
$name = $user['username'];
|
||||
|
||||
$mail->setFrom('wallos@wallosapp.com', 'Wallos App');
|
||||
$mail->addAddress($email, $name);
|
||||
|
||||
$mail->Subject = 'Wallos Notification';
|
||||
$mail->Body = 'This is a test notification. If you\'re seeing this, the configuration is correct.';
|
||||
|
||||
if ($mail->send()) {
|
||||
$response = [
|
||||
"success" => true,
|
||||
];
|
||||
echo json_encode($response);
|
||||
} else {
|
||||
$response = [
|
||||
"success" => false,
|
||||
"errorMessage" => "Error sending email." . $mail->ErrorInfo
|
||||
];
|
||||
echo json_encode($response);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user