send email when new comment posted

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ninethousandfeet
Forum Contributor
Posts: 130
Joined: Tue Mar 10, 2009 4:56 pm

send email when new comment posted

Post by ninethousandfeet »

hello,
i am looking for a little guidance... i allow users to either start a new conversation topic on other user's profile, or comment on existing conversations. my commentTable is pretty much sorted by the comment title for right now, which is another issue i needs to work out eventually, but my main question for now is this; how can i send an email to all users involved in an existing conversation when a comment is posted in that conversation?
my sql should be ok.. select * from commentTable where comment_title = $comment_title... now i just can't figure out how to structure the email script to send to all emails where $row['email']?

current email script and then the SQL:

Code: Select all

 
if (!$error) {
  $username = $_POST['username'];
  $commenttitle = $_POST['comment_title'];
 
      
  $insertSQL = sprintf("INSERT INTO testComment (user_id, username, email, comment_id, comment_username, comment_title, `comment`, comment_date) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['user_id'], "int"),
               GetSQLValueString($_POST['username'], "text"),
               GetSQLValueString($_POST['email'], "text"),
                       GetSQLValueString($_POST['comment_id'], "int"),
                       GetSQLValueString($_POST['comment_username'], "text"),
                       GetSQLValueString($_POST['comment_title'], "text"),
                       GetSQLValueString($_POST['comment'], "text"),
                       GetSQLValueString($_POST['comment_date'], "defined", 'NOW()'));
 
  mysql_select_db($database_connUser, $connUser);
  $Result1 = mysql_query($insertSQL, $connUser) or die(mysql_error());
 
  $insertGoTo = "openconversation.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  $headers = "From: me>";
    $to = $_POST['email']; // user email(s) MY PROBLEM MAY BE HERE??
    $subject = 'You have a new comment!';
    // build message
    $message = "Hello, $username!\r\n\r\n";
    $message .= "A conversation you are involved in has a new post.\r\n\r\n";
    $message .= "The conversation topic is: $commenttitle \r\n\r\n";
  $mailSent = mail($to, $subject, $message, $headers);
  header(sprintf("Location: %s", $insertGoTo));
  }
}
 

Code: Select all

 
$var1_getOtherUser = "-1";
if (isset($_GET['comment_title'])) {
  $var1_getOtherUser = $_GET['comment_title'];
}
mysql_select_db($database_connUser, $connUser);
$query_getOtherUser = sprintf("SELECT testComment.user_id, testComment.username, testComment.email, testComment.comment_username, testComment.comment_title FROM testComment WHERE testComment.comment_title = %s GROUP BY testComment.comment_title", GetSQLValueString($var1_getOtherUser, "int"));
$getOtherUser = mysql_query($query_getOtherUser, $connUser) or die(mysql_error());
$row_getOtherUser = mysql_fetch_assoc($getOtherUser);
$totalRows_getOtherUser = mysql_num_rows($getOtherUser);
 
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: send email when new comment posted

Post by mattpointblank »

You just need a loop. Write a query to select all user email address involved in the discussion, then use a while loop to run through each one and send them an email using the mail() function inside the loop.
ninethousandfeet
Forum Contributor
Posts: 130
Joined: Tue Mar 10, 2009 4:56 pm

Re: send email when new comment posted

Post by ninethousandfeet »

so this is what i have right now as one of the fields in my insert comment section ( and this is to insert a comment into an existing conversation),

Code: Select all

   <input name="email" type="hidden" id="email" value="<?php do { ?><?php echo $row_getOtherUser['email']; ?><?php } while ($row_getOtherUser = mysql_fetch_assoc($getOtherUser)); ?>" />
the way this stands, only the email of the commenting user ($row_getOtherUser['email']), the email for all others involved in the conversation do not appear in the database column for email.

should i perform the loop somewhere else in the code? sorry, i'm new to php so this is all still a big learning experience.

thanks for any help you can offer.
Post Reply