Forum Moderators: coopster

Message Too Old, No Replies

php form help

         

Selander

5:31 pm on May 27, 2003 (gmt 0)

10+ Year Member



I'm a beginner with PHP forms. I'm currently using a HTML form that uses a PHP file to send the form content to a specified email address. What should I use in the php file to show in the email address of the sender in the "from" field. Everything I've tried generates an anonymous@domain.com.

In the form I simply have "message" box and an "email" box.

dmorison

5:44 pm on May 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

I assume you're using the mail() function, in which case you can specify the from: header in the 4th parameter...

mail("bill@foo.com","Some Subject","Some Message","From: ben@bar.com");

Selander

6:13 pm on May 27, 2003 (gmt 0)

10+ Year Member



Sorry, no I'm not. I decided to use <form method="POST" action="/contactme.php"> in the form. This refers to a "contactme" file that has the php instructions on were to send the email. This way there are no email address listed on my site. The following is the php code in my contactme.php file.

<?php
// ************Begin Configure***************
//Put where you want the email to go
$mailto = "name@mydomain.com";
//Put your subject in here
$subject = "Question from website";
//Put where to redirect to after sending the email
$redirect = "thankyou.htm";

// ************End Configure****************

foreach($HTTP_POST_VARS as $key => $value) {

$message .= $key . '<BR> ' . $value;
$msg .= "Message: $message\n<BR>";

}
if (@mail($mailto, $subject, $message)) {

header("Location: $redirect");
} else {
// This echo's the error message if the email did not send.
// You could change the text in between the <p> tags.
echo('<p>Mail could not be sent. Please use your back button to try again.</p>');
}
?>

[edited by: Selander at 6:18 pm (utc) on May 27, 2003]

jatar_k

6:17 pm on May 27, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld Selander

dmorison was referring to this

if (@mail($mailto, $subject, $message)) {

take a look at the php mail() function [php.net] on php.net. It gives you a good breakdown of how to setup the proper headers for the email.

wruk999

6:35 pm on May 27, 2003 (gmt 0)

10+ Year Member



Selander,

Try this out:


<?php
// ************Begin Configure***************
//Put where you want the email to go
$mailto = "name@mydomain.com";
//Put your subject in here
$subject = "Question from website";
//Put where to redirect to after sending the email
$redirect = "thankyou.htm";
//Put the address you want it sending FROM
$from = "From: you@yourdomain.com";

// ************End Configure****************

foreach($HTTP_POST_VARS as $key => $value) {

$message .= $key . '<BR> ' . $value;
$msg .= "Message: $message\n<BR>";

}
if (@mail($mailto, $subject, $message, $from)) {

header("Location: $redirect");
} else {
// This echo's the error message if the email did not send.
// You could change the text in between the <p> tags.
echo('<p>Mail could not be sent. Please use your back button to try again.</p>');
}
?>

Let us know.
wruk999

Selander

6:50 pm on May 27, 2003 (gmt 0)

10+ Year Member



It now responds with "you@yourdomain.com". Is there anyway to reference the "email" text box from the form, so that the email address that is entered will be listed in the "from" line of the response?

jatar_k

6:53 pm on May 27, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



assuming it is a

$from = "From: $HTTP_POST_VARS['email']";

substitute the varname email for the name of your textbox where it is entered.

<edited my own foolishness>

[edited by: jatar_k at 7:08 pm (utc) on May 27, 2003]

Selander

7:06 pm on May 27, 2003 (gmt 0)

10+ Year Member



Just by chance I added..
$from = "From: <$email>";

This seems to be working.

wruk999

7:43 pm on May 27, 2003 (gmt 0)

10+ Year Member



>> It now responds with "you@yourdomain.com"

This was because of the variable set in the config section of your script I posted:

//Put the address you want it sending FROM
$from = "From: you@yourdomain.com";

>> Just by chance I added.. $from = "From: <$email>";

This is because you have this line in:
foreach($HTTP_POST_VARS as $key => $value) {

so when Jatar said put this in: $from = "From: $HTTP_POST_VARS['email']";
it already had set $email equal to $HTTP_POST_VARS['email']

Hope this clears it up ;)

wruk999

Selander

8:08 pm on May 27, 2003 (gmt 0)

10+ Year Member



Thanks, I understand now. You guys are great. If you don't mind I have one more question that is simalar. It's a problem with the same form.

The email response from the form is pretty basic, which is fine but is there a way to include a carrage return after each text box response in the email? For examlpe this is was I receive currently....

CompanyName<BR> businessAddress<BR> 2434Attn<BR> janCity<BR> morrisonPhone<BR> 555-1234State<BR> ILSubmit

Is there anyway for the email response to look like this....

CompanyName: business
Address: 2434
Attn: jan
City: morrison
Phone: 555-1234
State: IL

jatar_k

8:14 pm on May 27, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



check the link for phpmail above for all of the headers you can send but ...

The from var that we were talking about before is a bit of a misnomer. It should really be called $headers because it is the var in the function where you pass extra headers.

this is the function

bool mail ( string to, string subject, string message [, string additional_headers [, string additional_parameters]])

this would allow you to put html in the message.

$headers = "From: $HTTP_POST_VARS['email']\r\nContent-type: text/html\r\n";
mail($mailto, $subject, $message, $headers)

or you could use \r\n after every line instead of <br> for text only messages.

Selander

8:21 pm on May 27, 2003 (gmt 0)

10+ Year Member



Nope,

replacing the <br> with \r\n just replaces the same in the email. I understand the \r is return. I did try this at one time.

jatar_k

8:25 pm on May 27, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



\r carriage return
\n newline character

in windows if you send both you get a hard return.

Did you try setting the additional headers as shown above?

Selander

8:59 pm on May 27, 2003 (gmt 0)

10+ Year Member



no luck.

dmorison

9:46 pm on May 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try replacing:


foreach($HTTP_POST_VARS as $key => $value) {

$message .= $key . '<BR> ' . $value;
$msg .= "Message: $message\n<BR>";

}

with


$message = "";

foreach($HTTP_POST_VARS as $key => $value)
{
$message .= $key . ":" . $value . "\n";
}

This should certainly create a plain text email with the content you want...

Selander

10:07 pm on May 27, 2003 (gmt 0)

10+ Year Member



PERFECT! Thank you very much. That did the trick.