0

would anyone be able to help me with my question. I am trying to enable users to register to my site, and then login after they've verified their email address. When they register, their details get inserted in a database and once that happens, the email is supposed to be sent to their email. I find that their details are being inserted into the table, but the email is not sending. I don't know what I'm doing wrong and am at my wits end trying to figure it out and I'm not getting any error messages either. Also, I haven't been using PHP for very long. I also have my database connected and stuff. Here is my code:

$email = $_POST['email'];

$emailcheck = "SELECT email FROM useraccounts WHERE email = '$email'";

$result = $conn->query($emailcheck); // executes query $emailcheck

//if the email exists it gives an error
if ($result->num_rows != 0) {
    echo "Sorry, the email $email is already in use.";
}

// check to see if both passwords entered match     
if ($_POST['password'] != $_POST['confirmpassword']) {
    echo "Your passwords did not match. Please try again.";
}

// to encrypt the password
$passhash = sha1($_POST['password']);

// to make confirm_code
$confirm_code = sha1($email);

if ($result->num_rows == 0 && $_POST['password'] == $_POST['confirmpassword']) {

    $insert = "INSERT INTO useraccounts (confirm_code, email, username, passhash, 
    firstname, lastname, gender, phone, address, userlevel, confirmed) 
    VALUES ('$confirm_code', '$email', '$_POST[username]', '$passhash', '$_POST[firstname]',
    '$_POST[lastname]', '$_POST[gender]', '$_POST[phone]', '$_POST[address]', 'user', 'false')";

    $add_member = $conn->query($insert);  // executes query $insert (adds user to table)

    //send confirmation email if user is added to useraccounts
    if($add_member) {

        //send email to
        $to = $email;  // $email = $_POST['email'] is present at top

        //email subject
        $subject = 'Registration Confirmation';

        //email message
        $message = 'my message goes here';

        $headers = 'From myname';

        //send email
        $sentmail = mail($to,$subject,$message,$headers);

        //if email is sent
        if($sentmail){
            echo "Your Confirmation link has been sent to your email address.";
        } else {
            echo "Cannot send Confirmation link to your email address."; // this statement is the output
        } // end if email is sent
    } else {
        echo "Cannot send Confirmation link to your email address.";
    } // end if member is added
}

Any help would be greatly appreciated :)

o.o
  • 143
  • 1
  • 5
  • 11
  • What you are doing is **wide open** to SQL injection and **you will be hacked** if you haven't been already. Learn to use prepared queries with PDO to avoid this problem entirely. – Brad Aug 19 '12 at 00:34
  • I haven't done much PDO in the past so don't really know how to alter my code so that it uses it. Any suggestions? And I'm also pressed for time. – o.o Aug 19 '12 at 00:48
  • http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html – Brad Aug 19 '12 at 01:15
  • The issues you are having are with the configuration of the mail server, I suggest using a SMTP class to authenticate with the mail server and actually send the email (PHP's `mail()` function is **extremely** problematic, for many reasons). The problem with e-mail is not in your code, which makes this question extremely localized. You can find several good SMTP libraries on github, try using one of those and it should work. I'm closing this in the meantime. – Tim Post Aug 20 '12 at 01:47

2 Answers2

4
$headers = 'From myname';

This is an invalid header line. It should be:

$headers = 'From: myname';

This is probably causing a mail daemon or server to reject the message as ill-formed.

Jeremy
  • 1
  • 85
  • 340
  • 366
  • I tried that, but it still didn't work – o.o Aug 19 '12 at 00:46
  • Am I supposed to provide an actual email address, or is what I have alright? – o.o Aug 19 '12 at 01:14
  • @Swe `myname` is a legal way to refer to a user `myname` on the local server. Most servers should accept it, but it's possible that some may not. – Jeremy Aug 19 '12 at 01:16
  • I'm not too sure what the server is, so 'myname' might not actually be valid. Do you know how I can find out about my server? – o.o Aug 19 '12 at 01:20
0

Is your mail server configured as an open relay? You're trying to send an unauthenticated email. Most mail servers don't allow that.

Check this out.

Community
  • 1
  • 1
hardc0ded
  • 1
  • 5
  • I'm not sure what you mean. I don't know what mail server I am using so don't really know anything about it. – o.o Aug 19 '12 at 00:45
  • If you (or your webhosting) don't have a properly configured mail server, you won't be able to send emails using that method. [Wikipedia for Open Relay](http://en.wikipedia.org/wiki/Open_mail_relay) – hardc0ded Aug 19 '12 at 15:57