Click to See Complete Forum and Search --> : adding auto reply ??


mentaleruptions
10-14-2003, 09:14 PM
Hi there im using a php file on a guestbook that submits entries to a text file and also sends the entries to my email inbox so i no what has been added without having to view my site but i cant work out how to add auto response so it will send an email to the persons email box saying thanks.
Heres the php file im using

<?

//No need to edit anything below here.
if (!isset($name) || !isset($email) || !isset($message) || empty($name) || empty($email) || empty($message)) {
print "&result=Fail";
print "&errorMsg=" . urlencode("Input required for all fields.");
exit;
}

$email = strtolower($email);

addentry($name, $email, $website, $message);

function addentry($name, $email, $website, $message) {

$posted = strftime("%D %I:%M %p");

$message = stripslashes($message);

$file = fopen('entry.txt', 'a+');

if (!$file) {
print "&result=Fail";
print "&errorMsg=" . urlencode("Could not open entry.txt file. Change CHMOD levels to 766.");
exit;
}

fputs($file, "<font color=\"#000000\">Name:</font> $name\n<font color=\"#000000\">Email:</font> <font color=\"#990000\"><u><A href=\"mailto:$email\">$email</A></u></font><br>\n<font color=\"#000000\">Website:</font> <font color=\"#990000\"><u><A href=\"http://$website\"target=\"_blank\">$website</A></u></font><br>\n<font color=\"#000000\">Posted:</font> $posted\n<font color=\"#000000\">Message:</font> $message\n\n");
fclose($file);

// Send admin an email when new entry occurs
mailAdmin($name, $email, $website, $message);
}

function mailAdmin($name, $email, $website, $message) {
$mailTo = "you@youremail.com";
$mailFrom = "From: <you@youremail.com>";
$mailSubject = "New Guestbook Entry";
$mailBody = "A visitor to your site has left the following information in your guestbook:\n
Name: $name
Email: $email
Website: $website
The visitor commented:
------------------------------
$message
------------------------------
You can view the message at:
http://www.yoursite.com";
mail($mailTo, $mailSubject, $mailBody, $mailFrom);
}

print "&result=okay";
exit;

?>


Any help would be appreciated.

pyro
10-15-2003, 07:04 AM
Add this after you mailAdmin function:

mail($email, "Your Subject", "Thank you message", "From: Autoresponder");

mentaleruptions
10-15-2003, 10:05 AM
Hi pyro thanks for the help it works great.

However is there an easier way for me to define adding ur own email address into the php file ??

Like if i added:

$adminaddress = "you@yoursite.com";

Near the top of the file and then changing:

$mailTo = "you@yoursite.com";

To:

$mailTo = "$adminaddress";

Would this work ??

Also since my guestbook is using php and adding the entries to a text file would there be anyway for me to open that text file in the browser via a form of some kind to edit and remove entries and then saving the file on the server without manualy doing it ??

So i would have a little admin kind of tool ??

Thanks for all the help again :)

pyro
10-15-2003, 11:08 AM
Yes, that would work, though I'd remove the quotes, like this:

$mailTo = $adminaddress;Also, not sure it's all the much easier...

As far as an admin panel, try something like this:

<?PHP

$filename = "temp/guestbook.txt";

##update file

if (isset($_POST['submit'])) {
$gb = stripslashes(trim($_POST['guestbook']));

$fp = fopen ($filename, "w");
flock ($fp, LOCK_EX); #lock the file
fwrite ($fp, $gb);
flock($fp, LOCK_UN); #release the file
fclose ($fp);
$msg = "<p style=\"font-weight: bold;\">Guestbook Updated</p>\n";
}

##read file

$contents = file($filename);
$file = "";
foreach ($contents as $line) {
$file .= $line;
}

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Administrative Panel</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?PHP if (isset($msg)) { echo $msg; } ?>
<form action="<?PHP echo $_SERVER['PHP_SELF']; ?>" method="post">
<p>
<textarea rows="15" cols="50" name="guestbook"><?PHP echo $file; ?></textarea><br>
<input type="submit" name="submit" value="Update List">
</p>
</form>
</body>
</html>

