Can't get simple mail() function to work.

sjcjcr
sjcjcr used Ask the Experts™
on
I'm just trying to email the results from a very simple form to a recipient, and whilst it appears to process fine i get no email from the form. I'm running my own apache server, just as it comes pre-configured with PHP etc on Redhat Linux. All very clean and simple, but baffling. Could it be that the server sits on a LAN and that stops it for some reason? Here is the code i'm using below:

<?php


$recipient = $_REQUEST['recipient'] ;
$subject = $_REQUEST['subject'] ;
$realname = $_REQUEST['realname'] ;
$message = $_REQUEST['message'] ;
$email = $_REQUEST['email'] ;

if ($submit) { // If they've submitted the form...



   if ($message <= "")

       $warn = "<b>Please include a message!</b><br /><br />";



if ($warn) // If we've set a message with a warning about their form entry...

   echo ($warn);



else {

   mail("$recipient",

   "Buttery Enquiry: $subject",

   "$message",

   "From: $realname <$email>");



   echo ("<b>Thanks for your feedback, I'll try to respond as soon as I can.</b><br /><br />");



      }

}

?>


<form method=post>
<input type="hidden" name="recipient" value="me@myemail.com">

Subject:
<input type=text name="subject" size=25 class="fm">

Message:<TEXTAREA name="message" cols=51 rows=5 class="fm"></TEXTAREA>

Your name:
<input type=text name="realname" size=25 class="fm"></td>

Your email:
<input type=text name="email" size=25 class="fm">

<INPUT type=submit name="submit" value="Send"></form>



Any help would be much appreciated.
Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
please try the following... you should terminate the lines at the end of the mail function

<?php


$recipient = $_REQUEST['recipient'] ;
$subject = $_REQUEST['subject'] ;
$realname = $_REQUEST['realname'] ;
$message = $_REQUEST['message'] ;
$email = $_REQUEST['email'] ;

if (isset($submit)) { // If they've submitted the form...



  if ($message <= "")

      $warn = "<b>Please include a message!</b><br /><br />";



if (isset($warn)) // If we've set a message with a warning about their form entry...

   echo ($warn);



else {

   mail("$recipient",

   "Buttery Enquiry: $subject",

   "$message",

   "From: $realname <$email>\r\n");



   echo ("<b>Thanks for your feedback, I'll try to respond as soon as I can.</b><br /><br />");



    }

}

?>


<form method=post>
<input type="hidden" name="recipient" value="me@myemail.com">

Subject:
<input type=text name="subject" size=25 class="fm">

Message:<TEXTAREA name="message" cols=51 rows=5 class="fm"></TEXTAREA>

Your name:
<input type=text name="realname" size=25 class="fm"></td>

Your email:
<input type=text name="email" size=25 class="fm">

<INPUT type=submit name="submit" value="Send"></form>
also if register globals are off use this version

notice the if ($submit) ... I also changed some stuff in the last one and this one for the isset() so notices dont flag :)

<?php


$recipient = $_REQUEST['recipient'] ;
$subject = $_REQUEST['subject'] ;
$realname = $_REQUEST['realname'] ;
$message = $_REQUEST['message'] ;
$email = $_REQUEST['email'] ;

if (isset($_POST['submit'])) { // If they've submitted the form...



  if ($message <= "")

      $warn = "<b>Please include a message!</b><br /><br />";



if (isset($warn)) // If we've set a message with a warning about their form entry...

   echo ($warn);



else {

   mail("$recipient",

   "Buttery Enquiry: $subject",

   "$message",

   "From: $realname <$email>\r\n");



   echo ("<b>Thanks for your feedback, I'll try to respond as soon as I can.</b><br /><br />");



    }

}

?>


<form method=post>
<input type="hidden" name="recipient" value="me@myemail.com">

Subject:
<input type=text name="subject" size=25 class="fm">

Message:<TEXTAREA name="message" cols=51 rows=5 class="fm"></TEXTAREA>

Your name:
<input type=text name="realname" size=25 class="fm"></td>

Your email:
<input type=text name="email" size=25 class="fm">

<INPUT type=submit name="submit" value="Send"></form>

Commented:
Question:  Do you have a mail server?  You have to have an SMTP server to use the mail() function, if I'm not mistaken.
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Author

Commented:
I just get the same blank result from your first suggestion mwillbanks, and this error in your second:

Parse error: parse error, unexpected T_STRING in /var/www/html/mailform.php on line 57

I've only got apache running as it is shipped with Redhat. Sendmail is "on" when I check server services, how to I look for SMTP?
did you add anything else into the code?
or atleast tell me which line, line 57 is within your code?

Commented:
Hmm... ok, so you're on Linux.  I don't have much experience with Linux, but are you sure sendmail is configured properly?

Do something very basic with the mail function, in its own script:

mail("here@address.com", "test", "This is a message");
sjcjcr -
  look right above line 57 and see if the thing above it has an end semi-colon.
i can also assure you that the error is not coming from the script as there is no /var/.... within the script or any settings there of.

Author

Commented:
hi guys, thanks for all this. right, i think i'm getting somewhere - i was wrong, your script does work mwillbanks, cheers. the problem is in fact in the fact that the form is rejected, as I get this email bounce:

   ----- The following addresses had permanent fatal errors -----
<sjcjcr@earthonline.co.uk>
    (reason: 550-Verification failed for <apache@localhost.localdomain>)
                                                                                                                           
   ----- Transcript of session follows -----
... while talking to ppsw.cam.ac.uk.:
>>> DATA

<<< 550-Verification failed for <apache@localhost.localdomain>
<<< 550-Unrouteable address
<<< 550 Invalid Sender Address
550 5.1.1 <sjcjcr@earthonline.co.uk>... User unknown
<<< 503 valid RCPT command must precede DATA

this explains why i dont recieve the results from the form, but how do i correct it?
550-Unrouteable address would mean that it can not find the address
550- Invalid Sender Address would mean that the sender address does not exist
...

you might need to take a look at the php.ini values:

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
;sendmail_path =

also you may want to look into configuring sendmail...
here is an address provided by RedHat
http://www.redhat.com/support/resources/howto/RH-sendmail-HOWTO/c87.html
also does apache have access to sendmail?
Commented:
Since I am way to lazy to write all the code some of you have already written one of you experts who have already written some code should look into the optional header parameter which could set the from and reply-to address.

Example provided by www.php.net at http://us2.php.net/manual/en/function.mail.php
-------------------------------------------------------------------------------------

mail("nobody@example.com", "the subject", $message,
     "From: webmaster@{$_SERVER['SERVER_NAME']}\r\n"
    ."Reply-To: webmaster@{$_SERVER['SERVER_NAME']}\r\n"
    ."X-Mailer: PHP/" . phpversion());
-------------------------------------------------------------------------------------


This should help resolve the problem without forcing him to edit any configs (dangerous for someone using redhat with default configs ;] )

Cheers,
Kevin
Commented:
Just use your username@earthonline.co.uk address as the FROM address.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial