PHP email stopped working, please help me fix the script !

hello friends, the following PHP script on my website has stopped working without me editing it :

<?php if(isset($_POST['email'])) {
// CHANGE THE TWO LINES BELOW
$email_to = "[email protected]";

$email_subject = "enquiry";


function died($error) {
	// your error code can go here
	echo "Sorry, but errors were found in the form you submitted.<br /><br />";
	echo $error."<br /><br />";
	echo "Please go back and fix these errors.<br /><br />";
	die();
}

// validation expected data exists
if(!isset($_POST['full_name']) ||
	!isset($_POST['email']) ||
	!isset($_POST['telephone']) ||
	!isset($_POST['comments'])) {
	died('We are sorry, but there appears to be a problem with the form you submitted.');		
}

$first_name = $_POST['full_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required

$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

if(!preg_match($email_exp,$email_from)) {
$error_message .= ‘The Email Address you entered does not appear to be valid.

’;
}
$string_exp = “/^[A-Za-z .’-]+$/”;
if(!preg_match($string_exp,$first_name)) {
$error_message .= ‘The Full Name you entered does not appear to be valid.

’;
}
if(strlen($comments) < 2) {
$error_message .= ‘The Comments you entered do not appear to be valid.’;
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = “Form details below.\n\n”;

function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}

$email_message .= "Full Name: ".clean_string($first_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";

// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
‘X-Mailer: PHP/’ . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>

Thank you for contacting us, we will be in touch soon.

<?php } die(); ?>

which was working with the following form:

Full Name *
Email Address *
Telephone Number
Message *

Now, I have tried the following test script found here (http://myphpform.com/php-form-not-working.php) and it does not work :

<?php $from = "[email protected]"; $headers = "From:" . $from; echo mail ("[email protected]" ,"testmailfunction" , "Oj",$headers); ?>

so I have then contacted my Host which replied as follows:

when you send emails by scripts and it is that our SMTP server requires authentication in order to send emails out. By using one of the emails that exist in the control panel as a sender/"from" header, you will be able to authenticate yourself and the SMTP server will send without problems.

and provided this script, which works:

<? $from = "From: You "; $to = "[email protected]"; $subject = "Hi2! "; $body = "TEST";

if(mail($to,$subject,$body,$from)) echo “MAIL - OK”;
else echo “MAIL FAILED”;
?>

so can someone help me fix the code in the first quote I posted based on this last working one ?

A few things maybe worth a mention:

  • previously, about a few weeks back, this form (first two quotes) was working perfectly as I’ve posted it and sending emails to my hotmail account without a hitch…
  • in the quotes "[email protected]" is actually the domain email I have with the host

thanks to anyone kind enough to help out…

You have:

<?php $from = "[email protected]"; $headers = "From:" . $from; echo mail ("[email protected]" ,"testmailfunction" , "Oj",$headers); ?>

So, you can not echo a main function. Just us mail not “echo mail”. Unless you are attempting to
display the results from your mail function. Also, in this code, you have “Oj” as the message.
If that was just for testing, then okay. (You would get a message with just that text inside it.)

Otherwise show the code that is not working inside of PHP tags so we can copy the code and what
errors you are getting from the results.

right sorry, this is the code that is not working anymore :

[code]<?php
if(isset($_POST[‘email’])) {

   // CHANGE THE TWO LINES BELOW
   $email_to = "[email protected]";
   
   $email_subject = "enquiry";
   
   
   function died($error) {
      // your error code can go here
      echo "Sorry, but errors were found in the form you submitted.<br /><br />";
      echo $error."<br /><br />";
      echo "Please go back and fix these errors.<br /><br />";
      die();
   }
   
   // validation expected data exists
   if(!isset($_POST['full_name']) ||
      !isset($_POST['email']) ||
      !isset($_POST['telephone']) ||
      !isset($_POST['comments'])) {
      died('We are sorry, but there appears to be a problem with the form you submitted.');      
   }
   
   $first_name = $_POST['full_name']; // required
   $email_from = $_POST['email']; // required
   $telephone = $_POST['telephone']; // not required
   $comments = $_POST['comments']; // required
   
   $error_message = "";
   $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
     $error_message .= 'The Email Address you entered does not appear to be valid.<br /><br />';
  }
   $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
     $error_message .= 'The Full Name you entered does not appear to be valid.<br /><br />';
 }
  if(strlen($comments) < 2) {
     $error_message .= 'The Comments you entered do not appear to be valid.';
  }
  if(strlen($error_message) > 0) {
     died($error_message);
  }
   $email_message = "Form details below.\n\n";
   
   function clean_string($string) {
     $bad = array("content-type","bcc:","to:","cc:","href");
     return str_replace($bad,"",$string);
   }
   
   $email_message .= "Full Name: ".clean_string($first_name)."\n";
   $email_message .= "Email: ".clean_string($email_from)."\n";
   $email_message .= "Telephone: ".clean_string($telephone)."\n";
   $email_message .= "Comments: ".clean_string($comments)."\n";
   
   
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers); 
?>

<!-- place your own success html below -->

Thank you for contacting us, we will be in touch soon.

<?php
}
die();
?>[/code]

by the way for the test php you’ve posted, I’ve tried changing echo mail to mail and it still doesn’t work…
edit_I’ve got the test thing working by changing [email protected] with [email protected] which means the only way to send the email is from my own account…so i guess my question is: how to I do this in the PHP I’ve posted ?

Well, nothing jumps out to me on this code. It is badly validated, but, we can cover that later on.

