Jump to content

Mail function doesn't work all of the time


thegate

Recommended Posts

Hello everybody,

 

I have a problem with the mail() function that pretty much drives me crazy. The weird part about the problem is that the function sometimes does and sometimes doesn't get executed. The problem occurred in many attempts with several email adresses. (Meaning that even on the same email adress it would sometimes work, and sometimes not.

 

Below I included the code, I have a feeling that it's actually not wrong but I wish to check this first before contacting my webhost.

function subscribe($email) {
$email = mysql_real_escape_string($email);
$sql = mysql_query("SELECT email FROM mailinglist WHERE email = '".$email."'") or die("error");
$check = mysql_num_rows($sql);

/* Email */
$subject = 'Twenty Words: Inschrijving.';

$headers = "From: [email protected]\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";

$message = 'Thank you for subscribing!';
/* End email */

if($check >= 1){
	$result = "You're already subscribed!";
}else if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $email)){
	$result = "That's no email adress!";
}else{
	mysql_query("INSERT INTO mailinglist(email, regTime) VALUES('".$email."',NOW())");
	$result = "thanks for subscribing!";
	mail($email, $subject, $message, $headers);
}
return $result;
}	

 

*note: $result is returned correctly, so is the query into the database. It's really just the last mail() step that sometimes does and sometimes doesn't work.

I would check the return value from mail(). It could be that your hosting provider is putting too much stress on the mail server and it's not just responding, but because you're not checking the return value from mail() you never know. I would log it with error_log() though, there's no need to tell the user there was an error when the insert still worked.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.