1

I am picking up values and sending hidden field values through an AJAX call and then performing a query to update something in my database. The first initial query finds the id and other $_POST information and works great. However my email is not sending. My alert error message is not showing anything, but is displaying (it is a blank alert).

Does anyone see anything that is wrong with how am I am trying to send to my email or how I'm picking up the valuesin my email my email that it wouldn't send?

hidden input fields..

<input type="hidden" value="<?php echo $approved_id; ?>" id="approved_id" name="id" />
    <input type="hidden" value="<?php echo $approved_firstname; ?>" id="approved_firstname" name="firstname" />
    <input type="hidden" value="<?php echo $approved_lastname; ?>" id="approved_lastname" name="lastname" />
    <input type="hidden" value="<?php echo $approved_username; ?>" id="approved_username" name="username" />
    <input type="hidden" value="<?php echo $approved_email; ?>" id="approved_email" name="email" />

ajax call..

$(document).ready(function () {

    $('#update_group').on('submit', function (event) {
    event.preventDefault();
        $.ajax({
            url: 'user_group_update.php',
            type: 'POST',
            data: {
            id: $("#approved_id").val(), //id
            firstname: $("#approved_firstname").val(), //firstname
            lastname: $("#approved_lastname").val(), //lastname
            username: $("#approved_username").val(), //username
            email: $("#approved_email").val(), //email
           // update_group: $("#group_id").val() //group level
          update_group: $(this).find( "#group_id option:selected" ).val()
        },
            success: function (data) {
                //do something with the data that got returned
                $(".group_success").fadeIn();
                $(".group_success").show();
                $('.group_success').html('User Permission Level Changed!');
                $('.group_success').delay(5000).fadeOut(400);
                alert(data);
            },
             error: function(jqXHR, textStatus,errorThrown )
            {
              // alert on an http error 
              alert( textStatus +  errorThrown );
            }
        });
        return false;
    });
});

user_group_update.php

<?php
ini_set('log_errors', 1);
ini_set('error_log', __DIR__ . DIRECTORY_SEPARATOR . 'error.log');
error_reporting(E_ALL);
require_once 'core/init.php';

$approved_id = $_POST['id'];
//test - delete if it doesn't work
$approved_firstname = $_POST['firstname'];
$approved_lastname = $_POST['lastname'];
$approved_username = $_POST['username'];
$approved_email = $_POST['email'];
$change_group = $_POST['update_group'];

$con = mysqli_connect("localhost","","","");
    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    $stmt = $con->prepare("UPDATE users,user_requests SET users.group=?, user_requests.group=? WHERE users.id=? AND user_requests.user_id=?");
    if ( !$stmt || $con->error ) {
     // Check Errors for prepare
        die('User Group update prepare() failed: ' . htmlspecialchars($con->error));
    }
    if(!$stmt->bind_param('iiii', $change_group, $change_group, $approved_id, $approved_id)) {
    // Check errors for binding parameters
        die('User Group update bind_param() failed: ' . htmlspecialchars($stmt->error));
    }
    if(!$stmt->execute()) {
        die('User Group update execute() failed: ' . htmlspecialchars($stmt->error));
    }

    //test
    $email_stmt = $con->prepare("SELECT * FROM users WHERE id=?");
    if ( !$email_stmt || $con->error ) {
     // Check Errors for prepare
        die('User email prepare() failed: ' . htmlspecialchars($con->error));
    }
    /*if(!$email_stmt->bind_param('ii', $change_group, $approved_id)) {
    // Check errors for binding parameters
        die('User email bind_param() failed: ' . htmlspecialchars($stmt->error));
    }
    if(!$email_stmt->execute()) {
        die('User email execute() failed: ' . htmlspecialchars($stmt->error));*/
        /*
            $row = mysqli_fetch_assoc($$email_stmt);
            $pending_id = $_POST['id'];
            $group_firstname = $row['firstname'];
            $group_lastname = $row['lastname'];
            $group_username = $row['username'];
            $group_email = $row['email'];
            $group_email = $row['group'];*/

            $to = $approved_email;
            $subject = 'There is a new user request to join the Sunday Funday League';
            $message = '
            <html>
            <head>
                <title>New SFL User Request</title>
            </head>
            <body>
                <p>Hi '.$approved_firstname.',</p><br>
                <p>Your Sunday Funday League Account has been accepted. You have been added to the group. To sign in, click this link
                http://sundayfundayleague.com . </p><br>
                <p>Thank you,</p>
                <p>Administration</p>
            </body>
            </html>
            ';

            $from = "user-requests@sundayfundayleague.com";
            $Bcc = "user-requests-confirm@sundayfundayleague.com";

            // To send HTML mail, the Content-type header must be set
            $headers  = 'MIME-Version: 1.0' . "\r\n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

            // Additional headers
            //$headers .= 'To: ' .$to;//. "\r\n";
            $headers .= 'From: ' .$from; "\r\n";
            $headers .= 'Bcc: '.$Bcc; "\r\n";

            // Send the email
            mail($to,$subject,$message,$headers);
Becky
  • 2,283
  • 2
  • 23
  • 50

1 Answers1

1

The mail() function in PHP will return true or false and you'll therefore not be able to see the error in the client.

You can however use error_get_last() when mail() fails as answered here.

Since the mail() function returns true or false you'll be able to create a simple if-statement to check if it was successful:

if(mail($to,$subject,$message,$headers)){
     // success
} else { 
     print_r(error_get_last());
}

Try this to get more information about what's happening in the background.

Community
  • 1
  • 1
Lasse
  • 651
  • 2
  • 8
  • 24
  • Still getting a blank alert screen. – Becky Jul 20 '15 at 14:23
  • Could you try to `echo "success"` in if and `echo "fail"` in else, just to make sure it gets into one of them? – Lasse Jul 20 '15 at 14:34
  • I get fail in the else to show in the alert. – Becky Jul 20 '15 at 14:42
  • 1
    I've just read your code again and noticed that `$headers .= 'From: ' .$from; "\r\n";` and `$headers .= 'Bcc: '.$Bcc; "\r\n";` should be: `$headers .= 'From: ' .$from. "\r\n";` and `$headers .= 'Bcc: '.$Bcc. "\r\n";` could you try this? – Lasse Jul 20 '15 at 14:50
  • It now echos success, but it still doesn't send the email. – Becky Jul 20 '15 at 14:54
  • Okay, since you're using HTML in your mail() I have to ask if you have checked your SPAM folder in your email? – Lasse Jul 20 '15 at 14:58
  • Yes, I did. Nothing there (correct email address entered). I have close to the same html style format email in another page and it works great. – Becky Jul 20 '15 at 14:59
  • Okay, then I'm afraid that i can't help you much more. If the other page you've created works fine with mail, then you'll need to compare them and see if you can spot anything. Just to make sure, by "page", do you mean another PHP file on the same server or another website/server? – Lasse Jul 20 '15 at 15:16
  • Nevermind this actually works now. I had a huge delay in getting the emails. Thanks!! – Becky Jul 20 '15 at 15:52