First, you would need to DEBUG this code to figure out the error. One big issue is how you mail
the email out. You are suppressing the error messages. To explain: the mail function is used in
this manner: mail(arguments) to send the mail out. You have @mail(arguments) which is a valid
command, but, what it means is “suppress” all errors and try to send the email. This will not give
you any feedback of possible errors.

Also, the very first line checks to see if a posted variable had data inside it. Perhaps you are not
passing this value correctly. (You did not show the calling page.) I would suggest that you add
some debug code on the next line. Something simple like:
[php]

<?php if(isset($_POST['email'])) { die("*** GOT HERE ! ***"); [/php] What that will do is make sure that the code is actually getting to the mail section. If you see that message displayed, then we are getting to the code. If so, remove it and add this line just before we attempt to mail out the message: [php] die("
to:" . $email_to . "
subject:" . $email_subject . "
message:" . $email_message . "
headers:" . $headers); mail($email_to, $email_subject, $email_message, $headers); [/php] What this will do is display what you are attempting to mail. In this way, we can see what you are trying to send out and we can verify the data in each. This will help track down the problem. Also, obviously, you have YOUR email address in the $email_to variable, right? Let us know what you find...

thank you for your help Ernie, this is what I got with the second bit you posted:

to:[email protected] subject:enquiry message:Form details below. Full Name: test Email: [email protected] Telephone: Comments: this is a test headers:From: [email protected] Reply-To: [email protected] X-Mailer: PHP/5.4.28

Well, that is correct. Except you can not use a fake email address for either the FROM or TO fields.

Change them to your live email addresses and try again. (Don’t post your live addresses here, just
replace them with "X"s or something if you need to post them…)

I do not see anything wrong except the validation is messy and no error checking in the code.
(We can fix up these two later once it is working.)

So, I think you actually have it working, but, "[email protected]", "[email protected]" are both
most likely not valid.

Try it again with your real email address and let us know…

tried it, nope. This is what I’m saying, a few weeks ago this same form was working even with any fake email address I’d come up with (and real ones too of course) and now it isn’t.
The host says they haven’t changed anything, I thought it was the SMTP server authentication but they say it’s been like that for years.
For instance now this test PHP works:

[code]<?
$from = “From: You [email protected]”;
$to = "[email protected]";
$subject = "Hello! ";
$body = “TEST”;

if(mail($to,$subject,$body,$from)) echo “MAIL - OK”;
else echo “MAIL FAILED”;

?>[/code]

because it’s using my host account email address to send the email to get past the authentication. Any way I can apply this to my form ?

I just researched this a bit and I think it might be a header issue. Seems that headers can cause
this issue. Let’s change the way that the headers are created in your last full post and test it.
So, the old version is:
[php]
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
‘X-Mailer: PHP/’ . phpversion();
mail($email_to, $email_subject, $email_message, $headers);
[/php]
Let’s try this version…
[php]
// create email headers
$headers = “MIME-Version: 1.0” . “\r\n”;
$headers .= “Content-type: text/html; charset=iso-8859-1” . “\r\n”;
$headers .= "To: " . $email_to . “\r\n”;
$headers .= "From: " . $email_from . “\r\n”;
$headers .= "Reply-To: " . $email_from . “\r\n”;
mail($email_to, $email_subject, $email_message, $headers);
[/php]
This is basically from the PHP.NET site which is where PHP is from. It is a valid header set up to
handle HTML emails and should work for both HTML emails and for text only emails.

Not sure if this will work, but, it should be a good test to see if it actually is a header issue.
Let us know… Keep the faith, we will solve this!

PS: There is nothing different in a local test server and a live hosted server for emails. It should just
send out the mail using the server’s default mailer system. UNLESS you have a special SMTP system
set up which requires a higher level of authentication. If that is the case, I can send you instructions
on using that type of email routine. But, I think it is something more simple than that!

thanks for your patience Ernie, unfortunately your header did not work. I get the confirmation that the email has been sent but my inbox stays empty…

LOL, yikes! I hate these kinds of errors…

So, just for grins, did you check your SPAM folder? Perhaps it is going there…

negative, inbox and spam folder emptier than a banker’s heart !
going to try again just in case I made some mistakes, which is likely…

Try this code on your server…
[php]

<?PHP $email_to = "YOUR-EMAIL@YOUR-DOMAIN"; $email_from = "YOUR-EMAIL@YOUR-DOMAIN"; $email_subject = "Another email test!"; $email_message = "TESTING 1-2-3"; // create email headers $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n"; $headers .= "To: " . $email_to . "\r\n"; $headers .= "From: " . $email_from . "\r\n"; $headers .= "Reply-To: " . $email_from . "\r\n"; $results = mail($email_to, $email_subject, $email_message, $headers); die("Email Sent!
".$results); ?>

[/php]
Replace the to and from with your email address.
This is all the code you need to send an email.
Just place it into a test page and upload to your server and access it.
(If you REFRESH the page, it will send a second email…)

yes that works perfectly and super fast, received the email almost instantly…

Okay, so great! We are making headway…

Now replace your version with that one and work back to fill in the variables from the posted form.

Try this, though… Only do one field at a time and test, etc. in that way, you can find if you have
a bad field that we missed somehow. Like just replace my message text with your posted version.
Test it and then move to the subject with your version and test that. Etc…

You should be able to get it all working from there. Let us know…
(I’m leaving for a few hours and will check back in when I get home!)

Good luck!

welp…I’ll give it a try, but realistically I don’t think I’ll get far. In any case thank you very much for your guidance so far. I’ll report back in a bit…thanks again.

Sponsor our Newsletter | Privacy Policy | Terms of Service