Forum Moderators: coopster

Message Too Old, No Replies

mail script errors

Warning: Unexpected/ Parse error: parse error,

         

firstodd

7:02 pm on Jun 19, 2007 (gmt 0)

10+ Year Member



Hello,

I am trying to do a little php script to send a form from the .htm

On hitting the submit button, I'm getting this:

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/content/f/i/r/firstodd/html/contact.php on line 3

Parse error: parse error, unexpected T_VARIABLE in /home/content/f/i/r/firstodd/html/contact.php on line 4

--

I'm don't know anything about this stuff, I'm just modifying a guide showing how to do it. Here is the .php down to the 4th line.. modified to not reveal information:

<?php
$to = "email@example.net";
$subject = "Subject";
$applicant first name = $_REQUEST['applicant first name'] ;

and there is about 50-100 lines similar before it ends, one for each <input .

Any insight?

[edited by: eelixduppy at 7:02 pm (utc) on June 19, 2007]
[edit reason] example.net [/edit]

Sarah Atkinson

7:07 pm on Jun 19, 2007 (gmt 0)

10+ Year Member




$applicant first name

There's your problem

veriables have to be one word

such as
$applicant_fname =

I just uses $fname or $first_name

firstodd

7:34 pm on Jun 19, 2007 (gmt 0)

10+ Year Member



so it is appearing on the email as whatever you insert there, right? and the Request needs to be one word as well?

applicant_fname

mcibor

8:14 pm on Jun 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



how do they appear on the email as one name? is it possible to do spaces, %20 or something similiar?

You mixed two things here. One thing is variable name, and one thing is variable value:

here name and value are the same:
$a = "a";

And here aren't
$name = "John Smith";

So you cannot place a space (nor % sign) in the name of the variable:

incorrect:
$n ame = "John Smith";
$n'ame = "John Smith";
$n%ame = "John Smith";
$n!ame = "John Smith";
$n@ame = "John Smith";

only underline and maybe some other special character is correct:
$n_ame = "John Smith"; // this is correct

Hope this help you to understand error you made.

Regards
Michal

PS. And welcome to Webmasterworld!

PSS. Yes, request needs to be one word as well, as it is an array reference.
Moreover better not to use $_REQUEST.
Use $_POST, $_GET, $_COOKIE or $_SESSION instead. Why? Because while using request you don't have any control on where the data comes from.

firstodd

8:23 pm on Jun 19, 2007 (gmt 0)

10+ Year Member



I will test a bit with the underscores and see, I'm just confused as to what shows up in the email and getting it through will answer that. I'll repost if any questions,

thanks for the quick responses :)

firstodd

4:53 pm on Jun 22, 2007 (gmt 0)

10+ Year Member



Okay,

So I replaced both the <input> tags and the php spaces with underscores. The setup now looks like this:

<?php
$to = "email@email.net";
$subject = "Subject";
$applicant_first_name = $_REQUEST['applicant_first_name'] ;

coming off of

Applicant First Name: <input name="applicant_first_name" type="text" size="14">

in the html

I'm still getting :

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/content/f/i/r/firstodd/html/contact.php on line 3

Parse error: parse error, unexpected T_VARIABLE in /home/content/f/i/r/firstodd/html/contact.php on line 4

when I press submit.

More help?

eelixduppy

3:23 am on Jun 24, 2007 (gmt 0)



Are you sure these are the lines creating the errors? They seem to be written correctly...

And instead of $_REQUEST, you should be using either $_POST or $_GET unless you absolutely need both, which I do not think is the case here.

firstodd

5:29 am on Jun 24, 2007 (gmt 0)

10+ Year Member



Well it says found in line 3 and 4 of the .php file, so I figured what ever the problem is every line probably has.

I'm just following the guide here:
[php.about.com...]

So the Post and Gets also function for what I'm trying to do? No need to try to explain to me what they actually mean, like I said I'm just going off of that little guide.

That is what it seems like, that it's not knowing where to get the info or something. I'll replace the REQUESTS on monday and get back to you.

I also noticed mcibor's examples don't have a space between the end of the line and the semi colon, would that be a cause?

Thanks again

eelixduppy

2:02 pm on Jun 24, 2007 (gmt 0)



>> would that be a cause?

nope, should still work the same without error.

mattclayb

4:53 pm on Jun 24, 2007 (gmt 0)

10+ Year Member



use $_GET when you have a short string of variables that do not contain any sensitive information, as $_GET variables are passed through the URL. Use $_POST when you have a longer string of variables, or variables that need to be hidden from the user (as these aren't URL encoded).

A script like this should work - this would be using two files, a html for the form (form.html) and a php mail script (mail.php), you could have them all on the same page, but I prefer to separate them as I find it easier for securing th mail script from SPAM.

form.html -

<form id="form1" name="form1" method="post" action="mail.php">
<label>First Name :
<input type="text" name="fname" />
</label>
<input type="submit" />
</form>

mail.php -

<?PHP
$to = "email@example.net";
$subject = "Subject";
$fName = $_POST['fName'];

$headers = "From: yourfromemail@yourdomain.com";
$message = "your message here";

mail($to, $subject, $message, $headers);

header(location: form.html);
?>

You really should look at some basic tutorials into this, and to passing variables in php, there are various good ones around, W3Schools has good introductory tutorials. You should also look into methods of protecting your mail script from SPAM.

mattclayb

4:58 pm on Jun 24, 2007 (gmt 0)

10+ Year Member



sorry just spotted two typos -
- name attribute of input tag on form.html should be fName not fname.
- 'header' line in mail.php should be header("Location: form.html");

firstodd

8:05 pm on Jun 25, 2007 (gmt 0)

10+ Year Member



Well, I just changed the whole form to replica mattclayb's example, and it's still giving me the exact same error. So I don't know what the hell is wrong. Tried $_GET as well.

Going to to open up a new page and start over...

mattclayb

8:29 pm on Jun 25, 2007 (gmt 0)

10+ Year Member



are you trying to escape any characters with a / in your code? and are your errors still showing as appearing on the same lines of code?

you need to be looing for a stray on unproperly formated / and the unexpected T_variable parse error is normally when the line is not formatted properly.

You need to paste your ENTIRE code for us to see where the problem lies. i'ts no good showing part.

firstodd

9:12 pm on Jun 25, 2007 (gmt 0)

10+ Year Member



Something beyond this is wrong, here is what my pages look like after I started over just to see:

form.html:

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Title</title>
</head>

<body>

<form method="post" action="contact.php">
Email: <input name="email" type="text"><br>
Message:<br>
<textarea name="message" rows="15" cols="40"></textarea><br>
<input type="submit">
</form>

</body>

</html>

contact.php:

<?php
\$to = "email@email.net";
\$subject = "Contact Us";
\$email = $_REQUEST['email'] ;
\$message = $_REQUEST['message'] ;
\$headers = "From: $email";
\$sent = mail($to, $subject, $message, $headers) ;
if($sent)
{print "Your mail was sent successfully"; }
else
{print "We encountered an error sending your mail"; }
?>

I've done this before on the same host and it worked fine, I don't know what is up. In this try, the only thing I even changed was the email address (which I changed again to post).

mattclayb

1:36 pm on Jun 26, 2007 (gmt 0)

10+ Year Member



why are you postpending your variables with /?

This is what your code should be

<?php
$to = "email@email.net";
$subject = "Contact Us";
$email = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $email";
$sent = mail($to, $subject, $message, $headers);
if($sent){
print "Your mail was sent successfully";
} else {
print "We encountered an error sending your mail";
}
?>

Habtom

1:48 pm on Jun 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$to = "email@email.net";
$subject = "Contact Us";
$email = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $email";
$sent = mail($to, $subject, $message, $headers);

Shouldn't the full headers be written, return path and that sort of thing. Email without the complete headers never works on my server. Am I missing something?

Habtom

mattclayb

1:51 pm on Jun 26, 2007 (gmt 0)

10+ Year Member



you obviously didn't replicate my code! ;-)

mattclayb

1:53 pm on Jun 26, 2007 (gmt 0)

10+ Year Member



Habtom,

you're quite right, certain servers will reject email with incomplete or falsely injected headers.

On most servers, though containing "From: " should be adequate.

jatar_k

2:08 pm on Jun 26, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try adding a few more headers, plus some newlines and/or carriage returns

this one

$to = "email@email.net";
$subject = "Contact Us";
$email = $_POST['email'];
$message = $_POST['message'];

$headers .= "From: $email\n";
$headers .= "Return-Path: $email\n";
$headers .= "Reply-To: $email\n";

$sent = mail($to, $subject, $message, $headers);

and if not then try adding \r to those

$to = "email@email.net";
$subject = "Contact Us";
$email = $_POST['email'];
$message = $_POST['message'];

$headers .= "From: $email\r\n";
$headers .= "Return-Path: $email\r\n";
$headers .= "Reply-To: $email\r\n";

$sent = mail($to, $subject, $message, $headers);

see if those extras make any difference. You should have at least From, Reply-To and Return-Path when sending messages.

firstodd

4:04 pm on Jun 26, 2007 (gmt 0)

10+ Year Member



It's not your code because I said I started over and put in the basic stuff from the tutorial again to see if even that would work. I did try yours, same error.

There was backslashes in the front because searching around for the error I found a forum that mentioned doing that solved it.

----

<?php
$to = "email@email.net";
$subject = "Contact Us";
$email = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $email";
$sent = mail($to, $subject, $message, $headers);
if($sent){
print "Your mail was sent successfully";
} else {
print "We encountered an error sending your mail";
}
?>

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/content/f/i/r/firstodd/html/contact.php on line 2

Parse error: parse error, unexpected T_STRING in /home/content/f/i/r/firstodd/html/contact.php on line 2

$to = "email@email.net";
$subject = "Contact Us";
$email = $_POST['email'];
$message = $_POST['message'];

$headers .= "From: $email\n";
$headers .= "Return-Path: $email\n";
$headers .= "Reply-To: $email\n";

$sent = mail($to, $subject, $message, $headers);

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/content/f/i/r/firstodd/html/contact.php on line 2

Parse error: parse error, unexpected T_STRING in /home/content/f/i/r/firstodd/html/contact.php on line 2

$to = "email@email.net";
$subject = "Contact Us";
$email = $_POST['email'];
$message = $_POST['message'];

$headers .= "From: $email\r\n";
$headers .= "Return-Path: $email\r\n";
$headers .= "Reply-To: $email\r\n";

$sent = mail($to, $subject, $message, $headers);

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/content/f/i/r/firstodd/html/contact.php on line 2

Parse error: parse error, unexpected T_STRING in /home/content/f/i/r/firstodd/html/contact.php on line 2

-----

I think something larger here is wrong, maybe the server or host doesn't support it or something? None of these are returning any difference in error, except that it's on line 2 now, and I haven't changed line 1 or 2 at all from when it was returning in on line 3 and 4.

mattclayb

9:24 pm on Jun 26, 2007 (gmt 0)

10+ Year Member



Are you sure you've been running this code? and the file has been overwriting when you have uploaded it?

Delete your original script from the server, rename this one, upload it, and try the new script.

firstodd

9:31 pm on Jun 26, 2007 (gmt 0)

10+ Year Member



That's what I thought, I have deleted both the files, tried diff names, and even made a separate directory for them. Also, included the full http: address . No differences

=)

I'll try to to figure out another way to do this, I think it's just not supported or something. Thanks for all the help!

Tyler

jatar_k

2:33 pm on Jun 27, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



have you made a completely new file? or are you just pasting the new code into the same file?

I have seen things similar to this and fixed it by opening the old file in a pure text editor, copying all content, pasting into a new file, saving with a new name, deleting old files from the server and uploading the new file.

firstodd

4:00 pm on Jun 27, 2007 (gmt 0)

10+ Year Member



ha ha ha

That worked. Not sure if it was making the new file or switching from Wordpad to Notepad.

Now, script above works great w/ email & message, my only other question is how to add more inputs.

I tried adding more $x = $_POST['x']; lines, and then adding them to the mail( , but it's returning at error on that specific line, so I assume this isn't how you do it.

Sorry to take up so much of your times, I really appreciate it.

jatar_k

4:10 pm on Jun 27, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what specific error is it returning?

you can loop through the POST array and append it to your message var if you like, simple, and not overly secure but it will work

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

that would put all your $_POST values into the message

firstodd

4:33 pm on Jun 27, 2007 (gmt 0)

10+ Year Member



Parse error: parse error, unexpected T_VARIABLE in /home/content/f/i/r/firstodd/html/forms/query.php on line 13

but I understand that, the mail( function is just setup wrong

---

On the POST loop, how will the formatting by when the email is sent? I'm looking at around 50 input fields, sent to not me but the company I'm doing the site for, so I don't want them to come clumped together.

jatar_k

5:07 pm on Jun 27, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you can add whatever fomatting you like, that is just every field on a newline, nothing exciting

send it to yourself and then you can look at formatting

I seldom do the loop way any more as I need to validate fields and usually styling the message needs more individual treatment. I build it field by field to lay the whole thing out. If you are building it for a company then you should go the same route.

firstodd

8:38 pm on Jun 27, 2007 (gmt 0)

10+ Year Member



The loop is working well

I'm not sure what you mean by field by field. It should work fine as it is, it comes out like this:

email: email@email.net
fname: firstodd
lname: ladast
otherinfo: amsfmksafmlk
asdfkasf

asfa
sf
asdf

saf

how would I go about making the email fname lname entrys bold? Or if thats not possible, the other text smaller?
Sorry, as I said, I don't know the code that well.

Thanks!

jatar_k

3:16 am on Jun 28, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



the line inside the if statement is what you need to adjust

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

if you wanted to bold it then it would look like this

$message = '';
foreach ($_POST as $key => $value) {
$message .= '<b>' . $key . '</b>: ' . $value . "\n";
}

though this is adding html so it would have to be html which means you may need to adjust headers and also add an html line break

$message = '';
foreach ($_POST as $key => $value) {
$message .= '<b>' . $key . '</b>: ' . $value . "<br>\n";
}

if you're sending plain text then it's just, well, plain

mattclayb

7:40 pm on Jun 28, 2007 (gmt 0)

10+ Year Member



also your $message does not need to be a loop collecting the POST vars.

You can manually format the $message like this, as jatar_k says you will need to set html headers for the email client to recognise the html used to format the message. But if you include html, it is wise to have a plain text alternative for users without html email. The example below would send a html message and a plain text alternative, the email client will choose which one to show.

<?PHP

//Capture POST data from form

$fname = $_POST['fname'];
$lname = $_POST['lame'];
$email = $_POST['email'];
$enquiry = $_POST['enquiry'];

//Create unique boundary id

$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

//Set Headers

$mailto = "youremail@yourdomain.com";
$subject = "Enquiry from site";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/alternative;\n" .
" boundary=\"{$mime_boundary}\"";

//Set formatted message with plain text and html alternatives

$message = "
$mime_boundary
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit

First Name: $fname
Last Name: $lname
Email: $email
Enquiry: $enquiry

$mime_boundary
Content-Type: text/html; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit

<html>
<body>
<strong>First Name:</strong> $fname
<strong>Last Name:</strong> $lname
<strong>Email:</strong> $email
<strong>Message:</strong> $enquiry
</body>
</html>

$mime_boundary

";

mail($mailto, $subject, $message, $headers);

?>

This 33 message thread spans 2 pages: 33