1

I have an Order success page where you are directed to if your order is successful and it also updates your Order Status to Success. Why is my email not sending off to the user?

<?php
    $url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

    if (strpos($url,'order-success') !== false) {
        $servername = "REMOVED";
        $username = "REMOVED";
        $password = "REMOVED";
        $dbname = "REMOVED";

        // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);

        // Check Connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }

        $sql = "UPDATE orders SET Status = 'Success' WHERE Status = 'Incomplete' AND Username = '{$_SESSION['Username']}' ORDER BY OrderID DESC LIMIT 1;";
        echo $sql;

        if ($conn->query($sql) === TRUE) {
            $to .= ''. $_SESSION['Username']. '';
            $subject = 'Order Confirmation';

            $message = '
            <html>
            <head>
                <title>Birthday Reminders for August</title>
            </head>
            <body>
                <p>Here are the birthdays upcoming in August!</p>
                <table>
                    <tr>
                        <th>Person</th>
                        <th>Day</th>
                        <th>Month</th>
                        <th>Year</th>
                    </tr>
                    <tr>
                        <td>Joe</td>
                        <td>3rd</td>
                        <td>August</td>
                        <td>1970</td>
                    </tr>
                    <tr>
                        <td>Sally</td>
                        <td>17th</td>
                        <td>August</td>
                        <td>1973</td>
                    </tr>
                </table>
            </body>
            </html>
            ';

            $headers  = 'MIME-Version: 1.0' . "\r\n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

            mail($to, $subject, $message, $headers);
        }

        else {
            session_destroy();
        }

        $conn->close();
    }
?>

Please ignore the message that I am sending, it's from an example and I'm not actually going to use this. I am simply trying to get the email to send when the Order Status is set to Success.

What is wrong?

  • Have you start session at the top of your page??? – Saty Sep 18 '15 at 08:57
  • What is the return value of the query? is $_SESSION['Username'] set? – swidmann Sep 18 '15 at 08:59
  • All of the sessions are working fine. :) –  Sep 18 '15 at 09:00
  • possible duplicate of [Why is my php script is not sending email?](http://stackoverflow.com/questions/21391777/why-is-my-php-script-is-not-sending-email) – e4c5 Sep 18 '15 at 09:00
  • `Order by` and `limit` in update query !!! – Saty Sep 18 '15 at 09:02
  • The Order By is required as it'll grab the HIGHEST value in the OrderID column, therefore being the latest one for that user. –  Sep 18 '15 at 09:04
  • 2
    Nice catch @Saty... didn't even take a proper look at the sql myself. @Dan Ashbridge: you never need an `ORDER BY` clause for an `UPDATE` statement, no matter what your intentions – kennasoft Sep 18 '15 at 09:12

5 Answers5

1

First, the mail() function returns true or false depending on success, check for that

And second, check your SPAM folder for the email, there are numerous ways to approach this problem

Community
  • 1
  • 1
BobbyTables
  • 4,481
  • 1
  • 31
  • 39
  • 1
    Additionally, run a short script with a single mail command to check whether mails are generally not sent. – syck Sep 18 '15 at 09:02
0

From your source, I see two major things that may be the problem.

  1. Check that you have started session at the top of your script using session_start() so that you can access $_SESSION['Username']. Is this what you see when you echo $sql?

    UPDATE orders SET Status = 'Success' WHERE Status = 'Incomplete' AND Username = '' ORDER BY OrderID DESC LIMIT 1;

  2. You used a strict equality check at this line if ($conn->query($sql) === TRUE) {. I doubt if $conn->query($sql) results in a boolean value, thus that block may never be reached. Use this instead:

    if ($conn->query($sql) == TRUE) {

    or simply:

    if ($conn->query($sql)) {

Check these, and see if the problem goes away!

kennasoft
  • 1,595
  • 1
  • 14
  • 26
0

The Order By is required as it'll grab the HIGHEST value in the OrderID column, therefore being the latest one for that user.

Order By and Limit is not likely to be used in an UPDATE query. It is used as a sub-query unlike the way you used it.

Instead you may use:

UPDATE orders 
SET Status = 'Success'
WHERE Status = 'Incomplete' 
AND Username = '{$_SESSION['Username']}' 
AND OrderID = ( SELECT MAX(OrderID) FROM orders WHERE Username = '{$_SESSION['Username']}' ) ;

If your objective is to update the latest record for a specific Username.

0

What are you getting? does it update after you run your script? Do this:

if(mail($to, $subject, $message, $headers)){
    redirect to the page
}else{
    echo 'Mail Error';
}

What is your Output after this?

William Madede
  • 727
  • 4
  • 8
0

The mail() function generally sends the message out using the SMTP server running on the localhost. Have you checked to be sure that there is actually an SMTP server running on this host? If so, you may want to check the SMTP server's logs, and these should shed some light on what happened to the message as the server tried to deliver it.

mti2935
  • 11,465
  • 3
  • 29
  • 33