|
|
 |
June 1st, 2006, 05:38 PM
|
#1
|
|
New Member
Join Date: May 2006
Location: Barcelona
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
When a form is sent as email, how do I get an address in the from heading?
Hi I've successfully put a form into my site but when I receive the emails, it has a blank in the "from:" section. Check my code below that the php file sends to my email. I put in that "headers" and "from" title in, not knowing much about php yet, but it doesnt seem to do anything. How can I get the email I receive to have a "from" (even if its from the same address as the one receiving it?
Also, my customers can send a message concerning either "information" or "website problems" I have a little options bar for this, which you can see at:
http://www.bernisnudelbrett.de/en/contact.html
How can I get the php code to send to different emails, depending on the topic chosen in "concerning"?
Would I need to add to the form code in the contact. html page or to the code in the contact. php page (where I understand, the information is processed and sent on.
Anyway look at the code below and let me know if you can answer either problem! Thanks
This is the code on "contact. php"
Code:
<script language="php"> $email = $HTTP_POST_VARS[email]; $mailto = "info@bernisnudelbrett.de"; $mailsubj = "A message from a website visitor"; $mailhead = "From: $email\n"; reset ($HTTP_POST_VARS); $mailbody = "A message from a website visitor:\n"; $headers .= 'From: <info@bernisnudelbrett.de>'; while (list ($key, $val) = each ($HTTP_POST_VARS)) { $mailbody .= "$key : $val\n"; } if (!eregi("\n",$HTTP_POST_VARS[email])) { mail($mailto, $mailsubj, $mailbody, $mailhead, $headers); } </script>
PS, Its my first website, any comments on layout, improvements?
|
|
|
June 2nd, 2006, 02:19 PM
|
#2
|
|
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,307
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
Re: When a form is sent as email, how do I get an address in the from heading?
With regard to sending to a different email address, you can tackle this in your contact. php file.
Code:
$concern = $_POST['concerning'];
if ($concern == 'information'){
$mailto = '...'
}
else{
$mailto = '...'
}
With a simple form like yours, you only need the following:
mail($mailto, $mailsubj, $mailbody, 'From: ...');
NOTE: You only need the four variable, not the five that you have.
If you need to have a complex mail header, then build the header seperately and insert it in place of the 'From: ...'.
|
|
|
June 3rd, 2006, 01:04 PM
|
#3
|
|
New Member
Join Date: May 2006
Location: Barcelona
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
Re: When a form is sent as email, how do I get an address in the from heading?
Hi thanks for the code
I tried putting it in but it wouldnt work. I deleted the original 'mail to' and changed as suggested
On net it says there ¡s a parsing error.
Code:
<script language="php"> $email = $HTTP_POST_VARS[email]; $concern = $_POST['concerning']; if ($concern= 'information'){$mailto = 'info@bernisnudelbrett.de'} else{$mailto = 'web@bernisnudelbrett.de'} $mailsubj = "A message from a website visitor"; $mailhead = "From: $emailfield\n"; reset ($HTTP_POST_VARS); $mailbody = "A message from a website visitor:\n"; while (list ($key, $val) = each ($HTTP_POST_VARS)) { $mailbody .= "$key : $val\n"; } if (!eregi("\n",$HTTP_POST_VARS[email])) { mail($mailto, $mailsubj, $mailbody, $mailhead); } </script>
have I put something in mistakenly?
|
|
|
June 3rd, 2006, 07:12 PM
|
#4
|
|
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,307
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
Re: When a form is sent as email, how do I get an address in the from heading?
Yes.
1st. You are collecting the value of $email when you should be collecting the value of $emailfield.
2nd. Dont use $HTTP_POST_VARS anymore. Use $_POST
3rd. The field name inside the [] needs to be in quotes.
4th. Where you set the value of $mailhead, you have included the value of the collected variable as part of the string instead of appending its content.
Assuming you correct the first three what you want is:
$mailhead = "From: ".$emailfield;
Note also that the new line, \n is not required for a simple header like this.
|
|
|
June 4th, 2006, 04:22 AM
|
#5
|
|
New Member
Join Date: May 2006
Location: Barcelona
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
Re: When a form is sent as email, how do I get an address in the from heading?
Hi Geoff
Thanks for your assistance, I'm a PHP for Dummies level user at the mo, so bear with me!
I've changed the code as you suggested but am getting the following error in my browser:
Quote:
|
Parse error: syntax error, unexpected '}' in D:\Inetpub\webs\bernisnudelbrettde\en\contact.php on line 52
|
OK, so after your latest suggestions, my code looks like this:
Code:
<script language="php"> $emailfield = $_POST['email']; $concern = $HTTP_POST['concerning']; if ($concern = 'information'){$mailto = 'info@bernisnudelbrett.de'} else{$mailto = 'web@bernisnudelbrett.de'} $mailsubj = "A message from a website visitor"; $mailhead = "From: ".$emailfield; reset ($HTTP_POST_VARS); $mailbody = "A message from a website visitor:\n"; while (list ($key, $val) = each ($HTTP_POST_VARS)) { $mailbody .= "$key : $val\n"; } if (!eregi("\n",$HTTP_POST_VARS[email])) { mail($mailto, $mailsubj, $mailbody, $mailhead); } </script>
Line 52 is the 'if' line, I still find a lot of this is going over my head but hopefully the coding will finally make sense! If there is a problem with the '}', can you suggest the appropriate change?
By the way, when you say
Quote:
|
2nd. Dont use $HTTP_POST_VARS anymore. Use $_POST
|
do you mean in all parts of the script that use it?
|
|
|
June 4th, 2006, 02:04 PM
|
#6
|
|
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,307
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
Re: When a form is sent as email, how do I get an address in the from heading?
Code:
<script language="php">
$emailfield = $_POST['email'];
$concern = $HTTP_POST['concerning'];
There are three errors here right at the start.
You don't surround php code with the <script>...</script> tags, php is surrounded by;
<? php...?>
You are trying to pick up a value from a field named 'email'. In you form code, the email field is called 'emailfield'. I've pointed this out to you previously.
The third line above should be;
$concern = $_POST['concerning']
Quote:
By the way, when you say
Quote:
2nd. Dont use $HTTP_POST_VARS anymore. Use $_POST
do you mean in all parts of the script that use it?
|
Yes. This is considered old style and less secure.
Get rid of these bits of code:
Code:
reset ($HTTP_POST_VARS);
while (list ($key, $val) = each ($HTTP_POST_VARS)) { $mailbody .= "$key : $val\n"; }
if (!eregi("\n",$HTTP_POST_VARS[email])) { )
Although I've told you to get rid of the above, note in your code that on two of the above lines you have ; followed by }.
; terminates a line of code in php and accounts for your error message.
Do you have any php reference to hand at all?
Last edited by ukgeoff; June 4th, 2006 at 02:07 PM..
|
|
|
June 5th, 2006, 10:33 AM
|
#7
|
|
New Member
Join Date: May 2006
Location: Barcelona
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
Re: When a form is sent as email, how do I get an address in the from heading?
Hey, just been out of town for a day. Yes, I have PHP for Dummies and Ive just started reading it, so hopefully this lack of knowledge will dissapear over the coming weeks as I play around. For this reason, it's difficult at the moment to understand the basic PHP concepts as I'm not used to the vocabulary used yet. I'll correct the coding as you suggested. Thanks
|
|
|
June 5th, 2006, 04:18 PM
|
#8
|
|
New Member
Join Date: May 2006
Location: Barcelona
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
Re: When a form is sent as email, how do I get an address in the from heading?
HI Geoff
Its quite poissible you're banging your head against a table in despair at my idiocy, but I've tried to make your amendments as suggested and its not accepting that '}' symbol as shown when the message is sent from contact. html to contact. php... It relates to the one on the 'if' line that you gave me.
Quote:
|
Parse error: syntax error, unexpected '}'
|
I'm looking at my Dummies cheat sheet, and as you rightly show, the code should be:
Quote:
if (condition) {block}
else (condition) {block}
|
so why won't it take? I've reverted baclk to the old code on the website while I work this out, and again, much appreciated, your thoughts!
|
|
|
June 5th, 2006, 11:54 PM
|
#9
|
|
Reputable Member
Join Date: Jul 2005
Location: Indiana, USA
Age: 31
Posts: 150
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
Re: When a form is sent as email, how do I get an address in the from heading?
With the exception of the drop down menu this thread concerns forms too: http://www.webforumz.com/html-forum/...m-problems.htm
Although keep in mind that making one will help you learn (as you are) but will be more frustrating (as you can see).
|
|
|
June 6th, 2006, 06:36 AM
|
#10
|
|
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,307
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
Re: When a form is sent as email, how do I get an address in the from heading?
The code below is at least syntactically correct. Try it and see what you get.
Code:
<?php
$emailfield = $_POST['emailfield'];
$concern = $_POST['concerning'];
if ($concern == 'information') {
$mailto = 'EMAIL REMOVED - Send PM to This User Instead';
}
else {
$mailto = 'EMAIL REMOVED - Send PM to This User Instead';
}
$mailsubj = "A message from a website visitor";
$mailhead = "From: ".$emailfield;
$mailbody = "A message from a website visitor:\n";
mail($mailto, $mailsubj, $mailbody, $mailhead);
?>
Also notice the change I have made in the if statement from '=' to '=='.
The single '=' sets left equal to right. The double '==' is a comparison. Does left equal right.
It often catches people in the early days.
|
|
|
June 6th, 2006, 08:14 AM
|
#11
|
|
Reputable Member
Join Date: Jul 2005
Location: Indiana, USA
Age: 31
Posts: 150
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
Re: When a form is sent as email, how do I get an address in the from heading?
<off topic>
@ukgeoff - as a designer somethings in development can be confusing so I was extactic when I could easily understand the difference between = and ==. Everything I have seen about it now makes sense. Thank You.
</off topic>
|
|
|
June 7th, 2006, 05:08 PM
|
#12
|
|
New Member
Join Date: May 2006
Location: Barcelona
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
Re: When a form is sent as email, how do I get an address in the from heading?
Nice one guys
I shall try the new code, after a week of reading php and mysql for dummies, I'm wondering if there's a grade lower than dummy!
Will let you know how it works out.
|
|
|
June 9th, 2006, 01:35 PM
|
#13
|
|
New Member
Join Date: May 2006
Location: Barcelona
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
Re: When a form is sent as email, how do I get an address in the from heading?
Resolution (kind of)
OK, put the code in today and tried it. All works fine and starting to understand it, except, I was only getting "message from a website visitor" in the message body of the email, so I copied that bit of code that I'd found on the net originally and used that instead, now I get all the info from the original form.
so instead of your line
Quote:
|
mail($mailto, $mailsubj, $mailbody, $mailhead);
|
I used
while (list ($key, $val) = each ($HTTP_POST_VARS)) { $mailbody .= "$key : $val\n"; }
if (!eregi("\n",$HTTP_POST_VARS[email])) { mail($mailto, $mailsubj, $mailbody, $mailhead); }
?>
Even thought you said I didnt need that line, it seems to give me the results I want.
The messages will now go to either the info email or web email and all the comments are included so I'm pleased with the result, but one thing I still don't get. When I test run a few emails to each address, not all of them come through. If I send 4 or 5 in succession, they should all be processed, right? I don't think the problem is that code I changed from your suggested line, because the same thing happens whichever code I use. Any suggestions?
|
|
|
June 11th, 2006, 01:12 PM
|
#14
|
|
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,307
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
Re: When a form is sent as email, how do I get an address in the from heading?
The reason for the mesage you were seeing in the body is because if you look at the code i gave, you which is your code with my corrections, that message is hard coded into the code, i presume for test purposes.
You need to pick up the actual message using:
Code:
$mailbody = $_POST['comments'];
which is the name of the textarea tag in your form.
Are you still having problems sending multiple emails?
|
|
|
June 11th, 2006, 02:56 PM
|
#15
|
|
New Member
Join Date: May 2006
Location: Barcelona
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
Re: When a form is sent as email, how do I get an address in the from heading?
Multiple emails no prob now and I have just got an addition off someone else to what you have shown me below
$mailbody = $_POST['title'] . "\n";
$mailbody .= $_POST['namefield'] . "\n";
$mailbody .= $_POST['concerning'] . "\n";
$mailbody .= $_POST['comments'];
this way I have everything I need in the message. What exactly does \n mean?
Only nagging problem now is that not all of the emails are coming through and I wonder if it is a problem with the spam filter of the hosting company.
Thanks geoff
|
|
|
June 11th, 2006, 06:00 PM
|
#16
|
|
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,307
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
Re: When a form is sent as email, how do I get an address in the from heading?
It adds a new line character so the each fields content when printed start on a new line.
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|