Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 243
  • Last Modified:

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

0
runnerjp
Asked:
runnerjp
  • 8
  • 8
1 Solution
 
Ray PaseurCommented:
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

0
 
runnerjpAuthor Commented:
would i some how need tomatch the users id then get all there emails?
0
 
Ray PaseurCommented:
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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
runnerjpAuthor Commented:
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

0
 
Ray PaseurCommented:
What is the name of the table and the column that has the email address?
0
 
runnerjpAuthor Commented:
users is the table and email is colum
0
 
Ray PaseurCommented:
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

0
 
runnerjpAuthor Commented:
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

0
 
Ray PaseurCommented:
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!
0
 
runnerjpAuthor Commented:
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

0
 
Ray PaseurCommented:
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.
0
 
runnerjpAuthor Commented:
sorry getitng late lol...

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

$mail = row[email];

or something liek that
0
 
Ray PaseurCommented:
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.
0
 
runnerjpAuthor Commented:
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

0
 
Ray PaseurCommented:
You can be sending a mail message, and the mail command can complete properly, but because something is wrong in the headers or your server configuration, the mail may not get delivered.

If this is really the code you are using, you might want to go to the man page for the PHP mail command and read up on what goes into the "from" field.  It will contain certain information that I cannot guess about - you will know what belongs in there.  Who the message is from, etc., is an important part of the message.  You need to fill this in with the correct information!  Give yourself some time to read this page - sending mail is not difficult, once all the correct parts are in place, but the cautions and examples are very important.
http://us3.php.net/manual/en/function.mail.php

Once you have done that, try changing the while loop as follows, so you can see what is going on in your program.  Then post back here with the new code snippet that contains the appropriate variable values for the mail command.

Best, ~Ray


while ($row = mysql_fetch_assoc($res)) {
        extract ($row);
        echo "<br />Sending Mail to: $email \n";
        if (!mail($email, $subj, $text, $from)) {
                echo "<br />MAIL FAILED FOR $email \n";
        }
}

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 8
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now