Forum Moderators: coopster

Message Too Old, No Replies

Using requested variables directly in mail command

Converting a 2-step process into a 1-step process.

         

Jeremy_H

2:43 am on Mar 18, 2006 (gmt 0)

10+ Year Member



I'm requesting a value in my PHP code, and then using it once in a mail command.

Instead of doing a two step process, where I set the value to a variable, and using the variable in the mail command, I'm trying to use the value straight in the mail command.

From this:

$bodyvariable=$_REQUEST["bodyvariable"];
mail("to","sub","$bodyvariable","from");

To something like this:

mail("to","sub","$_REQUEST[\"bodyvariable\"]","from");

The problem is, I'm getting a parse error. Is a one-step process not allowable?

Thanks

Tastatura

5:23 am on Mar 18, 2006 (gmt 0)

10+ Year Member



I am not PHP expert but why not use ' instead of " around bodyvariable

Jeremy_H

7:02 am on Mar 18, 2006 (gmt 0)

10+ Year Member



Hi Tastatura,

Thanks for the suggestion. My first try was with using the single apostrophe, but it didn't work either. So then I tried escaping the double apostrophe (which seemed more proper), but that didn't work either.

:(

Tastatura

7:50 am on Mar 18, 2006 (gmt 0)

10+ Year Member



I got curious enough about this so I looked up references at php.net

It might be that you don't need quotes around $_REQUEST['bodyvariable'] (since it’s a variable).

These are pages that I briefly looked up:
[php.net ]
[php.net ]

HTH

mikesmith76

9:06 am on Mar 18, 2006 (gmt 0)

10+ Year Member



have you tried

mail("to","sub",$_REQUEST["bodyvariable"],"from");

that should work, however it's usually considered a good idea to do some kind of input validation before using the data. and if you wanted user input for the from field, for example, you would definately need to validate otherwise you'd be vunerable to email injection.

Mr_Fern

9:30 am on Mar 18, 2006 (gmt 0)

10+ Year Member



Mike offers good advice about input validation.

Not sure if it's too important in this case since the variable is the mail body.

Anyhow, when working with array values, here's what to consider:

$array = array('key' => 'value');

$array[key] // bad, key assumed to be a constant named key
$array['key'] // good
$array["key"] // good

when used within double-quotes
"$array[key]" // good
"{$array['key']}" // good
"{$array[key]}" // bad, key assumed to be a constant named key

Like others have said, you don't need the quotes around the $_REQUEST variable, but if you do want to use them, either use "$_REQUEST[bodyvariable]" or "{$_REQUEST['bodyvariable']}"

Jeremy_H

5:23 pm on Mar 20, 2006 (gmt 0)

10+ Year Member



Mike, that is good advice about validation before putting that information in the to and from parts, thanks.

Mr Fern, thanks for the information on using the curvy brackets. It works well.

Thanks everyone.