PHP Mail not working

Hi, 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. Here is my code:

By the way, sorry if I’m re-posting this message. I tried before and it didn’t seem to work so I’m posting it again.

[php]
$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 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
				}

} else {
echo “Cannot send Confirmation link to your email address.”;
}
[/php]

Any help would be greatly appreciated

are you getting any errors?

which echos do you see on the screen?

two things i see of hand

1 - the second else is reflecting the wrong error message. It should be giving you an error about the database entry failing

2 - you need valid headers. should be (at a bare minimum)
[php]$headers = "From: " . strip_tags($email) . “\r\n”;
$headers .= “Reply-To: [email protected]\r\n”;
$headers .= “MIME-Version: 1.0\r\n”;
$headers .= “Content-Type: text/html; charset=ISO-8859-1\r\n”;[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service