Closed Thread Icon

Topic awaiting preservation: can't print variables in HTML mail from PHP formmail (Page 1 of 1) Pages that link to <a href="" title="Pages that link to Topic awaiting preservation: can&amp;#039;t print variables in HTML mail from PHP formmail (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: can&#039;t print variables in HTML mail from PHP formmail <span class="small">(Page 1 of 1)</span>\

Nervous Wreck (II) Inmate

Insane since: Sep 2001

posted posted 12-22-2001 00:34

I have written a formmail in PHP, and I make the email I recieve an HTML mail, by specifying the appropriate headers in the formmail script. But a problem has arisen that I can't seem to figure a way out of. I can't figure out a way to print the fields that a user submits in the form to the email. I'm not sure why. Take a look at the code at . When I recieve the email, the variables and stuff look like


instead of the actual name sent in the form. Any help would be appreciated. Thanks in advance.

Eric Hesterman

[This message has been edited by YoBoyE (edited 12-22-2001).]

Paranoid (IV) Mad Scientist

From: Omicron Persei 8
Insane since: Oct 2000

posted posted 12-22-2001 00:46

not sure if that is the solution, but try to write:

$message = "...";
instead of
$message = '...';

inside type \" instead of all "

hth, but im afraid, not.

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 12-22-2001 11:05

I don't think php recognizes the message as one string.


$message = <<<EOF
bla (your message nice and pretty here)

good luck,

Tyberius Prime

Nervous Wreck (II) Inmate

Insane since: Sep 2001

posted posted 12-22-2001 15:52

Hmm, so far, nothing has worked. I tried GRUMBLE's suggestion a few times, but it's rather odd because with double quotes around the message, it didn't work, and with backslashes before the quotes, in my inbox it didn't even execute any of the html there. The EOF thing didn't work as well, but thanks a lot guys!!

Eric Hesterman

Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 12-24-2001 09:57


I made a solution for that, a bit tricky though...
here you go:

// Read the mail file
$t = 'file_name_with_php_variables.txt';
$fp = fopen($t, 'rb');
$lines = fread($fp, filesize($t));
// add slashes to make valid PHP string
$t = '$t = "' . addcslashes($lines, '"') . '";';
// evaluate the code

your .txt file can contain something like:
hello $name,
and welcome!

in your php code prior to the snippet i gave, you can have this:
$name = 'Yoyo';
[mysnippet here]
now $t will have the same text found in your text file but w/ variables parsed!

Good luck!

Paranoid (IV) Inmate

From: Minneapolis, MN, USA
Insane since: May 2000

posted posted 12-26-2001 20:10

Did you get this fixed? Here's a function I wrote and use in a separately included file. It gathers all form fields and returns a string that contains all the key/value pairs (except for Submit) in a well-aligned format, and adds IP information. Check it:

function gatherOutput() {
$maxLength = 0;
while (list($key,$value) = each($HTTP_POST_VARS)) {
if (strlen($key) > $maxLength) $maxLength = strlen($key);
for ($i=0; $i < $maxLength + 3; $i++) $emptyRow .= ' ';
while (list($key,$value) = each($HTTP_POST_VARS)) {
if ($value != '' && $key != 'Submit') {
$value = preg_replace ('/(\r\n)/',"\\1$emptyRow",$value);
if (is_array($value)) {
$firstRun = 1;
foreach ($value as $item) {
if ($firstRun) {
$pendingValue = $item;
$firstRun = 0;
} else {
$pendingValue .= "\r\n$emptyRow$item";
$value = $pendingValue;
$spacer = ' ';
for($i = 0; $i < ($maxLength - strlen($key)); $i++) $spacer .= ' ';
$output .= "$key$spacer: $value\n";
$output .= "\n\nForm Submitted by: {$HTTP_SERVER_VARS['REMOTE_ADDR']} aka {$HTTP_SERVER_VARS['REMOTE_HOST']}";
return $output;

One of my form processing scripts looks like this:

$emailRecipient = ",";
$emailSubject = "Alumni Group Event Registration Form";
$emailHeaders = "From: TCSU Alumni Group Webpage\nReply-to: $email";
$emailBody = gatherOutput();

mail($emailRecipient, $emailSubject, stripslashes($emailBody), $emailHeaders);


Nervous Wreck (II) Inmate

Insane since: Sep 2001

posted posted 12-30-2001 04:26

Jiblet, that script works to an extent. the only thing that ends up in my mailbox is the IP address and for example, 'Form Submitted by: aka '. do I have to modify any parts of the script? thanks.

Eric Hesterman

Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 12-30-2001 09:33

How do you call PHP script? If you're using GET method it won't work. You must use POST method instead...

<FORM ACTION="blah.php" METHOD="post"></FORM>

Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 12-30-2001 21:12

Also if you are only using this on a local box that is not named the IP is all you'll get.

:[ Computers let you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila. ]:

« BackwardsOnwards »

Show Forum Drop Down Menu