Sairah
Mar 19 2008, 12:55 PM
So far, my blog's up and running and works great (Thanks!), but I find that I'm constantly checking it (at least once a day?), going through all the pages to see if there's been any comments I may need to reply to. Is there any way of getting e-mail alerts or some other way of being notified of comments posted?
This is the only thing I can think of that the Build-A-Blog series of blogs can't do compared to a blogging service.
Any help on this would be greatly appreciated!
Neuriel
Mar 19 2008, 02:42 PM
Yes, it can be done but you can do this yourself just by looking at how other scripts that send out mail do it. That's how I learned
Sairah
Mar 19 2008, 03:11 PM
Yea, I know, but the form method specified in the scripts is "POST"...I don't know if adding the method "Mail" will confuse the code or not. I don't want one to override the other...plus, since my introduction to SQL, I'm thinking there could probably be a better, more customizable way to do this with php+SQL instead of just guessing html.
=)
Neuriel
Mar 19 2008, 03:33 PM
Exacly what I said, you need to look at how other scripts do it. The actual sending of the mail changes nothing in the form. It's separate from that. Plus, sending mail has nothing to do with SQL at all. Other scripts that send mail can teach you how to do this. For an example, have a look at
this tutorial.
Neuriel
Mar 19 2008, 07:24 PM
Just wanted to add... if you try to figure it out yourself and can't get it to work, post your coding and we can help you further!
Amelie
Mar 19 2008, 08:46 PM
Sairah
Mar 20 2008, 01:05 PM
Thanks, I checked out the topic and it seems like it should work based on the feedback. Right now I have class, but I will check it out again asap. Thanks so much ^^
Sairah
Mar 31 2008, 01:46 PM
Sorry for the late followup, but the code does not seem to be working for me. First I tried it with my own tweaks and it didn't work, so I tried it with changing just the capital things, and it still does. ='( Here's the full code of my process.php page (just in case something else might be messing with it, although I don't think that's the case...)
CODE
<?php
if (isset($_POST['submit'])) {
if (empty($_POST['name']) || empty($_POST['email']) || empty($_POST['comment'])) {
die("Ah! You left one of the required fields blank! Please go back and fill in your name, e-mail address, and a comment. If you don't have an email, type in none@mail.blah");
}
$entry = htmlspecialchars(strip_tags($_POST['entry']));
$timestamp = htmlspecialchars(strip_tags($_POST['timestamp']));
$name = htmlspecialchars(strip_tags($_POST['name']));
$email = htmlspecialchars(strip_tags($_POST['email']));
$url = htmlspecialchars(strip_tags($_POST['url']));
$comment = htmlspecialchars(strip_tags($_POST['comment']));
$comment = nl2br($comment);
if (!get_magic_quotes_gpc()) {
$name = addslashes($name);
$url = addslashes($url);
$comment = addslashes($comment);
}
if (!eregi("^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) {
die("I'm sorry, but you've entered an invalid e-mail address. Please go back and double check it.");
}
include("login.php");
$result = mysql_query("INSERT INTO php_blog_comments (entry, timestamp, name, email, url, comment) VALUES ('$entry','$timestamp','$name','$email','$url','$comment')");
$youraddress = "sairah.bashir@yahoo.com";
$emailsubject = "New comment on entry #" . $entry;
$bodyemail = "A new comment was posted on entry #" . $entry . " of your blog. The comment was made by " . $name . " (" . $email . "; " . $url. ") on " . date('d.m.y \a\\t H:i',$timestamp) . " and their message was "" . $comment . "".";
$extra = "From: Serendipitous Scribbles <" . $youraddress . ">\r\n" . "X-Mailer: PHP/" . phpversion();
mail($youraddress, $emailsubject, $bodyemail, $extra);
header("Location: single.php?id=" . $entry);
}
else {
die("Durf- Error! You can't access this page directly, foo!");
}
?>
Amelie
Mar 31 2008, 06:51 PM
What does it do/not do? What error messages are you getting?
Sairah
Mar 31 2008, 08:40 PM
That's the thing, I'm getting nothing. No error messages, no garbled e-mails. It functions just as if that code were never in there in the first place. o_o I've given a good two days for the e-mail to arrive, lol, JUST IN CASE it was just slow or something XD But no go. =P
Should I be getting any error messages? The comment form works just fine, it just doesn't alert me.
loadx
Mar 31 2008, 10:32 PM
Are you sure that your server is c onfigured to show php warnings and errors?
Also double check that mail does infact work.. just write a simple one liner.
ie.
CODE
mail('fred@spam.com', 'i like toast', 'TOAST!', '');
Sairah
Apr 1 2008, 01:18 AM
loadx, my server does support error messages because I know I got a ton during the BAB tut walkthrough. =P
Am going to go ahead and test the one-liner now.

[EDIT] Well, it appears the mail(); is not working. =O Is there a particular reason for this? Is mail(); something certain servers can't do? I'll be looking this up on the net, but any help would still be appreciated. =)
Amelie
Apr 1 2008, 08:22 AM
Yes, this is something certain servers can't do, but it is largely because it is disabled by hosts. PHP's mail() can be severely abused to send spam and a lot of hosts simply disable it to avoid problems. This is something you will need to ask your host about.
Sairah
Apr 1 2008, 03:29 PM
I guess it takes a long while to reach me, but the test line did in fact, work. I'm going to try the other, normal code once more, just in case.
Sairah
Apr 7 2008, 09:27 PM
Sorry to resurrect this, but the plain mail(); works fine for me (although it tends to take a while).
However, when all the variables are in, it doesn't work at all. ='( I'm not exactly sure why. Here's my code for my process.php:
CODE
<?php
if (isset($_POST['submit'])) {
if (empty($_POST['name']) || empty($_POST['email']) || empty($_POST['comment'])) {
die("Ah! You left one of the required fields blank! Please go back and fill in your name, e-mail address, and a comment. If you don't have an email, type in none@mail.blah");
}
$entry = htmlspecialchars(strip_tags($_POST['entry']));
$timestamp = htmlspecialchars(strip_tags($_POST['timestamp']));
$name = htmlspecialchars(strip_tags($_POST['name']));
$email = htmlspecialchars(strip_tags($_POST['email']));
$url = htmlspecialchars(strip_tags($_POST['url']));
$comment = htmlspecialchars(strip_tags($_POST['comment']));
$comment = nl2br($comment);
if (!get_magic_quotes_gpc()) {
$name = addslashes($name);
$url = addslashes($url);
$comment = addslashes($comment);
}
if (!eregi("^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) {
die("I'm sorry, but you've entered an invalid e-mail address. Please go back and double check it.");
}
include("login.php");
$result = mysql_query("INSERT INTO php_blog_comments (entry, timestamp, name, email, url, comment) VALUES ('$entry','$timestamp','$name','$email','$url','$comment')");
$youraddress = "sairah.bashir@yahoo.com";
$emailsubject = "New Comment on " . $entry;
$bodyemail = "A new comment was posted on entry #" . $entry . " of your blog by " . $name . " (" . $email . "; " . $url. ") on " . date('d.m.y \a\\t H:i',$timestamp) . " and their message was "" . $comment . "".";
$extra = "From: Serendipitous Scribbles <" . $youraddress . ">\r\n" . "X-Mailer: PHP/" . phpversion();
mail($youraddress, $emailsubject, $bodyemail, $extra);
header("Location: single.php?id=" . $entry);
}
else {
die("Durf- Error! You can't access this page directly, foo!");
}
?>
Any ideas what might be throwing it off?
Amelie
Apr 7 2008, 10:04 PM
I think that's something you need to ask your host about. It looks fine to me, but I don't know what your hosts' settings are, and they might have some spam-protection measures that stop your notifications because they look too much like spam to them.
Sairah
Apr 9 2008, 05:47 PM
Hrmm...I'm doing mad experiements on this little bit of code and getting mixed results. Hopefully I'll be on to something!
And as per your suggestion, I did talk to my host about it....they said to check back with them in six hours. -_-'
Sairah
Apr 11 2008, 01:11 PM
Okay, so everything's working now (yay! My provider said I'd called the variables wrong, but I checked, and they didn't change a thing. o_o I kinda think it was an internal sort of problem. -_-' But anyhow).
Now, I'd like for the subject of my notification mails to be "New Comment on TITLE OF POST HERE"
I've tried all sorts of variables and checked after each change to try and see what works. I was successful once- BUT I FORGOT TO CHECK MY E-MAIL AND SEE IF IT WORKS JUST THIS ONCE TIME.
So I've been trying to recreate it but it hasn't been working. Anyone got any clues? =/ I'm desperate!@ T_T
Neuriel
Apr 11 2008, 03:07 PM
That should be taken care of here:
CODE
$emailsubject = "New Comment on " . $entry;
Sairah
Apr 11 2008, 06:34 PM
hehe, I realize that, however, it doesn't work. The subject shows up as "New Comment on" only. Not "New Comment on TITLE HERE"
I changed the "entry" part to "title", as well as tried a bajillion combinations where I add "$title" as a variable, fiddle with the single entry page...yea, lots of stuff. I still can't believe the one time I didn't check my e-mail right away, I had the right combination, and I went and botched it. >.<
Sorry for being such a problem child...I'll go ahead and fiddle with it some more.
[EDIT] I'm just about to rip out all my hair, so frustrated T_T
Amelie
Apr 11 2008, 06:45 PM
You can't get the title as it's in a different table... Unless you want to get complicated with join queries, you can only have the entry ID in your email.
Sairah
Apr 11 2008, 07:18 PM
I'd agree with you, except it did in fact, work once. Take a look:
Sairah
Apr 12 2008, 04:01 PM
I don't know if this will help or not, but I thought it would so...I altered the comments table with a title section, too.
CODE
<?php
include("login.php");
$result = mysql_query("ALTER TABLE php_blog_comments ADD `title` varchar(255) NOT NULL");
if ($result == true) echo 'Table altered successfully.';
else 'Table could not be altered. ' . mysql_error();
mysql_close();
?>
Amelie
Apr 12 2008, 05:43 PM
This won't actually achieve anything, unless when you write and edit your blog entries, you copy the title into the comments table. To be honest, I am not sure I see the point in that. Is it absolutely necessary that you have the title in your emails? The original code I posted should give you the entry ID, which should suffice to distinguish which entry the person commented on...
Sairah
Apr 13 2008, 12:44 AM
Eh, well, I don't "know" my entries based on the ID...since I give them all unique titles (even if they're not called by said titles).
But, thanks for all your help, I decided to stick with the ID and stuff. I just added a bit where there's a link back to the post so I know which one it is. Would've been nice to have the title in the subject, but it's not SUCH a big deal. I'm just a bit anal about specifics. XD
It just sucks that I got it to work and then I didn't. >.<
Ah, well, it's all good. =D
P.S. - Figures I do the one would-be genius fix that actually doesn't do a thing. -_-'
Amelie
Apr 13 2008, 10:47 AM
You can get it to work, but you need an extra query to the database...
After this:
CODE
$result = mysql_query("INSERT INTO php_blog_comments (entry, timestamp, name, email, url, comment) VALUES ('$entry','$timestamp','$name','$email','$url','$comment')");
Add:
CODE
$post_name = mysql_fetch_assoc(mysql_query("SELECT title FROM php_blog WHERE id = $entry"));
Then change your subject line to:
CODE
$emailsubject = "New Comment on " . $post_name['title'];
loadx
Apr 13 2008, 12:42 PM
Why not just do the join query instead of screwing the whole point of database normalization?
Its not even a hard one, you know that if you get the email there is in fact at least one comment and because there is a comment you know that it is linked to one entry.
therefore the relationship is 'one to many'
So the query will be:
CODE
SELECT php_blog_comments.* as phpbc, title FROM php_blog_comments LEFT JOIN php_blog ON php_blog_comments.entry = php_blog.id WHERE php_blog.id = {$id} LIMIT 1;
Or because the join only needs to get back one row its rather useless to use a join. That and the fact that you're sending one mail at a time.
So how about just splitting it into two queries?
So the query will be:
CODE
//after the comment is inserted.
$commentid = mysql_insert_id();
SELECT * FROM php_blog, php_blog_comments WHERE php_blog_comments.entry = {$id} AND php_blog_comments.id = {$commentid};
I realise you don't want to make the blog tutorial complicated but when people want to have complex 'relational' information being pulled then you can't shy them away from join's...After-all this is a relational database not a text file.
This is however all dependant on the fact that you store the comment BEFORE you send the email.
Sairah
Apr 13 2008, 11:38 PM
Loadx, is your way the more "proper" way to do it? I figure I might as well learn the "right" way, first, so that I don't have to unlearn it later. =P
Thank you both for your answers, by the way, I will try them. ^^
loadx
Apr 13 2008, 11:44 PM
MY way conforms to the whole point of 'normalization' meaning we aren't storing the same data twice. We instead link to the two tables together using matching criteria from both tables(join) into one record set which we can use.
Sairah
May 30 2008, 02:42 AM
I'm incredibly sorry for the long wait in between replies, I'd been on a hiatus from blogging =/
I just wanted a quick clarification about where the query would go: I'm thinking of putting it in process.php- after the comment processing but before the mail sending. Is this right?
Sairah
Jun 8 2008, 09:33 PM
Hmm....loadx, your way doesn't work. I get an error when I try it.
CODE
$result = mysql_query("INSERT INTO php_blog_comments (entry, timestamp, name, email, url, comment) VALUES ('$entry','$timestamp','$name','$email','$url','$comment')");
$commentid = mysql_insert_id();
SELECT * FROM php_blog, php_blog_comments WHERE php_blog_comments.entry = {$id} AND php_blog_comments.id = {$commentid};
$myaddy = "sairah@endless-time.net";
$subject = "New Comment on " . $commentid;
$body = "" . $name . " (" . $email . " / " . $url . ") commented:
" . $comment . "
Link: http://www.music.endless-time.net/single.php?id=" . $entry . "";
$extra = "From: Euphonious Euphoria <" ."sairah@endless-time.net". ">\r\n" . "X-Mailer: PHP/" . phpversion();
mail($myaddy, $subject, $body, $extra);
Error:"Parse error: syntax error, unexpected T_STRING in /var/hsphere/local/home/sairah/music.endless-time.net/process.php on line 40"
Am I doing something wrong (again)?
loadx
Jun 9 2008, 08:54 AM
dude.. the query isn't even being called right.
CODE
$commentid = mysql_insert_id();
$commentQuery = mysql_query("SELECT * FROM php_blog, php_blog_comments WHERE php_blog_comments.entry = {$id} AND php_blog_comments.id = {$commentid}");
if(mysql_num_rows($commentQuery) > 0 && !mysql_error()){
$commentData = mysql_fetch_assoc($commentQuery);
}
Then in your mailout part you need to use what you get back from $commentData.
(print_r($commentData) will show you what you have access to) :|
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please
click here.