Problems with PHP MySQL Login System

Hello all,

I'm trying to follow a tutorial - Create a Login System using PHP and MYSQL Database (with a forgot password function).

I copied the source code and replicated the database and I seemed to get the site mostly working on my local development server. What didn't work was the sending of the confirmation emails. I guessed this might be to do with issues / Settings with my local WAMP set up, so I moved the project onto my live server, updating the database connection settings.  

However, it still isn't working properly. I have uploaded here:
http://www.orangojapan.com/

The idea is that a new user registers and then a confirmation email is sent to the email address they registered. The user would then click on that link which gets sent to her email account and that would then validate that user (by changing the value assigned for validate next to that user).

What  appears to be happening is that the new user information is populating the appropriate table but an email is not being sent out to the entered address.  The code is below. It seems like perhaps   mail( $to, $subject, $message_body ); isn't working?

    $sql = "INSERT INTO users (first_name, last_name, email, password, hash) " 
            . "VALUES ('$first_name','$last_name','$email','$password', '$hash')";

    // Add user to the database
    if ( $mysqli->query($sql) ){

        $_SESSION['active'] = 0; //0 until user activates their account with verify.php
        $_SESSION['logged_in'] = true; // So we know the user has logged in
        $_SESSION['message'] =
                
                 "Confirmation link has been sent to $email, please verify
                 your account by clicking on the link in the message!";

        // Send registration confirmation link (verify.php)
        $to      = $email;
        $subject = 'Account Verification ( clevertechie.com )';
        $message_body = '
        Hello '.$first_name.',

        Thank you for signing up!  

        Please click this link to activate your account:

        http://localhost/login-system/verify.php?email='.$email.'&hash='.$hash;  //This is the message which is sent using the hashed strings in the link. It sends you through to verify page 

        mail( $to, $subject, $message_body ); //This is the php mail function. It takes in the strings set and sends to that email address. 

        header("location: profile.php"); //in php we redirect using the header function (can create a function which does this or just using this)

    }

Open in new window


Does anybody understand what could be the issue here? Many thanks for your time.

Regards,
Adam
AdamTrying to learn phpAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
Look at the "Additional Headers" on this page: http://php.net/manual/en/function.mail.php  Many mail servers will not send emails that do not contain them.  Try sending a simple email to yourself with this code to make sure the mail service is working.
1
RobOwner (Aidellio)Commented:
What have you got set for mail in your php.ini config file? http://php.net/manual/en/mail.configuration.php

As Dave has said, make sure the way you have configured works in an isolated test
1
hieloCommented:
At the very least you need a From: header:
 mail( $to, $subject, $message_body,"From: adam@{$_SERVER['SERVER_NAME']}" );
0
Powerful Yet Easy-to-Use Network Monitoring

Identify excessive bandwidth utilization or unexpected application traffic with SolarWinds Bandwidth Analyzer Pack.

AdamTrying to learn phpAuthor Commented:
Thanks Dave, Rob, hielo for your replies.

My php.ini file has the following settings under -

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = me@example.com

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
;sendmail_path =

; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail().
;mail.force_extra_parameters =

; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename
mail.add_x_header = On

; The path to a log file that will log all mail() calls. Log entries include
; the full path of the script, line number, To address and headers.
;mail.log =
; Log mail to syslog (Event Log on Windows).
;mail.log = syslog

Open in new window


I'm guessing the default value is selected unless stated?

As suggested, I tried to create a really basic page just testing my mail function. I tried the following and uploaded it to my live server:

<!-- test email to see if mail function is working-->

<?php

$to      = 'adamyounis@yahoo.com';
$subject = 'Test Email';
$msg = "First line of text\nSecond line of text";


// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);

// send email
mail($to,$subject,$msg);
?>

Open in new window


I just uploaded the page (which is blank) and refreshed a few times. I guess that when the page lows the mail functions is called and it should then work? Anyway, I waited for 10 minutes but no email arrived at my email address - adamyounis@yahoo.com

I then updated as follows adding a from email
<!-- test email to see if mail function is working-->

<p>test page</p>

<?php
$to = "adamyounis@yahoo.com";
$subject = "Second TEst";
$message = "Hello world!";
$headers = "From: charlienick76@yahoo.co.uk" . "\r\n" .
"CC: somebodyelse@example.com";

mail($to,$subject,$message,$headers);
?>

Open in new window


Again, nothing was being sent through. I read somewhere that the settings in my sendmail.ini could be a factor? I can't find my sendmail.ini file but will keep searching. Could this be a cause of the mail function having problems?

Many thanks for your help.
0
RobOwner (Aidellio)Commented:
You've got it configured to connect to the local server via SMTP. Are you running a mail server?
What you need is a mail server that you can relay email through (using authentication)
0
hieloCommented:
Are you on a Unix/Linux server? If so, try running the following from the command line:

sudo setsebool -P httpd_can_sendmail 1

