elena94 Posted May 28, 2014 Share Posted May 28, 2014 hi need some help, the following PHP script on my website has stopped working without me editing it : <?phpif(isset($_POST['email'])) {// CHANGE THE TWO LINES BELOW$email_to = "[email protected]";$email_subject = "enquiry";function died($error) {// your error code can go hereecho "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 existsif(!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();?> which was working with the following form: <form name="htmlform" method="post" action="contact_uk.php"><table style="background-color:whitesmoke; border:1px solid black; border-collapse:collapse" ;="" width="542px"><tbody><tr><td style="border:1px solid black; text-align:center; font-family:calibri" valign="top"><label for="first_name">Full Name *</label></td><td style="border:1px solid black; text-align:center" valign="top"><input name="full_name" maxlength="50" size="30" type="text"></td></tr><tr><td style="border:1px solid black; text-align:center; font-family:calibri" valign="top"><label for="email">Email Address *</label></td><td style="border:1px solid black; text-align:center" valign="top"><input name="email" maxlength="80" size="30" type="text"></td></tr><tr><td style="border:1px solid black; text-align:center; font-family:calibri" valign="top"><label for="telephone">Telephone Number</label></td><td style="border:1px solid black; text-align:center" valign="top"><input name="telephone" maxlength="30" size="30" type="text"></td></tr><tr><td style="border:1px solid black; text-align:center; font-family:calibri" valign="middle"><label for="comments">Message *</label></td><td style="border:1px solid black; text-align:center" valign="top"><textarea name="comments" maxlength="1000" cols="22" rows="6"></textarea></td></tr><tr><td colspan="2" style="text-align:center"><input value="Submit" type="submit"></td></tr></tbody></table></form> 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 <[email protected]>";$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 hostthanks to anyone kind enough to help out... Quote Link to comment https://forums.phpfreaks.com/topic/288836-php-email-form-stopped-working-please-help-me-fix-it/ Share on other sites More sharing options...
jazzman1 Posted May 28, 2014 Share Posted May 28, 2014 Replace this line - @mail($email_to, $email_subject, $email_message, $headers); with - echo ($email_to.','.$email_subject.','.$email_message.','.$headers); and let me see the output. Quote Link to comment https://forums.phpfreaks.com/topic/288836-php-email-form-stopped-working-please-help-me-fix-it/#findComment-1481191 Share on other sites More sharing options...
elena94 Posted May 28, 2014 Author Share Posted May 28, 2014 Hi Jazzman, this is what I get: [email protected],enquiry,Form details below. Full Name: tester Email: [email protected] Telephone: 1234 Comments: this is a test ,From: [email protected] Reply-To: [email protected] X-Mailer: PHP/5.4.28 Thank you for contacting us, we will be in touch soon. Quote Link to comment https://forums.phpfreaks.com/topic/288836-php-email-form-stopped-working-please-help-me-fix-it/#findComment-1481195 Share on other sites More sharing options...
jazzman1 Posted May 28, 2014 Share Posted May 28, 2014 (edited) Change the headers variable in your script with mine and try again. $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/plain; charset=iso-8859-1' . "\r\n"; $headers=. "From: Sending Name <$email_from>\r\n"; $headers.= "Reply-To: The Reply To Name <$email_from>\r\n"; $headers.= "X-Mailer: PHP/" . phpversion()."\r\n"; Also, rid off in front of the php mail function this suppress (@) error operator and put the following error_reporting functions at top of the mail script you call. @mail($email_to, $email_subject, $email_message, $headers); // to if(mail($email_to, $email_subject, $email_message, $headers)) echo "MAIL - OK"; else echo "MAIL FAILED"; // at top of the file ini_set('display_startup_errors',1); ini_set('display_errors',1); error_reporting(-1); EDIT: mail_to should be the customer email address (not yours) , and mail_from and mail_reply is the mail address given by your hosting provider ([email protected]). According to the output that's not correct. Edited May 28, 2014 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/288836-php-email-form-stopped-working-please-help-me-fix-it/#findComment-1481200 Share on other sites More sharing options...
elena94 Posted May 28, 2014 Author Share Posted May 28, 2014 I pasted in your header but it produces a parse error, syntax error on this line: $headers=. "From: Sending Name <$email_from>\r\n"; sorry I'm not sure where I have to paste the second script you posted ? Quote Link to comment https://forums.phpfreaks.com/topic/288836-php-email-form-stopped-working-please-help-me-fix-it/#findComment-1481203 Share on other sites More sharing options...
jazzman1 Posted May 28, 2014 Share Posted May 28, 2014 (edited) Ops.....sorry it was a typo....the period (.) should be in front of (=) sign, like in others lines exept first one! sorry I'm not sure where I have to paste the second script you posted ? <?php ini_set('display_startup_errors',1); ini_set('display_errors',1); error_reporting(-1); if(isset($_POST['email'])) { .......... Maybe you have a lot of emails at [email protected], check inside this mail box. Edited May 28, 2014 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/288836-php-email-form-stopped-working-please-help-me-fix-it/#findComment-1481204 Share on other sites More sharing options...
elena94 Posted May 28, 2014 Author Share Posted May 28, 2014 nope, I get the confirmation that the email has been sent but my inbox and spam folder stay empty... Quote Link to comment https://forums.phpfreaks.com/topic/288836-php-email-form-stopped-working-please-help-me-fix-it/#findComment-1481210 Share on other sites More sharing options...
elena94 Posted May 28, 2014 Author Share Posted May 28, 2014 actually, forgive me my brain is turning into mush but the first line doesnt go at the end of the new header does it ? and where does the second one go ? @mail($email_to, $email_subject, $email_message, $headers); // to if(mail($email_to, $email_subject, $email_message, $headers)) echo "MAIL - OK"; else echo "MAIL FAILED"; Quote Link to comment https://forums.phpfreaks.com/topic/288836-php-email-form-stopped-working-please-help-me-fix-it/#findComment-1481212 Share on other sites More sharing options...
davidannis Posted May 28, 2014 Share Posted May 28, 2014 jazzman1 is saying change this: // 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); to this: // create email headers $headers = 'From: '.$email_from."\r\n". 'Reply-To: '.$email_from."\r\n" . 'X-Mailer: PHP/' . phpversion(); if(mail($email_to, $email_subject, $email_message, $headers)) echo "MAIL - OK"; else echo "MAIL FAILED"; Note: only last line changes - the rest is there to show you where. Quote Link to comment https://forums.phpfreaks.com/topic/288836-php-email-form-stopped-working-please-help-me-fix-it/#findComment-1481220 Share on other sites More sharing options...
elena94 Posted May 28, 2014 Author Share Posted May 28, 2014 (edited) okay, thank you davidannis This is what I get then: MAIL FAILED Thank you for contacting us, we will be in touch soon. and of course the inbox is empty... Edited May 28, 2014 by elena94 Quote Link to comment https://forums.phpfreaks.com/topic/288836-php-email-form-stopped-working-please-help-me-fix-it/#findComment-1481223 Share on other sites More sharing options...
jazzman1 Posted May 29, 2014 Share Posted May 29, 2014 (edited) No, i want to change all headers string and the mail function as shown below. // create email headers $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/plain; charset=iso-8859-1' . "\r\n"; $headers .= "From: Sending Name <$email_from>\r\n"; $headers .= "Reply-To: The Reply To Name <$email_from>\r\n"; $headers .= "X-Mailer: PHP/" . phpversion()."\r\n"; // call mail function if(mail($email_to, $email_subject, $email_message, $headers)) echo "MAIL - OK"; else echo "MAIL FAILED"; Make sure that $email_to is the customer mail address and $email_from is the email provided fro your hosting provvider some like - [email protected]. Can you confirm that? Edited May 29, 2014 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/288836-php-email-form-stopped-working-please-help-me-fix-it/#findComment-1481225 Share on other sites More sharing options...
elena94 Posted May 29, 2014 Author Share Posted May 29, 2014 (edited) if you are referring to these : // CHANGE THE TWO LINES BELOW $email_from = "[email protected]"; $email_to = "email"; $email_subject = "enquiry"; I've tried changing the $email_to to different addresses but the result is always: MAIL FAILED I can confirm I've changed the headers and call mail function directly under them like in your last post... Edited May 29, 2014 by elena94 Quote Link to comment https://forums.phpfreaks.com/topic/288836-php-email-form-stopped-working-please-help-me-fix-it/#findComment-1481247 Share on other sites More sharing options...
jazzman1 Posted May 29, 2014 Share Posted May 29, 2014 Try the following script and give me a feedback <?php $email_to = '[email protected]'; $email_from = $_SERVER['HTTP_HOST']; $email_subject = 'Mail subject'; $email_message = 'Mail message......'; // create email headers $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/plain; charset=iso-8859-1' . "\r\n"; $headers .= "From: Sending Name <$email_from>\r\n"; $headers .= "Reply-To: The Reply To Name <$email_from>\r\n"; $headers .= "X-Mailer: PHP/" . phpversion()."\r\n"; // call mail function if(mail($email_to, $email_subject, $email_message, $headers)) echo "MAIL - OK"; else echo "MAIL FAILED"; Note: change only the value of $email_to with some actual email address. Quote Link to comment https://forums.phpfreaks.com/topic/288836-php-email-form-stopped-working-please-help-me-fix-it/#findComment-1481249 Share on other sites More sharing options...
elena94 Posted May 29, 2014 Author Share Posted May 29, 2014 (edited) tried that script with a few different emails, always get: MAIL FAILED Edited May 29, 2014 by elena94 Quote Link to comment https://forums.phpfreaks.com/topic/288836-php-email-form-stopped-working-please-help-me-fix-it/#findComment-1481251 Share on other sites More sharing options...
jazzman1 Posted May 29, 2014 Share Posted May 29, 2014 (edited) Run the following script and let me see the output of echo command. You are doing something wrong. <?php $email_to = '[email protected]'; $email_from = $_SERVER['HTTP_HOST']; $email_subject = 'Mail subject'; $email_message = 'Mail message......'; // create email headers $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/plain; charset=iso-8859-1' . "\r\n"; $headers .= "From: Sending Name $email_from\r\n"; $headers .= "Reply-To: The Reply To Name $email_from\r\n"; $headers .= "X-Mailer: PHP/" . phpversion()."\r\n"; // call mail function //if(mail($email_to, $email_subject, $email_message, $headers)) echo "MAIL - OK"; else echo "MAIL FAILED"; echo ($email_to.','.$email_subject.','.$email_message.','.$headers); Edited May 29, 2014 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/288836-php-email-form-stopped-working-please-help-me-fix-it/#findComment-1481260 Share on other sites More sharing options...
elena94 Posted May 29, 2014 Author Share Posted May 29, 2014 right, this is what I get: [email protected],Mail subject,Mail message......,MIME-Version: 1.0 Content-type: text/plain; charset=iso-8859-1 From: Sending Name Reply-To: The Reply To Name X-Mailer: PHP/5.4.28 Quote Link to comment https://forums.phpfreaks.com/topic/288836-php-email-form-stopped-working-please-help-me-fix-it/#findComment-1481262 Share on other sites More sharing options...
jazzman1 Posted May 29, 2014 Share Posted May 29, 2014 I amended my script above. when you do a 'view source' in the browser, do you see the hostname? I mean the value of $email_from variable? Quote Link to comment https://forums.phpfreaks.com/topic/288836-php-email-form-stopped-working-please-help-me-fix-it/#findComment-1481263 Share on other sites More sharing options...
elena94 Posted May 29, 2014 Author Share Posted May 29, 2014 (edited) yes I see my domain name in 2 fields, this is copied from the source: [email protected],Mail subject,Mail message......,MIME-Version: 1.0Content-type: text/plain; charset=iso-8859-1From: Sending Name MYDOMAIN.comReply-To: The Reply To Name MYDOMAIN.comX-Mailer: PHP/5.4.28 Edited May 29, 2014 by elena94 Quote Link to comment https://forums.phpfreaks.com/topic/288836-php-email-form-stopped-working-please-help-me-fix-it/#findComment-1481267 Share on other sites More sharing options...
jazzman1 Posted May 29, 2014 Share Posted May 29, 2014 (edited) And the name of your domain is "MYDOMAIN.com" or something else....your real domain name? Edited May 29, 2014 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/288836-php-email-form-stopped-working-please-help-me-fix-it/#findComment-1481268 Share on other sites More sharing options...
elena94 Posted May 29, 2014 Author Share Posted May 29, 2014 my real domain name, the name of my website where the form is...I changed it to post it here... Quote Link to comment https://forums.phpfreaks.com/topic/288836-php-email-form-stopped-working-please-help-me-fix-it/#findComment-1481270 Share on other sites More sharing options...
jazzman1 Posted May 29, 2014 Share Posted May 29, 2014 (edited) yes...that's just fine....so I don't see anything wrong and I don't have any idea why you're running into a false result. Try those two solutions if they don't work maybe someone else would help you on this. 1. <?php $email_to = '[email protected]'; $email_from = $_SERVER['HTTP_HOST']; $email_subject = 'Mail subject'; $email_message = 'Mail message......'; // create email headers $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/plain; charset=iso-8859-1' . "\r\n"; $headers .= "From: <$email_from>\r\n"; $headers .= "Reply-To: <$email_from>\r\n"; $headers .= "X-Mailer: PHP/" . phpversion()."\r\n"; // call mail function if(mail($email_to, $email_subject, $email_message, $headers)) echo "MAIL - OK"; else echo "MAIL FAILED"; 2. <?php $email_to = '[email protected]'; $email_from = $_SERVER['HTTP_HOST']; $email_subject = 'Mail subject'; $email_message = 'Mail message......'; // create email headers $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/plain; charset=iso-8859-1' . "\r\n"; $headers .= "From: <$email_from>\r\n"; $headers .= "X-Mailer: PHP/" . phpversion()."\r\n"; // call mail function if(mail($email_to, $email_subject, $email_message, $headers)) echo "MAIL - OK"; else echo "MAIL FAILED"; [email protected] is my testing email, try to send me an email just for the test... Edited May 29, 2014 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/288836-php-email-form-stopped-working-please-help-me-fix-it/#findComment-1481272 Share on other sites More sharing options...
elena94 Posted May 29, 2014 Author Share Posted May 29, 2014 sorry to say, both failed....ugh this is a nightmare, I don't understand why they were working a few weeks back and now all this X__X well in any case thanks so much for your help so far I appreciate the effort Quote Link to comment https://forums.phpfreaks.com/topic/288836-php-email-form-stopped-working-please-help-me-fix-it/#findComment-1481273 Share on other sites More sharing options...
davidannis Posted May 29, 2014 Share Posted May 29, 2014 Since you changed nothing in the script and things we expect to work don't I suspect something changed on the server. Do other forms on the server send email? If so are they broken too? I would check with your ISP and see if they changed or disabled outgoing mail. Quote Link to comment https://forums.phpfreaks.com/topic/288836-php-email-form-stopped-working-please-help-me-fix-it/#findComment-1481276 Share on other sites More sharing options...
elena94 Posted May 29, 2014 Author Share Posted May 29, 2014 I contacted my hosting company and they say nothing has changed and that it was my code... will look into contacting my ISP, thanks for the suggestion Quote Link to comment https://forums.phpfreaks.com/topic/288836-php-email-form-stopped-working-please-help-me-fix-it/#findComment-1481278 Share on other sites More sharing options...
davidannis Posted May 29, 2014 Share Posted May 29, 2014 I contacted my hosting company and they say nothing has changed and that it was my code... will look into contacting my ISP, thanks for the suggestion I used ISP and hosting company interchangeably and shouldn't have. It is only the hosting company that matters. Is the site hosted on linux or windows? Quote Link to comment https://forums.phpfreaks.com/topic/288836-php-email-form-stopped-working-please-help-me-fix-it/#findComment-1481286 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.