From c8a062926ab8e6901c982240372a0f2a7cdb37f8 Mon Sep 17 00:00:00 2001 From: Russ Date: Sun, 5 Apr 2015 17:56:56 -0400 Subject: [PATCH] Added email functionality to event insertions and updates --- includes/insert_event.php | 32 +++++++++++++++++++++++++++----- includes/update_event.php | 36 +++++++++++++++++++++++++++++++----- 2 files changed, 58 insertions(+), 10 deletions(-) diff --git a/includes/insert_event.php b/includes/insert_event.php index 72d7410..61e4556 100644 --- a/includes/insert_event.php +++ b/includes/insert_event.php @@ -13,13 +13,35 @@ $user = mysqli_real_escape_string($conn, $_POST['user']); //Insert event to events table $event = "INSERT INTO events (unit_id, date_time_start, description, is_ongoing, date_time_end, alert_id, user) VALUES ('$unit','$start_date_time', '$description', '$is_ongoing', '$end_date_time', '$alert', '$user')"; +//MySQL queries to get Unit Name and Alert Name for the Email +$unitname_query = "SELECT unit_name FROM units WHERE unit_id=".$_POST['unit'].""; +$unitname_query_run = mysqli_query($conn, $unitname_query); +$unitname_array = mysqli_fetch_assoc($unitname_query_run); +$unitname = $unitname_array['unit_name']; +$alertname_query = "SELECT alert_name FROM alerts WHERE alert_id=".$_POST['alert'].""; +$alertname_query_run = mysqli_query($conn, $alertname_query); +$alertname_array = mysqli_fetch_assoc($alertname_query_run); +$alertname = $alertname_array['alert_name']; + +//If successful, redirect back to index.php and send email, else tell user that it failed. $result = mysqli_query($conn, $event); if($result){ - echo("Event added, redirecting..."); - sleep (2); - header('Location: ../index.php'); - exit(); + echo("Event added, redirecting..."); + sleep (2); + header('Location: ../index.php'); + //Get inserted Event ID + $event_id = $conn->insert_id; + //Set Email Info + $to = "TO-EMAIL@DOMAIN.COM"; + $subject = "New BMS Alert: ".$unitname." ".$alertname.""; + $headers = "From: FROM-EMAIL@DOMAIN.COM"; + $message = "BMS Unit: ".$unitname." \n Type of Alert: ".$alertname." \n Start Date / Time: ".$start_date_time." \n End Date / Time: ".$end_date_time." \n Description: ".$description." \n Created by: ".$user." \n Event Link: https://DOMAIN.com/bms/viewevent.php?eventid=$event_id \n \n This message generated by https://DOMAIN.com/bms"; + //WordWrap the message + $message_wrapped = wordwrap($message, 70, "\n", true); + //Send the email + mail($to,$subject,$message_wrapped,$headers); } else{ - echo('Error! Please go back and try again'); + echo('Error! Please go back and try again'); } + $conn->close(); ?> diff --git a/includes/update_event.php b/includes/update_event.php index 18e8567..00f2cd8 100644 --- a/includes/update_event.php +++ b/includes/update_event.php @@ -11,13 +11,39 @@ $user = mysqli_real_escape_string($conn, $_POST['user']); //Insert event to events table $event = "UPDATE events SET description='$description', is_ongoing='$is_ongoing', date_time_end='$end_date_time', user='$user' WHERE event_id='$event_id'"; +//Set variables for email +//MySQL queries to get Unit Name and Alert +$unitname_query = "SELECT unit_name FROM units AS units INNER JOIN events AS events ON events.unit_id=units.unit_id WHERE events.event_id=".$_POST['event'].""; +$unitname_query_run = mysqli_query($conn, $unitname_query); +$unitname_array = mysqli_fetch_assoc($unitname_query_run); +$unitname = $unitname_array['unit_name']; + +$alertname_query = "SELECT alert_name FROM alerts AS alerts INNER JOIN events AS events ON events.alert_id=alerts.alert_id WHERE events.event_id=".$_POST['event'].""; +$alertname_query_run = mysqli_query($conn, $alertname_query); +$alertname_array = mysqli_fetch_assoc($alertname_query_run); +$alertname = $alertname_array['alert_name']; + +$start_date_time_query = "SELECT date_time_start FROM events WHERE event_id=".$_POST['event'].""; +$start_date_time_query_run = mysqli_query($conn, $start_date_time_query); +$start_date_time_array = mysqli_fetch_assoc($start_date_time_query_run); +$start_date_time = $start_date_time_array['date_time_start']; +//If successful, redirect back to index.php and send email, else tell user that it failed. $result = mysqli_query($conn, $event); if($result){ - echo("Event updated, redirecting..."); - sleep (2); - header('Location: ../index.php'); - exit(); + echo("Event added, redirecting..."); + sleep (2); + header('Location: ../index.php'); + //Set Email Info + $to = "TO-EMAIL@DOMAIN.COM"; + $subject = "Updated BMS Alert: ".$unitname." ".$alertname.""; + $headers = "From: FROM-EMAIL@DOMAIN>COM"; + $message = "BMS Unit: ".$unitname." \n Type of Alert: ".$alertname." \n Start Date / Time: ".$start_date_time." \n End Date / Time: ".$end_date_time." \n Description: ".$description." \n Updated by: ".$user." \n Event Link: https://DOMAIN.com/bms/viewevent.php?eventid=$event_id \n \n This message generated by https://DOMAIN.com/bms"; + //WordWrap the message + $message_wrapped = wordwrap($message, 70, "\n", true); + //Send the email + mail($to,$subject,$message_wrapped,$headers); } else{ - echo('Error! Please go back and try again'); + echo('Error! Please go back and try again'); } + $conn->close(); ?>