Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Php contact us form doesnt work on php5 server

Posted on 2011-03-25
15
Medium Priority
?
589 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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
 

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 2000 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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 i…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

705 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