mentaleruptions
10-17-2003, 12:53 PM
Hi pyro thanks for the help u got me on the right track :).

I did manage to create something similar using flash but i have a little problem.
When i add entries to the guestbook they are written into a text file like this:


&result=okay&entries=<font color="#000000">Name:</font> test
<font color="#000000">Email:</font> <font color="#990000"><u><A href="mailto:test">test</A></u></font><br>
<font color="#000000">Website:</font> <font color="#990000"><u><A href="http://test"target="_blank">test</A></u></font><br>
<font color="#000000">Posted:</font> 10/15/03 05:40 PM
<font color="#000000">Message:</font> test

<font color="#000000">Name:</font> test
<font color="#000000">Email:</font> <font color="#990000"><u><A href="mailto:test">test</A></u></font><br>
<font color="#000000">Website:</font> <font color="#990000"><u><A href="http://test"target="_blank">test</A></u></font><br>
<font color="#000000">Posted:</font> 10/15/03 05:40 PM
<font color="#000000">Message:</font> test


And thats all great i can access that file from this flash movie and edit it and have it update BUT heres where the problem lies.
When i submit the changes i make instead of it being formatted like above it does this:


&result=okay& entries=%3Cfont+color%3D%5C%22%23000000%5C%22%3ENa
me%3A%3C%2Ffont%3E+tom%0D%3Cfont+color%3D%5C%22%23
000000%5C%22%3EEmail%3A%3C%2Ffont%3E+%3Cfont+color
%3D%5C%22%23990000%5C%22%3E%3Cu%3E%3CA+href%3D%5C%
22mailto%3Ablah%5C%22%3Eblah%3C%2FA%3E%3C%2Fu%3E%3
C%2Ffont%3E%3Cbr%3E%0D%3Cfont+color%3D%5C%22%23000
000%5C%22%3EWebsite%3A%3C%2Ffont%3E+%3Cfont+color%
3D%5C%22%23990000%5C%22%3E%3Cu%3E%3CA+href%3D%5C%2
2http%3A%2F%2Fwww.bla.com%5C%22target%3D%5C%22_blank%5C%22%3Ewww.bla. com%3C%2FA%3E%3C%2Fu%3E%3C%2Ffont%3E%3Cbr%3E%0D%3C
font+color%3D%5C%22%23000000%5C%22%3EPosted%3A%3C%
2Ffont%3E+10%2F17%2F03+04%3A33+PM%0D%3Cfont+color%
3D%5C%22%23000000%5C%22%3EMessage%3A%3C%2Ffont%3E+
just+testing%0D%0D%3Cfont+color%3D%5C%22%23000000%
5C%22%3EName%3A%3C%2Ffont%3E+blah%0D%3Cfont+color%
3D%5C%22%23000000%5C%22%3EEmail%3A%3C%2Ffont%3E+%3
Cfont+color%3D%5C%22%23990000%5C%22%3E%3Cu%3E%3CA+
href%3D%5C%22mailto%3Ablah%5C%22%3Eblah%3C%2FA%3E%
3C%2Fu%3E%3C%2Ffont%3E%3Cbr%3E%0D%3Cfont+color%3D%
5C%22%23000000%5C%22%3EWebsite%3A%3C%2Ffont%3E+%3C
font+color%3D%5C%22%23990000%5C%22%3E%3Cu%3E%3CA+h
ref%3D%5C%22http%3A%2F%2Fblah%5C%22target%3D%5C%22
_blank%5C%22%3Eblah%3C%2FA%3E%3C%2Fu%3E%3C%2Ffont%
3E%3Cbr%3E%0D%3Cfont+color%3D%5C%22%23000000%5C%22
%3EPosted%3A%3C%2Ffont%3E+10%2F17%2F03+04%3A34+PM%
0D%3Cfont+color%3D%5C%22%23000000%5C%22%3EMessage%
3A%3C%2Ffont%3E+blah%0D%0D


