Jump to content


Photo

Simple email form problem


  • Please log in to reply
12 replies to this topic

#1 CSpoon

CSpoon
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 14 September 2006 - 08:58 PM

Hello, Newbie here...

Just made an email form which works,
except that I am not getting the subject
that I want in the email.

I "hard-wired" it to say "Catalogue request"
but somehow it returns the user's email address.
Must be something obvious I am missing?

Also, is it possible to change the "From" email
address (currently it's "httpd@foxtrot3.whsecure.net")
to the user's email address?

Thanks for any input.
C.

<?php
  if ($_SERVER['REQUEST_METHOD'] != 'POST'){
      $me = $_SERVER['PHP_SELF'];
?>
  <form name="form1" method="post"
        action="<?php echo $me;?>">
      <table border="0" cellspacing="0" cellpadding="2">
        <tr>
            <td><font color="#666666" size="2" face="Verdana, Arial, Helvetica, sans-serif">Name</font></td>
            <td><input type="text" name="Name"></td>
        </tr>
        <tr>
            <td><font color="#666666" size="2" face="Verdana, Arial, Helvetica, sans-serif">Email</font></td>
            <td><input type="text" name="email"></td>
        </tr>
        <tr>
            <td valign="top"><font color="#666666" size="2" face="Verdana, Arial, Helvetica, sans-serif">Mailing
              Address </font></td>
            <td><textarea name="MsgBody"></textarea></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><input type="submit" name="Submit"
              value="Send"></td>
        </tr>
      </table>
  </form>
<?php
  } else {
      error_reporting(0);
      $recipient = 'myemail@address.com';
      $subject = 'Catalogue request';
      $from = stripslashes($_POST['Name']);
      $msg = "$from\n\n $email\n\n".stripslashes($_POST['MsgBody']);
      if (mail($recipient, $email, $msg))
        echo nl2br("<font color=#339900 size= 2 face='Verdana, Arial, Helvetica, sans-serif'><strong><br>
Thanks for your request!</strong></font><font color=#339900 size= 2 face='Verdana, Arial, Helvetica, sans-serif'>
A catalogue will be sent to: <br>
        $msg
        ");
      else
        echo "Message failed to send, please try again later or contact our office.";
}
?>


#2 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 14 September 2006 - 09:16 PM

The from is not mentioned in the mail() function
you can have something like
      $recipient = 'myemail@address.com';
      $subject = 'Catalogue request';
      $msg = "$from\n\n $email\n\n".stripslashes($_POST['MsgBody']);
      $header = 'From: ".stripslashes($_POST['Name']);
      if (mail($recipient, $email, $msg, header))
Tell me the problem, I will try tell you the solution

#3 CSpoon

CSpoon
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 15 September 2006 - 12:55 PM

The from is not mentioned in the mail() function
you can have something like
      $recipient = 'myemail@address.com';
      $subject = 'Catalogue request';
      $msg = "$from\n\n $email\n\n".stripslashes($_POST['MsgBody']);
      $header = 'From: ".stripslashes($_POST['Name']);
      if (mail($recipient, $email, $msg, header))

------------------------------------------------------------------
Thanks for trying.. this kinda makes sense, but I can't make it work.
Getting "Parse error: syntax error, unexpected '<' "
Here is what I used:

error_reporting(0);
      $recipient = 'arlo@magma.ca';
      $subject = 'Catalogue request';
      $from = stripslashes($_POST['Name']);
      $msg = "$from\n\n $email\n\n".stripslashes($_POST['MsgBody']);<br>
  $header = "$from: ".stripslashes($_POST['Name']);
      if (mail($recipient, $email, $msg, $header))


#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 15 September 2006 - 01:08 PM

to answer your initial question, you're getting their email for the subject because that's the variable you have in the mail() function. if you look at the API of the mail() function, you'll notice that the variables it takes (in order) are:
1) to == the email that you want the message sent to
2) subject == subject line of the message
3) message == obvious
4) optional headers (from, reply-to, bcc, etc)

so, with that in mind, here is a simple example i'm sure you can use to modify yours:
<?php
$to = "me@mydomain.com";
$subject = "My form!!!";
$msg = "Whatever content you want here";
$headers = "From: $_POST[name] <$_POST[email]>\r\nReply-To: $_POST[email]";
mail($to, $subject, $msg, $headers);
?>

hope this helps
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#5 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 15 September 2006 - 01:11 PM

With Regards to the Unexpetec <
This is normally when you have used HTM code outside an echo
or forgot to close the php
?>

Tell me the problem, I will try tell you the solution

#6 CSpoon

CSpoon
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 15 September 2006 - 01:40 PM

to answer your initial question, you're getting their email for the subject because that's the variable you have in the mail() function. if you look at the API of the mail() function, you'll notice that the variables it takes (in order) are:
1) to == the email that you want the message sent to
2) subject == subject line of the message
3) message == obvious
4) optional headers (from, reply-to, bcc, etc)

