Want to $_POST email address for mail()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

Want to $_POST email address for mail()

Post by khushbush »

I'm building a registration form where the user enters their details, such as username, email address, first name, etc, etc.
What I want the form to do is that when the user presses the submit button, the form is processed and the user is sent an email with their username and password serving as a confirmation of registration to them.
The user's details are entered into the database.

My problem here is that I want the email address to be entered into the mail() function using $_POST, but the mail() function doesn't seem to work. It keeps coming up with an error message telling me to enter a valid email address.

I am including the code for the form:

Code: Select all

 
<?php
 
include "session.php"; ?>
 
 
<html>
<title>Registration Page</title>
<?php include "menu.php";?>
<H2 align="center">
<BR>
<font face = "Arial" style ="font-size:20pt" color="#0B3A62">Register with Capital Abode</font></H2></BR>
<body>
<?
/**
 * The user has submitted the registration form and the
 * results have been processed.
 */
if(isset($_SESSION['regsuccess'])){
 
   /* Registration was successful */
   if($_SESSION['regsuccess']){
      echo "<h1>Registered!</h1>"; ?>
      <font face = "Arial" style ="font-size:12pt" color="#0B3A62">
      <?
      echo "<p>Thank you <b>".$_SESSION['reguname']."</b>, a confirmation of registration has been sent to your registered email address."
          ." You may now <a href=\"main.php\">log in</a>.</p>"; ?>
          </font>
          <?
$email = addslashes($_POST['userEmail']);
$first = addslashes($_POST['userFirstName']);
 
$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);?>
 
       <?
   }
   /* Registration failed */
   else{
   ?><font face = "Arial" style ="font-size:22pt" color="#0B3A62"><?
      echo "<h1>Registration Failed</h1>";?>
      <font face = "Arial" style ="font-size:12pt" color="#0B3A62">
      <?
      echo "<p>We're sorry, but an error has occurred and your registration for the username <b>".$_SESSION['reguname']."</b>, "
          ."could not be completed.<br>Please try again at a later time.</p>"; ?>
          </font>
          <?
   }
   unset($_SESSION['regsuccess']);
   unset($_SESSION['reguname']);
}
/**
 * The user has not filled out the registration form yet.
 * Below is the page with the sign-up form, the names
 * of the input fields are important and should not
 * be changed.
 */
else{
?>
 
<?
if($form->num_errors > 0){
   echo "<td><font size=\"2\" color=\"#ff0000\">".$form->num_errors." error(s) found</font></td>";
}
?>
 
<form action="process.php" method="POST">
<table align="center" border="0" cellspacing="0" cellpadding="3">
<tr><td><font face = "Arial" style ="font-size:11pt" color="#0B3A62">Username:</td><td><input type="text" name="username" maxlength="30" value="<? echo $form->value("username"); ?>"></td><td><? echo $form->error("username"); ?></font></td></tr>
<tr><td><font face = "Arial" style ="font-size:11pt" color="#0B3A62">First Name:</td><td><input type="text" name="userFirstName" maxlength="30" value="<? echo $form->value("userFirstName"); ?>"></td><td><? echo $form->error("userFirstName"); ?></font></td></tr>
<tr><td><font face = "Arial" style ="font-size:11pt" color="#0B3A62">Last Name:</td><td><input type="text" name="userLastName" maxlength="30" value="<? echo $form->value("userLastName"); ?>"></td><td><? echo $form->error("userLastName"); ?></font></td></tr>
<tr><td><font face = "Arial" style ="font-size:11pt" color="#0B3A62">Email:</td><td><input type="text" name="userEmail" maxlength="50" value="<? echo $form->value("userEmail"); ?>"></td><td><? echo $form->error("userEmail"); ?></font></td></tr>
<tr><td><font face = "Arial" style ="font-size:11pt" color="#0B3A62">Password:</td><td><input type="password" name="userPassword" maxlength="30" value="<? echo $form->value("userPassword"); ?>"></td><td><? echo $form->error("userPassword"); ?></font></td></tr>
<tr><td colspan="2" align="right">
<input type="hidden" name="subjoin" value="1">
<input type="submit" name = "submit" value="Join!"></td></tr>
<tr><td colspan="2" align="left"><a href="index.php"><font face = "Arial" style ="font-size:12pt" color="#0B3A62">Back to Home</font></a></td></tr>
</table>
</form>
 
<?
}
?>
 
</body>
</html>
 
How do I overcome this?

Thanks.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

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

Post by Chris Corbyn »

What error do you get?
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

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

Post by khushbush »

Warning: mail() [function.mail]: SMTP server response: 503 Must have sender and recipient first. in C:\Documents and Settings\Desktop\Project\newuser.php on line 32
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

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

Post by Chris Corbyn »

Odd, what does this show?

Code: Select all

var_dump($to);
Also, why are you using addslashes()? That could break things.
joran420
Forum Newbie
Posts: 8
Joined: Wed Apr 04, 2007 8:06 pm

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

Post by joran420 »

sounds like your running it on local host...local host doesnt usually play nice with STMP functions in my experience...try putting it on a live server.
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

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

Post by khushbush »

I tried var_dump() on $to...and it came out with NULL. What does that mean?

I've removed addslashes(), but still no solution. I keep getting the same error.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

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

Post by Chris Corbyn »

khushbush wrote:I tried var_dump() on $to...and it came out with NULL. What does that mean?

I've removed addslashes(), but still no solution. I keep getting the same error.
It means your error reporting is not high enough for a start ;)

Code: Select all

<?php error_reporting(E_ALL); ini_set('display_errors', true); ?>
NULL is what PHP treats undefined variables as. In this case $_POST['userEmail'] doesn't exist. Turn your error reporting on as shown above (always do this when programming... it will save you so much time!). Then you'll have to start debugging by echoing out what your expected variables are at different places in the code :)
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

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

Post by khushbush »

Well...I did as you said and this is what I got:


Notice: Undefined index: userEmail in C:\Documents and Settings\Desktop\Project\newuser.php on line 28
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

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

Post by John Cartwright »

khushbush wrote:Well...I did as you said and this is what I got:


Notice: Undefined index: userEmail in C:\Documents and Settings\Desktop\Project\newuser.php on line 28
So whats your question? You've identified that the variable doesn't exist (which comes from the form). So take a look at your form and add the variable.

Secondly, you should not be using input variables within verifying they exist.

I.e.

Code: Select all

$email = !empty($_POST['email']) ? $_POST['email'] : '';
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

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

Post by khushbush »

Thanks to everyone helping me so far :) ...a few queries though...

How should I add the variable to my form? Surely I've already done that, unless I'm totally missing something:

Code: Select all

 
<tr><td><font face = "Arial" style ="font-size:11pt" color="#0B3A62">Email:</td><td><input type="text" name="userEmail" maxlength="50" value="<? echo $form->value("userEmail"); ?>"></td><td><? echo $form->error("userEmail"); ?></font></td></tr>
 
And I think it's safe to say that the input variables do exist as they are registering within the database. I can register a new account within the database with no problems whatsoever, so details such as first name, last name and email addresses show up in my database when I press 'Join!'. It's getting the mail() function to capture the email address being entered by the user which is causing the biggest problem. How would one overcome that?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

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

Post by Chris Corbyn »

Are you getting this error before you ever submit the form? It doesn't look like you're checking when the form is actually submitted. You're also checking for the presence of a session variable indicating a success state before you process the data... I don't see where that's set, but maybe I've missed it.
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

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

Post by khushbush »

I get this error upon form submission.
And the success state is set on line 22 of the code in my first post:

Code: Select all

 
if($_SESSION['regsuccess']){
 
This state is originally defined in another php file called session.php.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

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

Post by Chris Corbyn »

That is not setting the session variable, it's merely checking if it evaluates to true. I still don't see where you set it.

You'll also want to change all your <? .. ?> to <?php .. ?> since the short version isn't going to be around much longer.

What does this show?

Code: Select all

var_dump($_POST);
Do you know what version of PHP you're running?
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

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

Post by khushbush »

var_dump() shows: array(0) { }

The version of PHP that I am running is 5.2.5.

I've found the session variable. It's in another file called process.php, which processes the registration form.

The variables are set as follows:

Code: Select all

 
      /* Registration Successful */
      if($retval == 0){
         $_SESSION['reguname'] = $_POST['username'];
         $_SESSION['regsuccess'] = true;
         header("Location: ".$session->referrer);
      }
 
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

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

Post by John Cartwright »

The session variable is not as important here. What Chris was getting at is you need to check that the form was submitted somehow.

hint, $_SERVER['REQUEST_METHOD']
Post Reply