Now because it does that the guestbook nor the admin panel reads the file.

Heres the php file that submits the changes to the text file for the admin panel:

<?
$datafile = "entry.txt";

$data = "&result=okay&entries=" . urlencode($HTTP_POST_VARS['message']);

$file = @fopen($datafile, 'w');

if (!$file) {
// Report error and exit
print "&result=Fail";
exit;
}

fwrite($file, $data);
fclose($file);

print "&result=Okay";
?>

Now all the flash files does is lets u access the text file nothing else.
So is there a way to get this php file to just submit the changes without messing up the original formatting ??
I have used the same admin thing to just update text and stuff and that works fine but when it comes to formatting im stuck.
I can send u the files if u would want to take a look at them ??

Thanks again for ur help.

Tom

pyro
10-17-2003, 01:43 PM
Looks like you need to run in through urldecode() (http://us4.php.net/manual/en/function.urldecode.php). Also, the TXT file (entry.txt) needs to be CHMODed to 666.

mentaleruptions
10-17-2003, 02:10 PM
Hi pyro thanks i changed the php file to this:


<?
$datafile = "entry.txt";

$data = "&result=okay&entries=" . urldecode($HTTP_POST_VARS['message']);

$a = explode('&', $QUERY_STRING);
$i = 0;
while ($i < count($a)) {
$b = split('=', $a[$i]);
echo 'Value for parameter ', htmlspecialchars(urldecode($b[0])),
' is ', htmlspecialchars(urldecode($b[1])), "<br />\n";
$i++;
}

$file = @fopen($datafile, 'w');

if (!$file) {
// Report error and exit
print "&result=Fail";
exit;
}

fwrite($file, $data);
fclose($file);

print "&result=Okay";
?>


But now it does this:


&result=okay&entries=<font color=\"#000000\">Name:</font> test
<font color=\"#000000\">Email:</font> <font color=\"#990000\"><u><A href=\"mailto:test\">test</A></u></font><br>
<font color=\"#000000\">Website:</font> <font color=\"#990000\"><u><A href=\"http://test\"target=\"_blank\">test</A></u></font><br>
<font color=\"#000000\">Posted:</font> 10/17/03 07:57 PM
<font color=\"#000000\">Message:</font> test

<font color=\"#000000\">Name:</font> test
<font color=\"#000000\">Email:</font> <font color=\"#990000\"><u><A href=\"mailto:test\">test</A></u></font><br>
<font color=\"#000000\">Website:</font> <font color=\"#990000\"><u><A href=\"http://test\"target=\"_blank\">test</A></u></font><br>
<font color=\"#000000\">Posted:</font> 10/17/03 07:57 PM
<font color=\"#000000\">Message:</font> test

<font color=\"#000000\">Name:</font> test
<font color=\"#000000\">Email:</font> <font color=\"#990000\"><u><A href=\"mailto:test\">test</A></u></font><br>
<font color=\"#000000\">Website:</font> <font color=\"#990000\"><u><A href=\"http://test\"target=\"_blank\">test</A></u></font><br>
<font color=\"#000000\">Posted:</font> 10/17/03 07:57 PM
<font color=\"#000000\">Message:</font> test


Its adding a bunch of \ to it now.
I no im bugging but im still learning here as im going.

pyro
10-17-2003, 02:15 PM
Easy enought to fix. Just run it through stripslashes() (http://us3.php.net/manual/en/function.stripslashes.php).

Also, might I recommend not using the font tag, and using <span>'s with CSS to format it?

mentaleruptions
10-21-2003, 03:57 PM
Hey pyro just posting to thank you so much for all ur help.
After u pointed me in the right direction i got everything working and the cms now posts the entries in the same formatt so both the guestbook and cms reads the files :).
The end result of the php is:


<?
$datafile = "entry.txt";
$data = "&result=okay&entries=" . urldecode(stripslashes($HTTP_POST_VARS['message'])
);
$a = explode('&', $QUERY_STRING);
$i = 0;
while ($i < count($a)) {
$b = split('=', $a[$i]);
echo 'Value for parameter ', htmlspecialchars(urldecode($b[0])),
' is ', htmlspecialchars(urldecode($b[1])), "<br />\n";
$i++;
}

$file = @fopen($datafile, 'w');

if (!$file) {
// Report error and exit
print "&result=Fail";
exit;
}

fwrite($file, $data);
fclose($file);

print "&result=Okay";

?


I appreciate all ur help.

Tom

pyro
10-22-2003, 07:04 AM
You are welcome... :)

