Page 2 of 2

Re: Want to $_POST email address for mail()

Posted: Wed Apr 16, 2008 10:04 am
by khushbush
Is this what you mean?

Code: Select all

 
          <?php
if ($_SERVER['REQUEST_METHOD'] == "POST")
      {
$email = ($_POST['userEmail']);
$to = $email;
$subject = 'Registration Confirmation';
$body = 'Hello'. $first . ', Thank you for registering with Capital Abode.';
$headers = 'From: noreply@capitalabode.com' . "\r\n";
mail($to, $subject, $body, $headers);
}
?>
 
The mail() function still doesn't work, so I'm guessing that the form isn't being submitted. How is that possible when the user's details are being entered into the database? Surely that's a clear sign of form submission...? :?

Re: Want to $_POST email address for mail()

Posted: Wed Apr 16, 2008 1:37 pm
by andym01480
Check mail() works on your server - it was an early reply on this thread.
Something like...

Code: Select all

<?php
$to='your@email.com';
$subject='testing mail()';
$message='Dude it works!';
//some simple headers, because some servers like reply set - so change the address to one on your domain
$headers = 'From: webmaster@example.com' . "\r\n" .
   'Reply-To: webmaster@example.com' . "\r\n" .
   'X-Mailer: PHP/' . phpversion();
if (mail($to,$subject,$message,$headers)) echo "Mail works, now check your inbox!";
?>
 

Re: Want to $_POST email address for mail()

Posted: Wed Apr 16, 2008 2:02 pm
by khushbush
mail() works perfectly well on my server...I received the email immediately when I typed my email address into the code. It's trying to capture an email address entered in the form which is causing me a great deal of grief :(

Re: Want to $_POST email address for mail()

Posted: Wed Apr 16, 2008 2:54 pm
by andym01480

Code: Select all

         <?php
if ($_SERVER['REQUEST_METHOD'] == "POST")
      {
 
var_dump($_POST);
 
$email = ($_POST['userEmail']);
$to = $email;
$subject = 'Registration Confirmation';
$body = 'Hello'. $first . ', Thank you for registering with Capital Abode.';
$headers = 'From: noreply@capitalabode.com' . "\r\n";
mail($to, $subject, $body, $headers);
}
?>
and then post what is on your screen please

Re: Want to $_POST email address for mail()

Posted: Wed Apr 16, 2008 3:03 pm
by khushbush
That piece of code alone works excellently!! :) :) :)

This is what I got:

array(2) { ["userEmail"]=> string(22) "myemail@devnetwork.net" ["submit"]=> string(5) "Join!" }

(I've changed the email address...)

And I also received the email...this means that there is something wrong with other parts of the code, isn't there?

Re: Want to $_POST email address for mail()

Posted: Wed Apr 16, 2008 3:12 pm
by andym01480
Sometimes emails sent from PHP take a long time to arrive - which is irritating when testing!

Not sure what you are asking now? The var_dump checked that the userEmail was being set in the form which it is. You can remove that now. The fact you got the email means its working, doesn't it?

If it is, you now need to make it safe. If you put user input in the "to" part of a mail call without checking it is just an email address then you are opening the script up to spammers hijacking the form to send millions of email inviting us to enlarge things etc. Search for "email validation" on the forum!

Re: Want to $_POST email address for mail()

Posted: Wed Apr 16, 2008 3:17 pm
by khushbush
Oh sorry...I didn't clarify myself well enough.

I meant that just THAT piece of code (plus a very simple HTML form) worked...I received an email. But when I put that piece of code back into my original code...I get nothing :?

I guess I'll have to debug...but if you find anything that could be acting as a hindrance to the mail() function in my original code, then I would greatly appreciate it as I have spent FAR too long on this...more than I intended :(

Thanks for the help! :)

Re: Want to $_POST email address for mail()

Posted: Wed Apr 16, 2008 5:35 pm
by khushbush
ok...now this is being REALLY ANNOYING!!

Basically, the form action is set to a file called process.php, which is incredibly important to run as it contains the database queries in order to allow a user to register. However, after testing out the mail() function several times on its own and with the form, it appears that the problem lies in the form action. I cannot process the form and therefore get the registration form to send an email to a new user unless I set the form action to

Code: Select all

<?php echo $PHP_SELF;?>
which is something that I REALLY don't want to as it has taken me FOREVER to get the registration and login to work.
If I do set it to this, then I won't be able to register a new user. Is there a way around this or am I officially stuck?

Re: Want to $_POST email address for mail()

Posted: Thu Apr 17, 2008 2:33 am
by andym01480
It sounds like you are expecting form POST data to persist through more than one form.

What I mean is are you posting the $_POST['userEmail'] to the registration script and then the user submits another form, the result of which is that the userEmail has disappeared?

If so you have a structural coding problem!

Re: Want to $_POST email address for mail()

Posted: Thu Apr 17, 2008 5:50 am
by khushbush
No, it's all one form. The user enters their details into the registration form and the details are entered into the database from there, while an email is sent as soon as the form has been submitted.