Solved

Php contact us form doesnt work on php5 server

Posted on 2011-03-25
15
571 Views
Last Modified: 2012-06-21
Hi Experts,

This is probably a easy one for you experts but im not very good with php, What is it we have a contact us form which is php and has worked on previous hosting sites, now we've just switched hosting which uses PHP5 and i can't figure out why the php contact form doesn't work.

The server idicates its something to do with this line:

$MESSAGE_BODY = "Name: ".$_POST["name"]."<br>";

Please bear with me as im new to php

Can you please let me know what i've done wrong or any advise?
<?php 
if ($_POST["email"]<>'') { 
	$ToEmail = 'something@something.co.uk'; 
	$EmailSubject = 'Site contact form '; 
	$MESSAGE_BODY = "Name: ".$_POST["name"]."<br>"; 
	$MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>"; 
	$MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])."<br>"; 
	mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure"); 
?> 
Your message was sent
<?php 
} else { 
?> 
<form action="contact_us.php" method="post">
<table width="400" border="0" cellspacing="2" cellpadding="0">
<tr>
<td width="29%" class="bodytext">Your name:</td>
<td width="71%"><input name="name" type="text" id="name" size="32"></td>
</tr>
<tr>
<td class="bodytext">Email address:</td>
<td><input name="email" type="text" id="email" size="32"></td>
</tr>
<tr>
<td class="bodytext">Comment:</td>
<td><textarea name="comment" cols="45" rows="6" id="comment" class="bodytext"></textarea></td>
</tr>
<tr>
<td class="bodytext">&nbsp;</td>
<td align="left" valign="top"><input type="submit" name="Submit" value="Send"></td>
</tr>
</table>
</form> 
<?php 
}; 
?>

Open in new window

0
Comment
Question by:JackShuker
  • 8
  • 6
15 Comments
 
LVL 8

Expert Comment

by:Rik-Legger
ID: 35215174
It is working fine here,
what kind of an error do you get?
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35215219
Put this at the top

<?php 
[b]ini_set('display_errors',1);   error_reporting(E_ALL);[/b]
if ($_POST["email"]<>'') { 
	$ToEmail = 'something@something.co.uk'; 
	$EmailSubject = 'Site contact form '; 
	$MESSAGE_BODY = "Name: ".$_POST["name"]."<br>"; 
	$MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>"; 
	$MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])."<br>"; 
	mai

Open in new window


and see if any error messages pop up.
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35215225
Arrghhhh it coded my tags........  I should have previewed it!

<?php
ini_set('display_errors',1);   error_reporting(E_ALL);
if ($_POST["email"]<>'') {
      $ToEmail = 'something@something.co.uk';
      $EmailSubject = 'Site contact form ';
      $MESSAGE_BODY = "Name: ".$_POST["name"]."<br>";
      $MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>";
      $MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])."<br>";
      ....etc
0
 

Author Comment

by:JackShuker
ID: 35215494
on the site i get

Notice: Undefined index: email in E:\Domains\c\cabmakerfs.co.uk\user\htdocs\contact_us.php on line 343

and line 343 is this line $MESSAGE_BODY = "Name: ".$_POST["name"]."<br>";

will add the ini_set('display_errors',1);   error_reporting(E_ALL); now and come back
0
 

Author Comment

by:JackShuker
ID: 35215584
ok added the ini_set and tested it and got this:
Notice: Undefined variable: mailheader in E:\Domains\c\cabmakerfs.co.uk\user\htdocs\contact_us.php on line 350

Warning: mail() [function.mail]: "sendmail_from" not set in php.ini or custom "From:" header missing in E:\Domains\c\cabmakerfs.co.uk\user\htdocs\contact_us.php on line 350
Failure

now line 350 is:
<?php
thats all

0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35215613
Have you switched from a Linux server to a Windows one?
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35215656
Where is $mailheader being set? I don't see that anywhere and yet it is a parameter to your mail() function and the "From:" header is mentioned in the error message.