Open in new window

0
Dave BaldwinFixer of ProblemsCommented:
The 'php.ini' settings you show are for Windows.  'sendmail' or a variant is normally used on Linux.  They're not interchangeable.  Run 'phpinfo()' on your hosting server to see what the settings are there.
0
AdamTrying to learn phpAuthor Commented:
Thanks Rob, Hielo.

I am not running a mail server - I think? I've uploaded to my shared server and am testing it on that (not locally)

Yes I looked at my server details and I am on a Linux server. I tried running from the command line after I had logged into my server - but I got an 'invalid command'. Screenshot attached. command line info

Would you advise sending email a different way (perhaps using php mailer)?

Regards,

Adam
0
hieloCommented:
Based on your screenshot, you are trying to execute the command from within an FTP session.  You need to connect via SSH and execute your command from there instead.  Since you are on a windows machine, download Putty (I typically use the "Alternative binary files > putty.exe" download option, since it doesn't require me to install it) and connect to the server.  Once connected you type the command I gave you earlier.
1
Dave BaldwinFixer of ProblemsCommented:
We really need to see his 'phpinfo()' or ('php.ini') setting for email to see if 'sendmail' should be used by PHP.  The Windows settings shown above will not work on Linux.

The two important items are:
sendmail_path      /usr/sbin/sendmail -t -i       
SMTP      relay-hosting.secureserver.net      // this is for Godaddy other hosts may have other values like 'localhost'.
0
RobOwner (Aidellio)Commented:
Further to Dave's comment, if you want email to work on your WAMP server as well as your shared server, the config will be different.  Typically on a shared server you have very little control over the php configuration so it would be best to speak to your host about how to connect your php app to their server.

The more I think about it, the easiest way I've found is to just use a PHP Library called PHPMailer (https://github.com/PHPMailer/PHPMailer/).  Just takes the running a mail server out of the equation.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dave BaldwinFixer of ProblemsCommented:
Of course, as for ALL email sending, PHPMailer requires you to have SMTP server somewhere to send email.  Sometimes you can use your own email service.  But Gmail is not found of accepting email from programs like PHPMailer.
0
AdamTrying to learn phpAuthor Commented:
Hi all,

Sorry for the delay in replying to this.  I tried starting again and using PHPMailer. I also decided to try to test this on my live shared server and not on my local WAMP server.

Also, I subsequently found the following message on my hosting company's website:

To comply with current SPAM standards involved with the PHP 'nobody' user, our newer servers that run PHP under Apache do not allow PHP to send mail via the mail() function, which is the default for most software. In order for you to be able to send mail you'll need to configure the software to use SMTP with authentication, similar to how a mail client is set up.

It then suggests using PHPMailer.  I'm assuming now that my site may be sitting on one of the newer servers. I'm now going to contact the hosting company to confirm this.  

I'm currently struggling still with PHP Mailer, but it probably makes more sense to open any queries I have with regard to that as a new question.

Many thanks for your help with this.
0
AdamTrying to learn phpAuthor Commented:
Thanks all.  Issue seemed to relate to my hosting company. I'll take the suggestion on board to use another function instead of the php mail function - probably php mailer, and see how I get on.  Cheers.
1
AdamTrying to learn phpAuthor Commented:
Hi all. Just wanted to update this post. I contacted my hosting company (InMotion Hosting) and they said that my site should work with php mail() function (on the server I'm hosted on).

Although I still intent to try using PHP mailer, I'm first going to try to get to the bottom of why php mail() isn't working for me. I'll update this POST with how I get on.

Thanks.
0
AdamTrying to learn phpAuthor Commented:
Ok - here is a final update...

I ran the following test script

<?php 
    ini_set( 'display_errors', 1 );
    error_reporting( E_ALL );
    $from = "adamyounis@yahoo.com";
    $to = "charlienick76@yahoo.co.uk";
    $subject = "PHP Mail Test script";
    $message = "This is a test to check the PHP Mail functionality";
    $headers = "From:" . $from;
    
	if(mail($to, $subject, $message, $headers)){
  	echo "Sent";
	}else{
  	echo "Failed";
	}
	
	
?>

Open in new window


It returned failed. I ran it again on another site I have which is hosted on a different server but also by the same hosting company - InMotion Hosting, and it worked. So a server issue. I contacted InMotion and they just told me not to use php mail()  as it; "...is unreliable and we discourage using it". I asked if there were any settings applied  in the one server (where the function worked) which were not applied on the other server (where the function failed) but I was just told it's an unreliable function and not to use it. Anyway, slightly annoying, not great service, but I'll now go on with the php mailer script and see how I get on with that.  Thanks all.
0
Dave BaldwinFixer of ProblemsCommented:
That's disappointing.  I had a high opinion of InMotion Hosting until now.  Their claim that it's unreliable is BS.  It is working fine on many thousands of servers around the world.  It is a very simple email function and there are many things it does not do but still...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.