0

Hi my issue is that my web application wont send an email when run. i am using an ajax function to get the details for the email from a JS file which gets the details from a form in html. i can confirm that the JS file is being run as the success alert appears. however i don't know if the mail php function is being run.

the other function of the form however is to save the data inputted to a database. which is does all ok, but doesn't mail. the $to address is taken from the session storage which is definitely present as the $contactemail from session is used to display a detial in the form.

if you have any advice on this i would be grateful.

PHP Mail Code

<?php
require_once("../../include/lock.php");     // The lock file to see if the user is logged in.

if(verifyUser()=="none"){       // If the user is not verified they will be redirected to the login 
screen.
        header("location: ".__ABSPATH__);
};

$BikeID = $_SESSION['BikeID'];// gets the bikeid from the session
$ContactEmail = $_SESSION['ContactEmail'];  // Gets the ContactEmail of the stolen bike from the 
Session.
$UpdateNotes = $_POST["UpdateNotes"];
$Status = $_POST["UpdateStatus"];

$now = new DateTime();      // Gets the current time and date.
$Time = $now->format('Y-m-d H:i:s');//converts the time and date format


$to = $ContactEmail;
$replyTo = "example@example.com";
$subject = "NoReply: Stolen Bike Investigation Update";

/* Email Headers*/
$header = "From: $replyTo,\r\n";
$header .= "Reply-To: $replyo,\r\n";
$header .= "MIME-Version: 1.0,\r\n";
$header .= "Content-Type: text/html; charset=UTF-8,\r\n";

$message = "<html><body><h1>Email Subject:$em_subject</h1>";
$message .= "<h2>Bike ID:</h2><p>$BikeID</p>";
$message .= "<h2>Time/Date of Update:</h2><p>$Time</p>";
$message .= "<p>Recently you reported a bike stolen on the BIKEIT! platform under the bike ID of 
$BikeID a new update has been added to the investigation profile.\n The update in question can be 
viewed below or alternatively you can login to the BIKEIT! platform and go to 'Check Investigation 
Updates'\n where you can see all your bikes listed and view any and all updates that have been 
uploaded to the investigaiton.\n The status of the investigaiotn can also be viewed on the platform 
and below the notes in this email.</p>";
$message .= "<h2>Update Notes:</h2><p>$UpdateNotes</p>";
$message .= "<h2>Investigation Status:</h2><p>$Status</p>";
$message .= "<p>For any querys to do with the investigaiotn at hand feel free to phone on: 
+0000000000 or email to: Example@Example.com.<br></p>";
$message .= "<h2>Update Notes:</h2><p>Please refrain from replying to this email as it is automated 
and will not be viewed, please contact the address above.</p>";
$message .= "<img src='../../resources/gloucestershire-logo-header.png'>";
$message .= "</body></html>";
$message .= wordwrap($message, 70, "\r\n");

$email = mail ($to,$subject,$message,$header);

if( $email == true ) {
    echo "Message sent successfully...";
}else {
    echo "Message could not be sent...";
}
?>

JS Code

function postStuff() { //Function poststuff
var BikeID = sessionStorage.getItem('BikeID');      // Gets the stolen bike's ID from the session.
var ContactEmail = sessionStorage.getItem('ContactEmail');      // Gets the ContactEmail from the 
session
var UpdateNotes = document.getElementById("UpdateNotes").value;     // Gets the update notes value 
from the text area element
var Status = document.getElementById("Status").value;       // Gets the status value form the select 
element
console.log(Status);

$.ajax({        // ajax, for sending data to the php file.
    url: "ConfirmUpdateDAO.php",        // The .php file where the data is going to be sent too.
    type: "POST",       // The method of how the data is going to be sent.
    data: { BikeID: BikeID, ContactEmail: ContactEmail, UpdateNotes: UpdateNotes, UpdateStatus: 
Status},        // The data to be sent.
    success: function(echoedMsg) {      // If the transfer was successful, the code below will be 
executed.
        alert(" Successfully Updated Investigation progress!");     // Informs the user that their 
bike was Successfully reported stolen.
        //location.replace("../index.php"); // Closes this page once the bike is reported stolen and 
the user clicks on the alert.
    }
});

$.ajax({        // ajax, for sending data to the php file.
    url: "EmailDAO.php",        // The .php file where the data is going to be sent too.
    type: "POST",       // The method of how the data is going to be sent.
    data: { BikeID: BikeID, ContactEmail: ContactEmail, UpdateNotes: UpdateNotes, UpdateStatus: 
Status},        // The data to be sent.
    success: function(echoedMsg) {      // If the transfer was successful, the code below will be 
executed.
        alert(" Successfully Emailed Victim The Update!");      // Informs the user that their bike 
was Successfully reported stolen.
        //location.replace("../index.php"); // Closes this page once the bike is reported stolen and 
the user clicks on the alert.
    }
});
};

HTML Code

<form>
            <?php
            session_start();
            $_SESSION['BikeID'] = $_POST['BikeID'];// assigns the value of the bikeid from updateinvestigation.php to the session under key BikeID
            $_SESSION['ContactEmail'] = $_POST['ContactEmail'];// assings the value of the ContactEmail from updateinvestigation.php to the session under key ContactEmail

            //echo below takes the values from the session and displays them.
            echo sprintf("<table><tr><td id='BikeID'>Bike ID: %s</td><td id='ContactEmail'> Contact: %s</td></tr></table>",
                $_SESSION['BikeID'],
                $_SESSION['ContactEmail']
            );

            ?>
            <br>
            <textarea type="text" placeholder="Enter Investigation Notes" class="form-control" 
            id="UpdateNotes" rows="14" cols="120"></textarea><br>
 <!--                textarea to input the update notes-->
            <p>Investigation Status: <select id="Status" name="Status">
 <!--                    select element to change the status of the investigaiton-->
                <option value="--Select--">--Select--</option>
                <option value="Open">Open</option>
                <option value="InProgress">InProgress</option>
                <option value="Closed">Closed</option>
            </select></p>
            <button onclick="location.replace('../index.php');" class="btn btn- 
            primary">Cancel</button>
 <!--                Cancel button-->

            <input type="button" value="Confirm" onclick="postStuff();" class="btn btn-primary">
 <!--                confirm button which calls the poststuff function form the confirm update js-->
        </form>
        <script src="./ConfirmUpdate.js"></script>
 <!--            finds the confirmupdate js file-->

Image of the form executed enter image description here

  • There's a million posts on PHP's `mail()` function and just how difficult it is to get working correctly. The better option is to use a library that can send via an external SMTP relay like PHPMailer + Gmail – Phil May 28 '20 at 02:28
  • Also, **always** `exit();` after sending a `Location` header – Phil May 28 '20 at 02:28

0 Answers0