Forum Moderators: coopster
I have this little guestbook script, in which the visitor enters his comments in a form, these comments are written to a database and shown on a different page.
Now I would like to get the script to send out an email to the admin every time a new comment is posted.
I've receive several tips on the mail function in PHP, but I can't find out how/where to implement this code into my process.php file.
Can s.o. help me out?
This is the process.php code:
<?
if ($_POST["name"] == "") $name = "Anonymous";
else $name = $_POST["name"];
if ($_POST["email"] == "") $email = "None";
else $email = $_POST["email"];
if ($_POST["url"] == "") $url = "None";
else $url = $_POST["url"];
if ($_POST["comment"] == "") {
header("Location: comment.php");
exit(0);
}
$comment = $_POST["comment"];
$time = time();
$query = "INSERT INTO comments (name, email, url, comment, time) VALUES ('" . $name . "', '" . $email . "', '" . $url . "', '" . $comment . "', " . $time . ")";
mysql_connect("localhost", "db_user", "password");
mysql_select_db("db_name");
mysql_query($query);
mysql_close();
header("Location: thanks.php");
exit(0);
?>
Greetz!
Mieke
Just add it right in there after your checks on the POST variables.
$subject = "New Comment!";
$message = "name: ".$name."\r\n comment: ".$comment." etc..";
[url=http://php.net/manual/en/function.mail.php]mail[/url]("admin@example.com",$subject,$message);
Refer to the link above for more information. You may want to change the headers, too.
Best of luck! :)