Jump to content

Sendmail Help


joshuaer

Recommended Posts

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

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

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

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

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

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

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...