homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

php +mail()
does it work on all unix servers?

 4:45 pm on Dec 27, 2002 (gmt 0)

I'm trying to write a code that will take a user's information and email it back to me. I've written it before (and it worked), but it was a site that was stored on my personal server (Apache.) I'm working on another site, using the same code (modified to fit the new form) and it doesn't work. (The new site is also stored on a Unix server (but not my own.)) I've debugged the php code, and so has my husband. The form is fine. And both the html form and php code match. There's no problem with the code that I can see. Is there possibly a problem with the server? Can someone please help me? (I don't know if I can post my code here...)



 4:56 pm on Dec 27, 2002 (gmt 0)

My first guess would be that PHP isn't configured propperly on the server in question. There are settings in php.ini for the path to a sendmail binary or an SMTP host to use for sending mail, and if they are wrong the mail() function breaks.

If you'd like to sticky me the code, I'll be glad to see if anything looks wrong with it to my eye, but I do suspect the server config.


 6:10 pm on Dec 27, 2002 (gmt 0)

Hello princess,
Put up a php file with phpinfo(); in it and take a looksy at the output. That will tell you if they have a mail server available to you or not. Look for "Path to sendmail".


 6:14 pm on Dec 27, 2002 (gmt 0)


I did that--made a new file called test.php, ftp'd it to the server, and looked at it online. All it said was "phpinfo();". Did I do it wrong?


 6:30 pm on Dec 27, 2002 (gmt 0)

write it like this



 6:33 pm on Dec 27, 2002 (gmt 0)

Ok, it says "/usr/lib/sendmail -t -i". What does that mean?


 6:36 pm on Dec 27, 2002 (gmt 0)

It means that to send mail from PHP the server needs Sendmail, or another mail server that provides a work-alike binary for sending mail (most do) with the same name, in the /usr/lib directory.


 6:38 pm on Dec 27, 2002 (gmt 0)

Ok, so I need to call my hosting service and see if they'll change it? (This isn't something that I can do myself...)



 6:42 pm on Dec 27, 2002 (gmt 0)

What does that mean?

It means it should work. That path indicates that SendMail is loaded and setup correctly.

Write a test page:

$send = mail('youremail@address.com','test','I'm testing the mail.');
echo $send;

If $send is one, then the mail has been sent. You should also get the mail if you put in your email address.


 6:56 pm on Dec 27, 2002 (gmt 0)

Um...that code's not working. Just make a php file with that code, put it on the server, and call it in the browser? Am I missing something? Am I supposed to have a form on a seperate page?

But I talked to my hosting service, and he's going to talk to some people at his work and see if they can figure it out...(he doesn't know much about php.)


 7:14 pm on Dec 27, 2002 (gmt 0)

Well, something appears to be broken. That code should work fine and yes, all you need to do is call the page with your browser. You don't even see a little 1 in the upper left hand corner of your screen?

If not, then we're out of options (unless someone comes up with something we've overlooked) and your host will need to resolve it. Sorry.


 7:14 pm on Dec 27, 2002 (gmt 0)

Ok, looking at your code, here are a few things I would do for starters.

First, it looks to me like you are trying to have the same file be both the submission form and the form processing agent. Is this right? If not I really don't understand your source at all.

Assuming that you do mean to have a self-processing form, I would reccomend a structure along the lines of this:

if (we_got_POST_data)

function print_form()
?> Standard HTML form here <?php

function process_form()
if (Post_data_makes_sense)
compose message;
if (mail(all those args you know))
print confirmation;
print error message;
Tell user what went wrong;

Obviously, I've left out a lot of details, and this is pseudocode that won't run, but I think you get the idea. The function calls aren't strictly necessary, but I think they are a good organizational tool and the cost is pretty minimal on such a simple script.

I think you'll find it's easier to keep track of what your script is doing if you don't try to decide which mode to be in every few lines, but rather determine that once, and then do all the work for a particular state in one place.


 7:18 pm on Dec 27, 2002 (gmt 0)


I tried Lorax' code and it didn't work, but it worked with a small correction.

Remove the apostrophe between I and m in 'I'm testing the mail.'


 7:22 pm on Dec 27, 2002 (gmt 0)

That path indicates that SendMail is loaded and setup correctly

Umm... it could perfectly well be a correct path. It might not be - it wouldn't work on some of my machines.

I'm havinging the devil's own time getting the code she sent me to run on my own server, where register_globals is turned off. However, I see nothing wrong with her syntax in the mail() command.


 7:23 pm on Dec 27, 2002 (gmt 0)

Remove the apostrophe between I and m in 'I'm testing the mail.'

Good gawd - thanks for catching that! Sorry princess.


 8:51 pm on Dec 27, 2002 (gmt 0)

lorax--the email.php file that i sent you is the file that the form calls up when it is submitted.


 8:55 pm on Dec 27, 2002 (gmt 0)

ok, the test file worked. it sent a message to my email.

but then why isn't my code working?

Here's what my hosting service told me:
Well, PHP can be used for a "mailback" form. We don't have either pop or imap compiled into php but simple sending does work.

Also, "Globals" are not enabled. Apparently, there have been numerous security risks with having them on.
What exactly does this mean, and how do I fix my code?


 9:10 pm on Dec 27, 2002 (gmt 0)

Aha! That's your problem.

What's the version number of PHP you are using? it changes the best way to do it.

Basically, what they are saying is that the "normal" configuration for PHP is to add any form variables to the global scope before you start processing - that's why you are used to just being able to use variable from the form. If register_globals is turned off, though, you have to explicitly fetch them. It is a security issue, depending on how your scripts are written.

You can deal with it pretty easily by starting the PHP code off with:

$formvar = $_POST['formvar'];
$formvar = $HTTP_POST_VARS['formvar'];

The first line is the better way, if you have a version of PHP new enough to understand it. (4.1.0 and above)

More complete info can be found at [php.net...]


 9:14 pm on Dec 27, 2002 (gmt 0)

So, the new code that you gave me--where does it go? In the page with the form, or the page that the code pulls up?


 9:18 pm on Dec 27, 2002 (gmt 0)

A very lame generic function for e-mailing the data in a form. It's ripped from a form I actually use that does some input checking before this gets called

function deliver_message(){
foreach ($_POST as $field => $contents) {
$message_body .= "$field}:\n{$contents}\n\n-=-=-=-=-=-=-=-=-=-\n\n";

$sent = mail('oneofmynames@oneofmydomains.com',
"[oneofmydomains.com contact form] {$_POST['subject']}",

return $sent;

Works with or without register globals.


 9:20 pm on Dec 27, 2002 (gmt 0)

The results page. IE, for <form method="post" action="submit.php"> that code would go in submit.php.


 9:23 pm on Dec 27, 2002 (gmt 0)

Ok, I understand that, however, what's the significance of the code you sent in the message before? (I feel really dumb...)


 9:26 pm on Dec 27, 2002 (gmt 0)

the version is 4.2.3


 9:33 pm on Dec 27, 2002 (gmt 0)

12: proposed code structure to help keep ones head clear writing a self-processing mail form.

18: Two ways to get the value from <input name="formvar" ...> into $formvar. Since you have PHP 4.2.0, use the first. It's better.

20: An example piece of code that uses the same approach as the first one in message 18, more or less, but it just grabs every value from the form rather than getting each one by name.

Make sense?


 9:40 pm on Dec 27, 2002 (gmt 0)

Ok, cool! It's making progress! It sent me an email! Thanks!

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved