Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Send Email

Status
Not open for further replies.

rhnewfie

Programmer
Jun 14, 2001
267
CA
Hi All

I am a newbie at PHP and am trying to put together a script that will email some form data.

Below is what I have but I am getting a compiler error on line 2, unexpected T_STRING

if someone could help out it would be great!!!

Oh, is there also a way that I could redirect the user to another page once the email is sent?

Thanks!!

Code:
<?php

$todayis = date("l, F j, Y, g:i a");

$notes = stripcslashes($notes);

$urlis = $urlis;

$visitor = $visitor;

$visitormail = $visitormail;

$friend = $friend;

$friendmail = $friendmail;

$redirectlink = $redirectlink;

$subject = "Link From A Friend";

$message = " $todayis [EST] \n
From: $visitor ($visitormail)\n
To: $friendmail \n
Link: $redirectlink \n
Comments: $notes \n ";

$from = "From: $visitormail\r\n";


mail($friendmail, $subject, $message, $from);

?>
 
Well other than the pointless assignment of variables to themselves, I see nothing wrong with your code, is there by any chance anything else before this piece of code that might be causing the problem? Since it specifies a line can you point us to that exact line in your code, including other html, and whatever else there might along with this code.

As for the redirect, provided that you haven't output anything to the browser prior to this, you can use

header("Location: someotherpage.php"); To send the browser there after the email has been sent.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Ok, I took out those assignments.

This script is called in a form action if that helps but there is no other php in the html page.

The exact error is:

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /hsphere/local/home/SendMail/SendEmail.php on line 2 Parse error: syntax error, unexpected T_STRING in /hsphere/local/home/SendMail/SendEmail.php on line 2
 
Warning: Unexpected character in input: '\' ... nn line 2

I'm guessing there is something in your $notes variable that is causing the error with [blue]stripcslashes() (I'm assuming that is line 2)[/blue]. Its choking at a slash but I don't know why, and i haven't been able to recreate it.

Can you show us the contents of $notes?



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
do you not need to use POST for getting the data from the form?

eg. $urlis = '$_POST[urlis]'?

I'm certainly no expert on PHP but i've done a few mail scripts and that's how I did it.

You could also try:

if (get_magic_quotes_gpc()) {
$notes = stripslashes( $notes );
}

instead of just $notes = stripcslashes($notes);

Finally, try structuring your message like this:

$message =

"Details are as Follows:\n" .
"---------------------------\n\n" .

"$todayis [EST]\n" .
"-----------------\n\n" .
"From: $visitor ($visitormail)\n" .
"To: $friendmail \n" .
"Link: $redirectlink \n" .
"Comments: $notes \n" .;

mail($friendmail, $subject, $message, $visitormail);

header("Location: yourpage.php");

Some of this stuff may just be another way to do it but thats how I do it and it always works.

Hope this is useful.


'When all else fails.......read the manual'
 
I have changed the scrip to the following:

<?php
$todayis = date("l, F j, Y, g:i a")

if (get_magic_quotes_gpc())
{
$notes = stripslashes( $notes );
}


$urlis = '$_POST[urlis]';
$visitormail = '$_POST[visitormail]';
$friendmail = '$_POST[friendmail]';
$from = '$_POST[friend]';
$redirectlink = '$_POST[redirectlink]';
$visitor = '$_POST[visitor]';
$subject = "Fernlea Flowers Link From A Friend";
$message = " $todayis [EST] \n
From: $visitor ($visitormail)\n
To: $friendmail \n
Link: $redirectlink \n
Comments: $notes \n ";
$from = "From: $visitormail\r\n";
mail($friendmail, $subject, $message, $from);
header("Location: thanks.html");

?>
Now I get

Parse error: syntax error, unexpected T_IF in /hsphere/local/home/SendMail/SendEmail.php on line 4

Thanks for all the help!!!
 
put a semicolon after the line starting $todayis. make sure that there is nothing above the opening <? otherwise the header redirect will not work.
 
I made the changes above and now everything appears to work perfectly but the email that gets sent never seems to arrive. Am I missing something??
 
none of these variables will work.
Code:
$urlis = '$_POST[urlis]';
$visitormail = '$_POST[visitormail]';
$friendmail = '$_POST[friendmail]';
$from = '$_POST[friend]';
$redirectlink = '$_POST[redirectlink]';
$visitor = '$_POST[visitor]';

you have got your enquoting incorrect.

they should look like
Code:
$urlis = $_POST['urlis'];
 
jpadie - my scripts work with both $var = '$_POST[var]' and $var = $_POST['var'] so i'm not sure if that would be the problem? As it happens however I do use $var = $_POST['var'] and this is what i intended to say.

As for not recieving them, this doesn't necessarily mean your scripts are not working. They could well be being blocked by a spam filter. Try sending it to a hotmail address and be sure to check your junk mail.

'When all else fails.......read the manual'
 
When jpadie posted, "none of these variables will work", he misspoke. If he had taken the time to be more exact in his wording, he would have written, "These variable references may not work in the way you expect."

Both

[tt]$var = $_POST['var'];[/tt]

and

[tt]$var = '$_POST[var]';[/tt]

are valid PHP statements. However, the behaviors of the two statements are very different. The first statement will copy the value in $_POST['var'] to the variable $var. The second statement will place the string literal '$_POST[var]' into $var.



Want the best answers? Ask the best questions! TANSTAAFL!
 
I didn't know that! what's the difference? In practice (from my limited use) the end result has been the same!

'When all else fails.......read the manual'
 
No, you would get anything near the same value in $var. The placement of those singlequotes makes for a drastic difference in the behaviors of the two statements.

Suppose $_POST['var'] contains 'abc'. Then after the statement

[tt]$var = $_POST['var'];[/tt]

$var will contain 'abc'. However, after the statement

[tt]$var = '$_POST[var]';[/tt]

then $var will contain '$_POST[var]'. On my system, the script:

Code:
<?php
$_POST['var'] = 'abc';

$var = $_POST['var'];

print $var . "\n";

$var = '$_POST[var]';

print $var . "\n";
?>

outputs:

[tt]abc
$_POST[var][/tt]


Keep in mind that PHP treats singlequotes (') and doublequotes (") very differently. With doublequotes, PHP interpolates variable references inside the quotes. With singlequotes, it does not. (See
So this script:

Code:
<?php
$_POST['var'] = 'abc';

$var = $_POST['var'];

print $var . "\n";

$var = '$_POST[var]';

print $var . "\n";

$var = "$_POST[var]";

print $var . "\n";
?>

outputs:

[tt]abc
$_POST[var]
abc[/tt]

This is because in that last assignment statement, I have, through my use of doublequotes, told PHP to interpolate variable references inside the string.




Want the best answers? Ask the best questions! TANSTAAFL!
 
Yeah, I changed all the quoting around and now all is working great!!
 
I don't know what you mean by that.

There are canonical forms for array references. Although [tt]"$AnArray[foo]"[/tt] will work, either [tt]$AnArray['foo'][/tt] or [tt]$AnArray["foo"][/tt] is preferred. See the examples in the PHP online manual page that describes the Array type



Want the best answers? Ask the best questions! TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top