Link to home
Start Free TrialLog in
Avatar of runnerjp
runnerjp

asked on

getting users email and sending them in a loop

ok i have my code below that checks to see if a user wants to recive emails about new posts on a topic...

after doing so i would like to know how i could get each users email from the table users and then send each user an email saying that the topic has been updated
$query = "SELECT fe.*, u.email, u.ID FROM forum_email as fe INNER JOIN users as u ON u.ID = fe.user_id WHERE fe.topic_id = " . $forumpostid . "  AND fe.mailed=1";
$res = @mysql_query($query) or die ("sendNotification query fails :".mysql_error());
echo $forumpostid; echo $id;
//nobody wants an email, stop now
if(mysql_num_rows($res)==0)
{
 echo "no records returned";
        exit;
}

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Try something like this.  You might want to check the variable name for $email - I am not sure of that.

Good luck, ~Ray
$query = "SELECT fe.*, u.email, u.ID FROM forum_email as fe INNER JOIN users as u ON u.ID = fe.user_id WHERE fe.topic_id = " . $forumpostid . "  AND fe.mailed=1";
$res = @mysql_query($query) or die ("sendNotification query fails :".mysql_error());
echo $forumpostid; echo $id;
//nobody wants an email, stop now
if(mysql_num_rows($res)==0)
{
 echo "no records returned";
        exit;
}
 
// ADDED CODE TO SEND EMAIL MESSAGES
$subj = 'Forum Updated';
$from = ' [your return information ] ';
$text = ' [your message text] ';
 
while ($row = mysql_fetch_assoc($res)) {
	extract ($row);
	if (!mail($email, $subj, $text, $from)) {
		echo "<br />MAIL FAILED FOR $email \n";
	}
}

Open in new window

Avatar of runnerjp
runnerjp

ASKER

would i some how need tomatch the users id then get all there emails?
I don't know.  What is in your data tables?  If they can have multiple emails per user id, then I guess the answer could be "yes. "  If I subscribed to a forum with two different emails, I would expect to receive notices at both emails.  That is really a question for you to decide - it is part of the business logic, and not a technical issue.
no they use just 1 but im not sure how to pull them with this query


$query = "SELECT fe.*, u.email, u.ID FROM forum_email as fe INNER JOIN users as u ON u.ID = fe.user_id WHERE fe.topic_id = " . $forumpostid . "  AND fe.mailed=1";
$res = @mysql_query($query) or die ("sendNotification query fails :".mysql_error());
echo $forumpostid; echo $id;
//nobody wants an email, stop now
if(mysql_num_rows($res)==0)
{
 echo "no records returned";
        exit;
}
 

Open in new window

What is the name of the table and the column that has the email address?
users is the table and email is colum
It looks like you are getting that information from the query you have above.  Try doing this and you can see what you are selecting in the output of var_dump().
$query = "SELECT fe.*, u.email, u.ID FROM forum_email as fe INNER JOIN users as u ON u.ID = fe.user_id WHERE fe.topic_id = " . $forumpostid . "  AND fe.mailed=1";
$res = mysql_query($query) or die ("$query fails : ".mysql_error());
$row = mysql_fetch_assoc($res);
var_dump($row);

Open in new window

i got this

array(5) { ["user_id"]=>  string(1) "1" ["topic_id"]=>  string(3) "484" ["mailed"]=>  string(1) "1" ["email"]=>  string(25) "admin@runningprofiles.com" ["ID"]=>  string(1) "1" } 4841
so what would $email=
so for this


// ADDED CODE TO SEND EMAIL MESSAGES
$subj = 'Forum Updated';
$from = ' [your return information ] ';
$text = ' [your message text] ';
 
while ($row = mysql_fetch_assoc($res)) {
        extract ($row);
        if (!mail($email, $subj, $text, $from)) {
                echo "<br />MAIL FAILED FOR $email \n";
        }
}

Open in new window

That's perfect - when you do the "extract($row)" command, you will have a local variable named $email that will go right into the mail command.  Try it and see!
ok i tred it and did it like the code below and had not email sent to me and just array(5) { ["user_id"]=>  string(1) "1" ["topic_id"]=>  string(3) "484" ["mailed"]=>  string(1) "1" ["email"]=>  string(25) "admin@runningprofiles.com" ["ID"]=>  string(1) "1" } 4841 was outputted
		$query = "SELECT fe.*, u.email, u.ID FROM forum_email as fe INNER JOIN users as u ON u.ID = fe.user_id WHERE fe.topic_id = " . $forumpostid . "  AND fe.mailed=1";
$res = mysql_query($query) or die ("$query fails : ".mysql_error());
$row = mysql_fetch_assoc($res);
var_dump($row);
echo $forumpostid; echo $id;
//nobody wants an email, stop now
if(mysql_num_rows($res)==0)
{
 echo "no records returned";
        exit;
}
 
// ADDED CODE TO SEND EMAIL MESSAGES
$subj = 'Forum Updated';
$from = ' [your return information ] ';
$text = ' [your message text] ';
 
while ($row = mysql_fetch_assoc($res)) {
        extract ($row);
        if (!mail($email, $subj, $text, $from)) {
                echo "<br />MAIL FAILED FOR $email \n";
        }
}

Open in new window

runnerjp: You need AT LEAST A LITTLE PHP knowledge to make progress here.  I gave you a test case so we could see what is in the query results set.  Now that we know that, you should know to remove the test case statements, right??

Pull lines 3, 4, and 5 from the code you posted above and try it again.
sorry getitng late lol...

i now have nothing... no email sent and no  echo "<br />MAIL FAILED FOR $email \n";
what should i be setting $mail to??

$mail = row[email];

or something liek that
How do you know you have no email sent?  Please post the most recent version of the code.

Also, read the man page here and see if it makes sense to use the extract command.
http://us3.php.net/manual/en/function.extract.php

Extract should do the right think with the $row that is retrieved by mysql_fetch_assoc.
i know because i hav'nt got an emai yet :D
$query = "SELECT fe.*, u.email, u.ID FROM forum_email as fe INNER JOIN users as u ON u.ID = fe.user_id WHERE fe.topic_id = " . $forumpostid . "  AND fe.mailed=1";
$res = mysql_query($query) or die ("$query fails : ".mysql_error());
//$row = mysql_fetch_assoc($res);
//var_dump($row);
//echo $forumpostid; echo $id;
//nobody wants an email, stop now
if(mysql_num_rows($res)==0)
{
 echo "no records returned";
        exit;
}
 
// ADDED CODE TO SEND EMAIL MESSAGES
$subj = 'Forum Updated';
$from = ' [your return information ] ';
$text = ' [your message text] ';
 
while ($row = mysql_fetch_assoc($res)) {
        extract ($row);
        if (!mail($email, $subj, $text, $from)) {
                echo "<br />MAIL FAILED FOR $email \n";
        }
}
 
//set emailed to 'y' so that they don't get numerous emails... set it back to 'n' when they view this topic
$query_noted = "UPDATE forum_email SET mailed='0' WHERE topic_id = '".$forumtopicid."'";

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial