Trouble with my Mail Form / form 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

Post Reply
Gachet
Forum Newbie
Posts: 8
Joined: Wed Jan 30, 2008 4:21 am

Trouble with my Mail Form / form mail

Post by Gachet »

Hi

I am trying to modify a php mail form. I have added in a new field called 'telephone' but now the text I type in the text box isn't displaying in the email that I receive.

Could someone have a look and see what I have done wrong please?

Thanks


mail.php:

Code: Select all

<?php
@extract($_POST);
$name = stripslashes($name);
$email = stripslashes($email);
$subject = stripslashes(Reflexology);
$telephone = stripslashes($telephone);
$text = stripslashes($text);
 
$text = $telephone;
$text = $text;
$text .= "\n\n IP Address: " . $_SERVER['REMOTE_ADDR'];
  if ($_SERVER['HTTP_X_FORWARDED_FOR']) 
  {
  $text .= " or Proxy Reports: " . $_SERVER['HTTP_X_FORWARDED_FOR'];
  }
$text .=  "\n\nUser Agent: " . stripslashes($_SERVER['HTTP_USER_AGENT']);
mail('X@gmail.com', $subject,$text,"From: $name <$email>");
header("location:mailsent.htm"); 
?>
The HTML form:

Code: Select all

<form action="mail.php" method="post">
        <div align="center">
          <div align="left"><span >Your name: </span></div>
          <div align="left">
            <input name="name" type="text" class="text-box"  size="47%" maxlength="50">
          </div>
          <div align="left"><span>Your e-mail address: </span></div>
          <div align="left">
            <input name="email" type="text" class="text-box"  size="47%" maxlength="50">
          </div>
          <div align="left"><span >Your telephone number: </span></div>
          <div align="left">
            <input name="telephone" type="text" class="text-box" size="47%" maxlength="50">
          </div>
          <div align="left"><span>Your message:</span></div>
          <div align="left">
            <textarea name="text" cols="35%" rows="6" class="text-box" id="text" ></textarea>
          </div>
          <div align="left">
            <input name="submit" type="submit" class="button"   onClick="return document.MM_returnValue" value="send">
            <input name="Reset" type="reset" class="button" value="reset">
 
</div>
        </div>
      </form>
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Trouble with my Mail Form / form mail

Post by aceconcepts »

Just from glancing at mail.php you probably want to surround your subject (Reflexology) with quotation marks so it should look like this:

Code: Select all

$subject = stripslashes("Reflexology");
Unless its a variable, in which case you would be missing the dollar sign.
Gachet
Forum Newbie
Posts: 8
Joined: Wed Jan 30, 2008 4:21 am

Re: Trouble with my Mail Form / form mail

Post by Gachet »

Thanks.

The subject was working any ways but I have updated the file.

Any ideas why the message isn't displaying?
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Trouble with my Mail Form / form mail

Post by aceconcepts »

Take a look at lines 9 and 10

You end up setting the variable $text as $telephone.
Gachet
Forum Newbie
Posts: 8
Joined: Wed Jan 30, 2008 4:21 am

Re: Trouble with my Mail Form / form mail

Post by Gachet »

Thanks.

I have changed it to this:

Code: Select all

<?php
@extract($_POST);
$name = stripslashes($name);
$email = stripslashes($email);
$subject = stripslashes("Reflexology");
$telephone = stripslashes($telephone);
$text = stripslashes($text);
 
 
$text = $text;
$text .= "\n\n IP Address: " . $_SERVER['REMOTE_ADDR'];
  if ($_SERVER['HTTP_X_FORWARDED_FOR']) 
  {
  $text .= " or Proxy Reports: " . $_SERVER['HTTP_X_FORWARDED_FOR'];
  }
$text .=  "\n\nUser Agent: " . stripslashes($_SERVER['HTTP_USER_AGENT']);
mail('x@gmail.com', $subject,$text,[b]$telephone[/b],"From: $name <$email>");
header("location:mailsent.htm"); 
?>
The telephone number and the text box text is being displayed in the email but now the 'from' field on the email is not being filled in with what it entered in the form.

Where can I put the '$telephone' for it to be displayed in the email without it affecting anything thats already there?

Thanks
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Trouble with my Mail Form / form mail

Post by aceconcepts »

Try setting the "From" parameter in the mail() function as a single variable

i.e.:

Code: Select all

 
$from=$name . "<" . $email . ">";
 
Do the above and then insert it into your mail() function like this:

Code: Select all

mail('x@gmail.com', $subject,$text,$telephone,"-f $from");
Gachet
Forum Newbie
Posts: 8
Joined: Wed Jan 30, 2008 4:21 am

Re: Trouble with my Mail Form / form mail

Post by Gachet »

Right, it now looks like this:

Code: Select all

<?php
@extract($_POST);
$name = stripslashes($name);
$email = stripslashes($email);
$subject = stripslashes("Reflexology");
$telephone = stripslashes($telephone);
$text = stripslashes($text);
$from=$name . "<" . $email . ">";
 
 
$text = $text;
$text .= "\n\n IP Address: " . $_SERVER['REMOTE_ADDR'];
  if ($_SERVER['HTTP_X_FORWARDED_FOR']) 
  {
  $text .= " or Proxy Reports: " . $_SERVER['HTTP_X_FORWARDED_FOR'];
  }
$text .=  "\n\nUser Agent: " . stripslashes($_SERVER['HTTP_USER_AGENT']);
mail('x@gmail.com', $subject,$text,$telephone,"-f $from");
header("location:mailsent.htm"); 
?>
But its not showing the name of who it is from.

Thanks
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Trouble with my Mail Form / form mail

Post by aceconcepts »

Does it show the "from" email address?
Gachet
Forum Newbie
Posts: 8
Joined: Wed Jan 30, 2008 4:21 am

Re: Trouble with my Mail Form / form mail

Post by Gachet »

Yes it does.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Trouble with my Mail Form / form mail

Post by aceconcepts »

Ok, try amending the $from value to:

Code: Select all

$from="$name <$email>";
Gachet
Forum Newbie
Posts: 8
Joined: Wed Jan 30, 2008 4:21 am

Re: Trouble with my Mail Form / form mail

Post by Gachet »

No joy.

Now its not showing the from email address either.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Trouble with my Mail Form / form mail

Post by aceconcepts »

Ok,

Try it like this:

Code: Select all

$from=$name . "<" . $email . ">";
and take out the "-f" from the mail() function.
Gachet
Forum Newbie
Posts: 8
Joined: Wed Jan 30, 2008 4:21 am

Re: Trouble with my Mail Form / form mail

Post by Gachet »

Now the from name or email aren't showing.

Code: Select all

<?php
@extract($_POST);
$name = stripslashes($name);
$email = stripslashes($email);
$subject = stripslashes("Reflexology");
$telephone = stripslashes($telephone);
$text = stripslashes($text);
$from=$name . "<" . $email . ">";
 
 
$text = $text;
$text .= "\n\n IP Address: " . $_SERVER['REMOTE_ADDR'];
  if ($_SERVER['HTTP_X_FORWARDED_FOR']) 
  {
  $text .= " or Proxy Reports: " . $_SERVER['HTTP_X_FORWARDED_FOR'];
  }
$text .=  "\n\nUser Agent: " . stripslashes($_SERVER['HTTP_USER_AGENT']);
mail('x@gmail.com', $subject,$text,$telephone,"-f $from");
header("location:mailsent.htm"); 
?>
Last edited by Gachet on Wed Jan 30, 2008 10:48 am, edited 1 time in total.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Trouble with my Mail Form / form mail

Post by aceconcepts »

To save you and me time and yespecially your testing effort take a look at this link: http://uk.php.net/manual/en/function.mail.php and in paerticular example 4.

It should be able to help you out - I'm not palming you off :D

If you want more help with the example then reply.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Trouble with my Mail Form / form mail

Post by RobertGonzalez »

Untested...

Code: Select all

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  // instead of extracting and then applying strip slashes why not loop and apply at once?
  foreach ($_POST as $k => $v) {
    ${$k} = stripslashes($v);
  }
  $from = $email;
  $text .= "\n\n IP Address: " . $_SERVER['REMOTE_ADDR'];
  if ($_SERVER['HTTP_X_FORWARDED_FOR']) {
    $text .= " or Proxy Reports: " . $_SERVER['HTTP_X_FORWARDED_FOR'];
  }
  $text .=  "\n\nUser Agent: " . stripslashes($_SERVER['HTTP_USER_AGENT']);
  if (mail('joefylan@gmail.com', $subject, $text, "From: $from\r\n")) {
    header("Location: http://fulluriasperspec.com/mailsent.htm"); 
  } else {
    die('The mail message could not be sent.'); // This is for testing, not production use
 }
}
?>
On a side note, why not look into something like Swiftmailer?
Post Reply