so, with that in mind, here is a simple example i'm sure you can use to modify yours:

<?php
$to = "me@mydomain.com";
$subject = "My form!!!";
$msg = "Whatever content you want here";
$headers = "From: $_POST[name] <$_POST[email]>\r\nReply-To: $_POST[email]";
mail($to, $subject, $msg, $headers);
?>

hope this helps


Hi Obsidian..thanks! This solved the Subject problem.
I used your header line "From: $_POST[name] <$_POST[email]>\r\nReply-To: $_POST[email]"
to get the proper reply address (the form user's address), but instead get
"INVALID_ADDRESS@.SYNTAX-ERROR." in the From.
Am sure I am missing something obvious here?

    $subject = 'Catalogue request';
      $from = stripslashes($_POST['Name']);
      $msg = "You have received a catalogue request from: $from\n\n $email\n\n".stripslashes($_POST['MsgBody']);
  $header = "From: $_POST[name] <$_POST[email]>\r\nReply-To: $_POST[email]";
      if (mail($recipient, $subject, $msg, $header))


#7 CSpoon

CSpoon
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 15 September 2006 - 01:44 PM

With Regards to the Unexpetec <
This is normally when you have used HTM code outside an echo
or forgot to close the php
?>


Thanks onlyican... I did find some HTML code outside... ;-)

#8 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 15 September 2006 - 01:50 PM

well, in my example code, i'm not verifying the email address. you'll need to run a check to make sure the entered email address is an appropriate entry before you assign it to your headers:
<?php
$email = trim($_POST['email']);
if (!preg_match('|^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$|i', $email)) {
  // invalid email address entered. handle some sort of error message here
} else {
  // valid, let's send it:
  $to = 'me@mydomain.com';
  $subject = "My form!!!";
  $msg = "Whatever content you want here";
  $headers = "From: $_POST[name] <$email>\r\nReply-To: $email";
  mail($to, $subject, $msg, $headers);
}
?>

make sense?
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#9 CSpoon

CSpoon
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 15 September 2006 - 02:17 PM

well, in my example code, i'm not verifying the email address. you'll need to run a check to make sure the entered email address is an appropriate entry before you assign it to your headers:

<?php
$email = trim($_POST['email']);
if (!preg_match('|^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$|i', $email)) {
  // invalid email address entered. handle some sort of error message here
} else {
  // valid, let's send it:
  $to = 'me@mydomain.com';
  $subject = "My form!!!";
  $msg = "Whatever content you want here";
  $headers = "From: $_POST[name] <$email>\r\nReply-To: $email";
  mail($to, $subject, $msg, $headers);
}
?>

make sense?


Yes. Though beyond me.. wow. I incorporated it.. and received this error:
Parse error: syntax error, unexpected $end on on line 111
..which is the end of the webpage!



#10 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 15 September 2006 - 02:26 PM

that usually means you're missing a closing bracket '}' somewhere. check all your loops and conditionals and see if you can figure out where something may be amiss.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#11 CSpoon

CSpoon
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 15 September 2006 - 03:14 PM

that usually means you're missing a closing bracket '}' somewhere. check all your loops and conditionals and see if you can figure out where something may be amiss.


Hi again,
thanks for persisting with me...
I've tried several things with no luck... can you see it?

<?php
  } else {
      error_reporting(0);
  $email = trim($_POST['email']);
if (!preg_match('|^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$|i', $email)) {
  // invalid email address entered. Please enter a valid email address.
} else {
  // valid, let's send it:
      $recipient = 'me@email.com';
      $subject = 'Catalogue request';
      $from = stripslashes($_POST['Name']);
      $msg = "You have received a catalogue request from: $from\n\n $email\n\n".stripslashes($_POST['MsgBody']);
  $header = "From: $_POST[name] <$_POST[email]>\r\nReply-To: $_POST[email]";
      if (mail($recipient, $subject, $msg, $header))
        echo nl2br("<font color=#339900 size= 2 face='Verdana, Arial, Helvetica, sans-serif'><strong><br>
Thanks for your request!</strong></font><font color=#339900 size= 2 face='Verdana, Arial, Helvetica, sans-serif'>
A catalogue will be sent to: <br>
        $msg
        ");
      else
        echo "Message failed to send, please try again later or contact our office.";
}
?>


#12 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 15 September 2006 - 03:17 PM

yes
Top of the script u have else
but u dont close the else
Tell me the problem, I will try tell you the solution

#13 CSpoon

CSpoon
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 15 September 2006 - 03:25 PM

yes
Top of the script u have else
but u dont close the else


Yes! That was it...thank-you.
Now, the From email address has ".whsecure.net"  tagged onto it, as in "user@home.com.whsecure.net."




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users