Jump to content
elena94

PHP email form stopped working, please help me fix it !

Recommended Posts

hi need some help, 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 = "contact@myemail.com";

$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();
?>

 



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 = "contact@myemail.com";
$headers = "From:" . $from;
echo mail ("admin@awardspace.com" ,"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 <contact@myemail.com>";
$to = "anymail@hotmail.com";
$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 "contact@myemail.com" is actually the domain email I have with the host

thanks to anyone kind enough to help out...

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Hi Jazzman, this is what I get:

 

 

contact@mydomain.com,enquiry,Form details below. Full Name: tester Email: test@gmail.com Telephone: 1234 Comments: this is a test ,From: test@gmail.com Reply-To: test@gmail.com X-Mailer: PHP/5.4.28 Thank you for contacting us, we will be in touch soon.

Share this post


Link to post
Share on other sites

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 (contact@myemail.com). According to the output that's not correct.

Edited by jazzman1

Share this post


Link to post
Share on other sites

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 ?

Share this post


Link to post
Share on other sites

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 contact@mydomain.com, check inside this mail box.

Edited by jazzman1

Share this post


Link to post
Share on other sites

nope, I get the confirmation that the email has been sent but my inbox and spam folder stay empty...

Share this post


Link to post
Share on other sites

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";

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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 by elena94

Share this post


Link to post
Share on other sites

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 - contact@myemail.com. Can you confirm that?

Edited by jazzman1

Share this post


Link to post
Share on other sites

if you are referring to these :

 

// CHANGE THE TWO LINES BELOW

 

$email_from = "contact@mydomain.com";

 

$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 by elena94

Share this post


Link to post
Share on other sites

Try the following script and give me a feedback

<?php
$email_to = 'customer_address@example.com';
$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.

Share this post


Link to post
Share on other sites

tried that script with a few different emails, always get:
 

 

MAIL FAILED

Edited by elena94

Share this post


Link to post
Share on other sites

Run the following script and let me see the output of echo command. You are doing something wrong.  

<?php
$email_to = 'customer@email.com';
$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 by jazzman1

Share this post


Link to post
Share on other sites

right, this is what I get:

 

 

customer@email.com,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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

yes I see my domain name in 2 fields, this is copied from the source:

 

 

customer@email.com,Mail subject,Mail message......,MIME-Version: 1.0
Content-type: text/plain; charset=iso-8859-1
From: Sending Name MYDOMAIN.com
Reply-To: The Reply To Name MYDOMAIN.com
X-Mailer: PHP/5.4.28

Edited by elena94

Share this post


Link to post
Share on other sites

And the name of your domain is "MYDOMAIN.com" or something else....your real domain name?

Edited by jazzman1

Share this post


Link to post
Share on other sites

my real domain name, the name of my website where the form is...I changed it to post it here...

Share this post


Link to post
Share on other sites

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 = 'tuparov86@gmail.com';
$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 = 'tuparov86@gmail.com';
$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";

tuparov86@gmail.com is my testing email, try to send me an email just for the test...

Edited by jazzman1

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.