homepage Welcome to WebmasterWorld Guest from 54.227.141.230
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Accredited PayPal World Seller

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?
princess




msg:1292210
 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...)

 

dingman




msg:1292211
 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.

lorax




msg:1292212
 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".

princess




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

lorax:

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?

RussellC




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

write it like this

<?
phpinfo();
?>

princess




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

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

dingman




msg:1292216
 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.

princess




msg:1292217
 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...)

Thanks!

lorax




msg:1292218
 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:

<?php
$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.

princess




msg:1292219
 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.)

lorax




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

princess,
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.

dingman




msg:1292221
 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:


<?php
if (we_got_POST_data)
{
process_form();
}
else
{
print_form()
}

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;
}
else
{
print error message;
}
}
else
{
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.

HarryM




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

princess,

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.'

dingman




msg:1292223
 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.

lorax




msg:1292224
 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.

princess




msg:1292225
 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.

princess




msg:1292226
 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?

dingman




msg:1292227
 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'];
or
global $HTTP_POST_VARS;
$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...]

princess




msg:1292228
 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?

dingman




msg:1292229
 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']}",
$message_body);

return $sent;
}

Works with or without register globals.

dingman




msg:1292230
 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.

princess




msg:1292231
 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...)

princess




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

the version is 4.2.3

dingman




msg:1292233
 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?

princess




msg:1292234
 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.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved