Liam
Liam

Reputation: 9855

Get all POST data and send in email

Im trying to use PHP Mail function to send myself an email of all post variables.

So far I have this...

$message = foreach ($_POST as $key => $value)
echo "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)."<br>";

$message = wordwrap($message, 70);

mail('[email protected]', 'sghting', $message);

Only the message being submitted is my last post record, can anybody see where im going wrong?

Upvotes: 10

Views: 11522

Answers (3)

Erik Thiart
Erik Thiart

Reputation: 391

I personally just use var_export() for this kind of thing.

$message = var_export($_POST,true);

If used and set to TRUE, var_export() will return the variable representation instead of outputting it.

Practice example would be:

mail('[email protected]', 'Export of the Post Data', var_export($_POST,true));

OR if you want to do something more pretty

$posted_data = var_export($_POST,true);
$message = '
<h4> Debug Registration Form on XXX </h4>
<p>Here is a dump of the posted data</p>
<pre>
'.$posted_data.'
</pre>
';
mail('[email protected]', 'Export of the Posted Data', $message);

Upvotes: 1

Vikash Kumar
Vikash Kumar

Reputation: 222

$message = "";
foreach ($_POST as $key => $value)
$message .= "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)."<br>";

mail('[email protected]', 'sghting', $message);

Upvotes: 1

Mitch Satchwell
Mitch Satchwell

Reputation: 4830

foreach ($_POST as $key => $value)
    $message .= "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)."<br>";

mail('[email protected]', 'sghting', $message);

$message = foreach ($_POST as $key => $value) is not correct, this will iterate over the results and store the last one. You want to store the values in your $message variable, not echo them.

Upvotes: 19

Related Questions