Simple contact form fail

Hey,

I got this script from the net but when executing the file it doesn’t really work. I just get a page with the php code printed on it.

Here is my form:


<form id="formulier" class="horizontal" method="post" action="contactprocess.php">
	<fieldset>
		<legend>Reservatie</legend>
		<div>
			<label for="ename">Naam:</label>
			<input type="text" class="text" id="ename" name="ename" />
		</div>
		<div>
			<label for="eemail">E-mail:</label>
			<input type="text" class="text" id="eemail" name="eemail" />
		</div>
		<div>
			<label for="esubject">Onderwerp:</label>
			<input type="text" class="text" id="esubject" name="esubject" />
		</div>
		<div>
			<label for="emessage">Commentaar:</label>
			<textarea cols="50" rows="6" id="emessage" name="emessage"></textarea>
		</div>
		<div>
			<input type="submit" name="esubmit" class="button" value="Verstuur" style="cursor:pointer" />
			<input type="reset" class="button" value="Annuleer" style="cursor:pointer" />
			<input type="hidden" name="ip" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>" />
		</div>
	</fieldset>
</form>

here is the contactprocess.php page:


<?php

  //Some variables
  $mymail = "email";
  $ename = $_POST['ename'];
  $eemail = $_POST['eemail'];
  $esubject = $_POST['esubject'];
  $emessage = $_POST['emessage'];
  $eip = $_POST['eip'];

  //Function to check email address
  function checkemail($eemail) { 
    if(eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\\\.[a-z]{2,4}$",$eemail))  { 
      return true; 
    } 
    else { 
      return false; 
    } 
  }

  //Mail Processing
  if ($_POST['esubmit']) {
    //Check for blank fields
    if ($ename == "" || $eemail == "" || $esubject == "" || $emessage == "") {
      echo "<p>It appears that you left a blank field.<br/> Please make sure you fill everything in.</p>";
    }
    //Check to see if the email address is valid
    else if (checkemail($eemail) == false) { 
       echo "<p>It appears that you enter an invalid email address.<br/> Please check your email again.</p>";
    }
    //Send the email if there's no error
    else {
      $body = "$emessage\
\
Name: $ename\
Email: $eemail\
Ip: $eip";
      mail($mymail,$esubject,$body,"From: $eemail\
");
      echo "<p>Thank you for your email $ename!</p>";
    }
  }

?>

What am I doing wrong?

I just get a page with the php code printed on it.

You either do not have PHP set up correctly for your server, or you are not accessing the script correctly.

I am presuming you are doing this on your own computer, which usually assumes the name “localhost” by default.

Put a test file in your htdocs folder (find out where that is by looking in your documentation OR check your HDD for php.ini and look in that for the entry next for “doc_root”)

call your file test.php


<p>This is HTML, so you found the right file ....</p>
<?php

echo "<p>PHP said this!</p>" ;

 ?>

and put it in that doc_root folder, then in your browser enter

http://localhost/test.php

OR

127.0.0.1/test.php

hm the script seems to work when i put it on a webserver, but i don’t get an e-mail in my inbox. It just displays:‘thank you for your e-mail’ but then i receive nothing…

If your problem is “my script does not work locally but it does work on another” server then your problem still remains which are you doing wrong locally.

Setting PHP up?
Accessing your scripts?

In this case stay away from mail() for now, just get PHP to echo something onto a page to prove the point - that is if you are bothered that PHP does not work locally.

Well i don’t have anything php related installed as far as i know or atleast i didn’t insall xampp* or something like that…

haha, OK so that serves me right for assuming you were testing something locally.

So, you put a script on a live server and it does not work.

So is this still true or not?

I just get a page with the php code printed on it.

Perhaps you could tell us the address of this page so we can see for ourselves.

No, what i get if i put it on a live server is an execution of the script which prompts me with the message: ‘thank you for your email.’

BUT i don’t receive an e-mail in my inbox.

Create a simple script which when loaded just sends a mail using mail();

If that fails you need to now establish why the mail() function is not working on that server.

Contact the server adminners and ask them.

Putting that form and script on a server which has mail() working will likely lead to it being abused, you should read up on mail spam

http://www.google.co.uk/search?q=php+mail+spam+protection

Hm thanks.

Any alternatives?

Is this correct for testing purposes? A script that loads when submitting a form:


<form id="formulier" class="horizontal" method="post" action="mail.php">

mail.php


<?php
$to      = 'somemail@provider.com';
$subject = 'the subject';
$message = 'hello';

mail($to, $subject, $message, $headers);
?>

EDIT: i’m hosting with byethost and it seems they have turned mail(); off… Any alternatives…?


<?php
$to      = 'somemail@provider.com';
$subject = 'the subject';
$message = 'hello';

mail($to, $subject, $message );
?>

Thats about it, put your own email as the $to, then navigate your browser to that page, that is a fundamental test of whether mail() is working.

Alternatives? Find a better hosting company.

Get a gmail account and connect and send to it using their API, there are lots of similar services but they likewise require you to learn how to use their APIs.

Anyone suggest something else?

hm I just got a reply from byethost :



We have not read the forum post but can confirm php mail is enabled on free hosting.

For best email delivery we recommend upgrading to premium hosting or using remote Google mail SMTP servers:

So that means there is something wrong with the script or … ?

Hey,

thanks for your response. I uploaded test.php to the webserver and filled in my own email, surfed to the page and got the message : “email sent”.
Sadly i never received an email - what could the issue be?

test.php


<?php
$to      = 'somemail@provider.com';
$subject = 'the subject';
$message = 'hello';

if( mail($to, $subject, $message ) ){
echo 'Mail sent' ;
}else{
echo 'Mail not sent';
}
?>

Change the email to your own email address and then put that file called test.php on your server, then navigate to it using your browser.

yourwebsite . com/test.php

From the manual on mail()
[B]Return Values

Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination. [/B]

I’ve known a few hosters require you to set a ‘from’ address for outgoing mail, they advise you to use an email address based as the sending domain.

For example, if you have the domain, http://www.thisdomain.com/, you would use:-



<?php
$to      = 'you@home.com';
$subject = 'the subject';
$message = 'hello';

ini_set('sendmail_from', 'no-reply@thisdomain.com');

if( mail($to, $subject, $message ) ){
echo 'Mail sent' ;
}else{
echo 'Mail not sent';
}
?>

Give that a whirl. :slight_smile:

Hm email accounts arent provided with the free hosting service i’m using…Still weird it won’t work when they specify php mail is enabled.

Still, try it with a dummy account using the hosting domain.

tried it but still nothing :confused:

Maybe i should also note that i have a .co.cc domain name for my byethost domain. So it is linked with some other server … Maybe this is the issue?