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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping through vars and sending to mail 1

Status
Not open for further replies.

rninja

Technical User
Apr 11, 2001
381
US
I am pretty sure there is a way to do this but I am having errors.

I want to be able to loop through $_POST variables and place the output into an email. What I am doing is supplying a form that sends to itself then if the form was completed, the variables get looped into a message that is mailed out. The only problem is that It doesn't include the actual output.

I've tried using $message= file_get_contents("test.html"); to include the code, but it returns the text and code as one, it doesn't parse.

I appreciate help on this one!

Rninja

smlogo.gif

 
Well if I were to do a loop in a regular PHP page like so:

foreach($_POST as $key=>$value){
echo $key." - ".$value;
}

It would come out fine.
But if I wanted to have that output in an email like so:

$message = foreach($_POST as $key=>$value){
echo $key." - ".$value;
};

mail($mailfrom,$subject,$message);

I would not get an email with the contents of that loop, rather the script might not run or would produce an empty body in the email.

What I want to accomplish is to get that output from the loop sent into an email in the mail() format.

Preferably, I would like to have a seperate file that I could place the loop and any HTML or php code into that would port to email from a PHP script.

Thanks.

Rninja

smlogo.gif

 
What you want is:
Code:
$message = '';
foreach($_POST as $key=>$value) 
       $message .= $key." - ".$value."\n";

Ken
 
A little more technical explanation:
The dot operator . in PHP concatenates strings. You can, just as kenrbnsn shows, just keep adding to a string variable when you loop through an array. The .= construct adds to the already existing string, appending the assigned value.
 
Thank you guys, that was exactly what I needed to know. I did try using the concatenation operator, but I didn't use it properly.

Rninja

smlogo.gif

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top