mentaleruptions
10-22-2003, 12:08 PM
Hi again pyro :).

I kind of figured out a way to get each new entry to the top of the text file and it does actualy work great.

However for my guestbook to work the text file must have:


&result=okay&entries=


At the very front of it and then the entries after it.
Now the way i now have my php file it looks like this:


<?

//No need to edit anything below here.
if (!isset($name) || !isset($email) || !isset($message) || empty($name) || empty($email) || empty($message)) {
print "&result=Fail";
print "&errorMsg=" . urlencode("Input required for all fields.");
exit;
}

$email = strtolower($email);

addentry($name, $email, $website, $message);

function addentry($name, $email, $website, $message) {

$posted = strftime("%D %I:%M %p");

$message = stripslashes($message);

$filename = "entry.txt";
$fp = fopen( $filename,"r");
$OldData = fread($fp, 80000);
fclose($fp);

$Input = "<font color=\"#000000\">Name:</font> $name\n<font color=\"#000000\">Email:</font> <font color=\"#990000\"><u><A href=\"mailto:$email\">$email</A></u></font><br>\n<font color=\"#000000\">Website:</font> <font color=\"#990000\"><u><A href=\"http://$website\"target=\"_blank\">$website</A></u></font><br>\n<font color=\"#000000\">Posted:</font> $posted\n<font color=\"#000000\">Message:</font> $message\n\n";

$New = "&result=okay&entries=$Input$OldData";

$fp = fopen( $filename,"w");
fwrite($fp, $New);
fclose($fp);

// Send admin an email when new entry occurs
mailAdmin($name, $email, $website, $message);
}

function mailAdmin($name, $email, $website, $message) {
$mailTo = "you@yoursite.com";
$mailFrom = "From: <you@yoursite.com>";
$mailSubject = "New Guestbook Entry";
$mailBody = "A visitor to your site has left the following information in your guestbook:\n
Name: $name
Email: $email
Website: $website
The visitor commented:
------------------------------
$message
------------------------------
You can view the message at:
http://www.yoursite.com";
mail($mailTo, $mailSubject, $mailBody, $mailFrom);
mail($email, "Your Subject", "Thank you message", "From: Autoresponder");
}

print "&result=okay";
exit;

?>


So now it opens the file takes the old info, posts the new info and adds the old info below it but i cant get it to strip the old info of:


&result=okay&entries=


But it will post the new info with that code because of:


$New = "&result=okay&entries=$Input$OldData";


Now when it adds to the text file it does this:


&result=okay&entries=Name: blah Email: blah
Website: blah
Posted: 10/22/03 05:12 PM Message: blah &result=okay&entries=Name: haha Email: haha
Website: haha
Posted: 10/22/03 05:12 PM Message: haha


As u see the bit of code that should be at the front is now at the front of each entry and i dont want that.
So is there a way to strip the old info of the:


&result=okay&entries=


Before adding the new code with that bit added to it ??

I no ive been bugging u here but i will give u credits when i post this over at flashkit.com again.

Tom

pyro
10-22-2003, 05:51 PM
Run your $OldData variable through this:

$OldData = preg_replace("/^&result=okay&entries=/", "", $OldData);or:

$OldData = substr($OldData, 21);Both of them will strip the characters off the front.