joshuaer Posted August 24, 2012 Share Posted August 24, 2012 Hello I am trying to create a sendmail script that pulls a list that a user has created out of a database that is associated with their user id and sends it to the email associated with their user id, Each item they add is put into a row called list and the userid for the person who is creating the item is also put in a row called userid I can get it to send the email to the users email address but it is not pulling the rows for that user, can someone help point me in the right direction please Thank you <?phpini_set('display_errors', 1);error_reporting(E_ALL);include("sql.php"); //Connect to SQLinclude("auth.php"); //Connect to SQL$UID= $_SESSION['UID'];$headers = 'header';$d = date ("D");$result = mysql_query("SELECT * FROM users where UID='$UID'");$mail_hrd = "From: ;"; $txt = "Your List ";if(mysql_num_rows($result) > 0){$count = 0;while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)){$message = $txt . $row['list'].' ' . $row['type'];$to = $row['email'];mail($to, $headers, $message, $mail_hrd);$count++;}echo "myResult=$count Emails Sent. Done.";}else{echo "myResult=Email Submissions Failed.";}?> Link to comment Share on other sites More sharing options...
justsomeguy Posted August 24, 2012 Share Posted August 24, 2012 You need to add another query where you get the records from the other table, and loop through those records to build the text for the message. Link to comment Share on other sites More sharing options...
joshuaer Posted August 24, 2012 Author Share Posted August 24, 2012 Thank you for the help I added the second query and it is now pulling the first row of the list not looping through the rows to get all of the list associated with the user id <?phpini_set('display_errors', 1);error_reporting(E_ALL);include("sql.php"); //Connect to SQLinclude("auth.php"); //Connect to SQL$UID= $_SESSION['UID'];$headers = 'header';$d = date ("D");$sel = mysql_query("SELECT * FROM users where UID='$UID' AND email != ''");$result = mysql_query("SELECT * FROM users where UID='$UID' AND iChore1 != ''");$mail_hrd = "From: ;"; $txt = "Your List ";if(mysql_num_rows($result) > 0){$count = 0;while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)){$message = $txt . $row['list'].' ' . $row['type'];while ($rows = mysql_fetch_array ($sel, MYSQL_ASSOC)){$to = $rows['email'];mail($to, $headers, $message, $mail_hrd);$count++;}}echo "myResult=$count Emails Sent. Done.";}else{echo "myResult=Email Submissions Failed.";}?> Link to comment Share on other sites More sharing options...
justsomeguy Posted August 24, 2012 Share Posted August 24, 2012 What's your database set up like? Do you have multiple rows in the users table for the same user with different data in each row? The code you have now will send several emails to the user, each one with just one item. Are you trying to send a single email with everything in it? Link to comment Share on other sites More sharing options...
joshuaer Posted August 25, 2012 Author Share Posted August 25, 2012 (edited) I want to send a single email with everything in it Edited August 25, 2012 by joshuaer Link to comment Share on other sites More sharing options...
justsomeguy Posted August 27, 2012 Share Posted August 27, 2012 That means you need to move the mail function outside of the loop. The loop should build the email text, and then after the loop and after the email is built you send it. How many rows would the first query return, are there multiple rows in that table for the same user with one or more email addresses? I'm confused about how your users table is set up and what data it holds. It has multiple rows per user, correct? Link to comment Share on other sites More sharing options...
joshuaer Posted September 4, 2012 Author Share Posted September 4, 2012 Thanks for the response and sorry for the delay in response The list it is creating is a grocery type list, I am trying to create a button that emails the list to the email address the user has associated with its unique id. my rows are ID | username | password | list | UID | email | type | amount01 testuser 123456 1234567 test@test.com 02 bread 1234567 pkg 103 milk 1234567 gal 1 So the email I would like for it to contain the List, type and amount information Link to comment Share on other sites More sharing options...
birbal Posted September 4, 2012 Share Posted September 4, 2012 you can prepare the list inside the while loop where associated id matches. and outside the loop you can send the mail in fetched email id of that associated user id account. Link to comment Share on other sites More sharing options...
justsomeguy Posted September 4, 2012 Share Posted September 4, 2012 It's pretty weird to have all of that incongruous data in the same table. It would make more sense to have a users table that only holds users, a lists table that holds the different lists for each user (list name, user, date, etc), and a list_items table that holds all of the items on each list. I guess with that structure you can do a query to get records where the email address is not empty, and that's your list of users. Then you can loop through that one and, for each user, do another query where the list ID matches the list ID from the user you're processing. That should get all items on their list. You can loop through that result and build the email text, then send the email and go on to the next person. Link to comment Share on other sites More sharing options...
joshuaer Posted September 10, 2012 Author Share Posted September 10, 2012 Does this look correct to how you where describing to do it justsomeguy? Thanks <?phpini_set('display_errors', 1);error_reporting(E_ALL);include("sql.php"); //Connect to SQLinclude("auth.php"); //Connect to SQL$UID= $_SESSION['UID'];$headers = 'test';$d = date ("D");$result = mysql_query("SELECT * FROM users where UID='$UID' AND list != ''");$sel = mysql_query("SELECT * FROM users where UID='$UID' AND email != ''");$mail_hrd = "From: test list;"; $txt = "your test list - ";if(mysql_num_rows($result) > 0){$count = 0;while ($rows = mysql_fetch_array ($sel, MYSQL_ASSOC)){$to = $rows['email'];while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)){$message = $txt . $row['list'].' ' . $row['type'];}}mail($to, $headers, $message, $mail_hrd);$count++;echo "myResult=$count Emails Sent. Done.";print $to;echo "$to";print $message;//print $day;}else{echo "myResult=Email Submissions Failed.";}?> Link to comment Share on other sites More sharing options...
justsomeguy Posted September 10, 2012 Share Posted September 10, 2012 That's only going to send one email, to whoever the last user in the database is. If you're only getting one user then it will only send to them. The loop where you build the message text doesn't keep every line, it keeps rebuilding $message to be $txt plus the current row. It doesn't save the previous rows. It should add each row to the same variable to send in the email. Link to comment Share on other sites More sharing options...
joshuaer Posted September 10, 2012 Author Share Posted September 10, 2012 How do I add each row to the same variable? Thanka again Link to comment Share on other sites More sharing options...
justsomeguy Posted September 10, 2012 Share Posted September 10, 2012 $txt .= $row['list'] . ' ' . $row['type']; Link to comment Share on other sites More sharing options...
joshuaer Posted September 11, 2012 Author Share Posted September 11, 2012 Thank you very much that worked great can I get one more thing, how can I get it to break after each line $txt .= ' '.$row['list'] . ' ' . $row['type'].$row['amount']; i tried /r/n but it did not break each line Link to comment Share on other sites More sharing options...
joshuaer Posted September 11, 2012 Author Share Posted September 11, 2012 ok discard that, got it workin $txt .= "\r\n".' '.$row['list'] . ' ' . $row['type'].$row['amount']; Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now