1

I am trying to send mail with the following php script but the mail is not going through ....the script runs to the end but no mail is sent, What is the best way to debug if the mail is being sent?

$link = $this->db_connection();
        $enc_password = md5($password);

        //checking if the username is available in the table
        $result = mysqli_query($link, "SELECT concat(users.title,' ',users.f_name,' ',users.s_name,' ',users.o_name) as fullname, user_id,user_name,role_id,status from users WHERE email='$emailusername' or user_name='$emailusername' and password='$enc_password'");
        $user_data = mysqli_fetch_array($result, MYSQLI_BOTH);
        $count_row = mysqli_num_rows($result);
        $base_url = $this->url();
        if ($count_row == 1) {
            if ($password === "123456") {

                //set the random id length 
                $random_id_length = 10;
                $today = date("Y-m-d H:i:s");
                //generate a random id encrypt it and store it in $rnd_id 
                $rnd_id = crypt(uniqid($today, 1));
                //to remove any slashes that might have come 
                $rnd_id = strip_tags(stripslashes($rnd_id));

                //Removing any . or / and reversing the string 
                $rnd_id = str_replace(".", "", $rnd_id);
                $rnd_id = strrev(str_replace("/", "", $rnd_id));

                //finally I take the first 10 characters from the $rnd_id 
                $random_key = substr($rnd_id, 0, $random_id_length);

                $email = $user_data['email'];
                $username = $user_data['username'];
                $user_id = $user_data['user_id'];
                $full_name = $user_data['fullname'];
                $headers = "From: webmaster@emarps.org" . "\r\n" .
                        "CC: harrisdindisamuel@gmail.com";
                $subject = "Reset User Account Password";
                // the message
                $msg = '
                                                                           ---------------------
                                                                                Hey :' . $full_name . '!
                                                                                    <fieldset>
                                                                                We currently received a request for resetting your EMARPS  ACCOUNT Password. You can reset your   Personal Account Password 
                                                                                through the link below:
                                                                                <hr>
                                                                                ------------------------
                                                                                Please click this link to activate your account:<a href="' . $base_url . 'resetpassword.php?uq=/' . $random_key . '">Reset</a>


                                                                                ------------------------ ';
                ;

                // use wordwrap() if lines are longer than 70 characters
                // $msg = wordwrap($msg, 70);
                // send email
                mail($email, $subject, $msg, $headers);

            }

What is the best way to determine if the mail function is working/ if the mail is going through?

Cœur
  • 37,241
  • 25
  • 195
  • 267
H Dindi
  • 1,484
  • 6
  • 39
  • 68
  • First, have a look at what [`mail()`](http://php.net/manual/en/function.mail.php) returns to see if the function "is working". (Should return `true`.) –  Mar 16 '15 at 11:37
  • What soft is installed for sending emails on your environment? – Alexandr Lazarev Mar 16 '15 at 11:39

1 Answers1

1

Use a simple if statement:

if(mail($email, $subject, $msg, $headers)) {
    echo 'Mail send!';
}

http://php.net/manual/en/function.mail.php

Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.

You can only check if the mailserver has excepted the mail not if the mail was really delivered you can check that case only in your mailserver (mailq) or with a return mail address where all bounce mails come to and you check that inbox. Its a bit more complicated.

René Höhle
  • 26,716
  • 22
  • 73
  • 82