thegate Posted May 23, 2012 Share Posted May 23, 2012 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: Contact@twentywords.nl\r\n"; $headers .= "Reply-To: Contact@twentywords.nl\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. Quote Link to comment https://forums.phpfreaks.com/topic/262983-mail-function-doesnt-work-all-of-the-time/ Share on other sites More sharing options...
Adam Posted May 23, 2012 Share Posted May 23, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/262983-mail-function-doesnt-work-all-of-the-time/#findComment-1347898 Share on other sites More sharing options...
thegate Posted May 23, 2012 Author Share Posted May 23, 2012 Thank you for the fast response, I put both a error log and return on the mail function. I noticed that even when mail() returns true (And no errors are logged) I'm not receiving the email. So now my best guess would be that the host is the problem.. Quote Link to comment https://forums.phpfreaks.com/topic/262983-mail-function-doesnt-work-all-of-the-time/#findComment-1347915 Share on other sites More sharing options...
Adam Posted May 23, 2012 Share Posted May 23, 2012 Yeah, if mail() returns true but you don't always receive the email, then you need to raise the issue with your provider. Quote Link to comment https://forums.phpfreaks.com/topic/262983-mail-function-doesnt-work-all-of-the-time/#findComment-1347917 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.