Mail under PHP/Windows is more complicated than under Linux - most LAMP servers come with the MTA pre-installed but on Windows you have to set it up and since I never, ever use Windows you will need input from someone who does use it.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:JackShuker
ID: 35215840
Ok i've deleted the $mailheader as you stated its not set and shouldnt be there,
the error i get now after sending a test email is:
Warning: mail() [function.mail]: "sendmail_from" not set in php.ini or custom "From:" header missing in E:\Domains\c\cabmakerfs.co.uk\user\htdocs\contact_us.php on line 350
Failure

It was from a php/windows to a php/windows the only differance is the new one is php5

im not sure what the error means by [function.mail]: "sendmail_from" not set in php.ini or custom "From:" header missing etc am i missing something? or how do i set the php ini,

Sorry im not very technical
0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 35216140
"Ok i've deleted the $mailheader as you stated its not set and shouldnt be there,"

I didn't ask you to delete it - I just asked where it was defined because you DO need it and I wanted to be sure that it was set somewhere. People often post parts of scripts here at EE so it is not unusual for variables or functions to be defined elsewhere where we cannot see them.

Set $mailheader like so

$mailheader = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

and change the emails in that fragment to match your email addresses and then put it back in the code like you originally had

mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");

That should eliminate the error and, depending on your server set up, may allow the email to go. If not then the problem may be that the windows server does not have mail enabled. This is a useful page with examples http://uk2.php.net/manual/en/function.mail.php read it and scan for Windows. In particular http://uk2.php.net/manual/en/function.mail.php#102282 and also this may help

"Note:

The Windows implementation of mail() differs in many ways from the Unix implementation. First, it doesn't use a local binary for composing messages but only operates on direct sockets which means a MTA is needed listening on a network socket (which can either on the localhost or a remote machine).

Second, the custom headers like From:, Cc:, Bcc: and Date: are not interpreted by the MTA in the first place, but are parsed by PHP.

As such, the to parameter should not be an address in the form of "Something <someone@example.com>". The mail command may not parse this properly while talking with the MTA."


0
 

Author Comment

by:JackShuker
ID: 35216397
That has worked fantastically, im able to receive emails off it now,
Just one more thing I have this on the same page and i dont know why, this is before I send a message :

Notice: Undefined index: email in E:\Domains\c\cabmakerfs.co.uk\user\htdocs\contact_us.php on line 344

Now line 344 is:
$MESSAGE_BODY = "Name: ".$_POST["name"]."<br>";

Why am I receiving this, btw you’ve been great im just sorry im not very knowledgeable in this area

Btw i have increased the points as you've been incredibly helpful
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35216504
"Undefined index" notices pop up in PHP all the time. It just means that when you execute this

$MESSAGE_BODY = "Name: ".$_POST["name"]."<br>";

the array $_POST does not have an entry whose key is "name". Notices will not stop a script executing and is $_POST['name'] does not exist then PHP assigns a null string in its place so you get a gap where you expected data.

Remember to remove or comment out the ini_set('display_errors',1);   error_reporting(E_ALL); you added earlier.
0
 

Author Comment

by:JackShuker
ID: 35216648
i have to apologise as i am a n00b so how do i stop the

Notice: Undefined index: email in E:\Domains\c\cabmakerfs.co.uk\user\htdocs\contact_us.php on line 344 from coming up then?

And thank you for reminding me about the ini_set i would have forgotten otherwise

Many thanks again
0
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 500 total points
ID: 35216751
Removing the line I pointed out should remove the error message unless it is enabled at the server level. By default PHP error messages are turned off. If the error continues to display you can either adjust your PHP.INI file by finding the line in it that has display_errors = On and changing it to Display_errors = Off or by altering the ini_set line above to read

ini_set('display_errors', 0 );

Either method should work.
0
 

Author Closing Comment

by:JackShuker
ID: 35216914
Thank you very much for all of your help bportlock. have a lovely weekend
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35216969
Have a good weekend yourself. Thanks!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now