PHPNewbie55 Posted December 14, 2007 Share Posted December 14, 2007 OK.. I am trying to create a newsletter script that sends products from my database to users e-mail addy... Here is my problem:::: $to = "email@user.com"; $from = "email@user.com ( My Newsletter )"; $subject = "Weekly Newsletter - $date"; $message = " HOW DO I GET A WHILE STATEMENT TO RUN HERE "; $headers = "From: $from\r\n"; $headers .= "Content-type: text/html\r\n"; //options to send to cc+bcc //$headers .= "Cc: whoever@whereever.com"; //$headers .= "Bcc: whoever@whereever.com"; // send the email. mail($to, $subject, $message, $headers); I am trying to send multiple products from multiple categories but I can't figure out how to get a WHILE statement to run for the MEASSAGE portion of the e-mail... Any help would be appreciated.... Thanks In Advance!! Quote Link to comment Share on other sites More sharing options...
Yesideez Posted December 14, 2007 Share Posted December 14, 2007 First decide what stays the same (header? to email? subject string? message?) then define these variables. Next define a query to pull all the records from your database. Now run that query in a WHILE loop sending the email to everyone in your database. Quote Link to comment Share on other sites More sharing options...
corbin Posted December 14, 2007 Share Posted December 14, 2007 I would probably break it down into a function like: function SendTheEmail($username, $fname, $lname, $anyOtherInfoYouMightWant) { //do something here } Then just pull stuff from the database: //pretend mysql link is established $q = mysql_query("SELECT * FROM users"); while($r = mysql_fetch_assoc($q)) { SendTheEmail($r['username'], $r['fname'], $r['lname'], $r['somethingelse']); } SendTheEmail would contain the email text, and the variables would be plugged in.... Or, if you wanted a reusable function, you could make it where you also sent the text to the function. Quote Link to comment Share on other sites More sharing options...
PHPNewbie55 Posted December 14, 2007 Author Share Posted December 14, 2007 I think I get what you are saying.... I was thinking about making a function but my logic was way off... I'll try different ways and let you all know how it turned out and post the code too... Thanks... Quote Link to comment Share on other sites More sharing options...
PHPNewbie55 Posted December 15, 2007 Author Share Posted December 15, 2007 I ran into a problem... The problem is that the email addresses and the products are in two different tables... So I can run a WHILE loop for the emails... but I can't figure out how to run another WHILE loop to include the products in each email... Anyone have any ideas.....?? Quote Link to comment Share on other sites More sharing options...
Stooney Posted December 15, 2007 Share Posted December 15, 2007 are the products static? or are there different products per email? Quote Link to comment Share on other sites More sharing options...
PHPNewbie55 Posted December 15, 2007 Author Share Posted December 15, 2007 The products will be static... I was thinking of having the members choose the categories that they would like to see in their newsletters, but that seems like it might be impossible at this point. Quote Link to comment Share on other sites More sharing options...
PHPNewbie55 Posted December 15, 2007 Author Share Posted December 15, 2007 wooo hooo... I got it.... It works.... I'll post the code as soon as I get the bugs worked out... But I did get multiple products in one e-mail... The problem was I was combining the WHILE LOOPS and I didn't have to...... Back in a bit.... Quote Link to comment Share on other sites More sharing options...
PHPNewbie55 Posted December 15, 2007 Author Share Posted December 15, 2007 OK Here is my code..... Now I am a beginner so if anyone sees anything weird in my code OR see anything that doesn't need to be there.. OR a better way to do this... (PLEASE LET ME KNOW) <?PHP ////// MAKE A CONNECTION $mysqlserver = "localhost"; $mysqllogin = "login_name"; $mysqlpassword = "login_password"; $mysqldb = "database_name"; $conn = mysql_connect("$mysqlserver", "$mysqllogin", "$mysqlpassword"); // IF CONNECTION CANNOT BE MADE QUIT AND GIVE AN ERROR MESSAGE if (!$conn) { die('<h4>Could <u>Not</u> Connect To The Database</h4><hr>' . mysql_error()); } mysql_select_db("$mysqldb", $conn); ////////// query the products table $product_query = mysql_query("SELECT * FROM products_table ORDER BY title desc limit 10"); while($info = mysql_fetch_array($product_query)) { $message_body .= "THIS WILL BE YOUR HTML FOR EACH PRODUCT"; } // end the while($info = mysql_fetch_array($product_query)) loop ######################################### ## Now that you have all of your products chosen start looping through the e-mail table... ######################################### $query = mysql_query("SELECT * FROM my_newsletter_table WHERE confirmed='Yes'"); while($row = mysql_fetch_array($query)) { /// to personalize each e-mail create a beginning and an ending for each... $message_begin = "<html><body><p><b>Hello ".$row['name']."</b>,<br>This is our weekly newsletter that you requested..</p>"; $message_end = "</body></html>"; // grab the e-mail address... $email = $row['email']; // create the date... $date = date("m-d-y"); // set up the outgoing e-mail... $to = "$email"; $from = "$email"; $subject = "My Newsletter for $date"; $headers = "From: $from ( My Brand New Newsletter )\r\n"; $headers .= "Content-type: text/html\r\n"; // now set up the message to send combining the Begin - BODY - End $message .= "$message_begin $message_body $message_end"; // now send it... mail($to, $subject, $message, $headers); } // end while($row = mysql_fetch_array($query)) // you are finished... messages sent.... ?> Thanks.. and any tips would be appreciated Anything that will help me learn MORE...... Teaching myself sucks... I have one book.. that I rarely open.. and the rest is trial and error... and also a lot of looking around at other peoples codes to see how they put stuff together... so it is a time consuming process.. but I am learning!! ---- One Question... How do you get your code to be COLORED like it is in some posts..?? Quote Link to comment Share on other sites More sharing options...
PHPNewbie55 Posted December 15, 2007 Author Share Posted December 15, 2007 OOOOOps.... I just tested that code with multiple e-mail addresses...... It sends one e-mail for every address that is in the table.. The first e-mail is perfect.. The second e-mail contains both the first and second e-mail address information along with two sets of products... The third e-mail contains all three sets of email info and three sets of products... Weird........ since the send mail is inside a WHILE loop I thought it would send each individual e-mail with only one set of MESSAGE_BODY..... Quote Link to comment Share on other sites More sharing options...
PHPNewbie55 Posted December 15, 2007 Author Share Posted December 15, 2007 Ah Ha..... I found the error.... This part...... $message .= "$message_begin $message_body $message_end"; Should be...... $message = "$message_begin $message_body $message_end"; Without the period before the equals sign.... "I really don't even know what that indicates.. if someone would care to shed some light on that.. it would be cool..." I have tested it several times now and it does work..... So here is the code... <?PHP ////// MAKE A CONNECTION $mysqlserver = "localhost"; $mysqllogin = "login_name"; $mysqlpassword = "login_password"; $mysqldb = "database_name"; $conn = mysql_connect("$mysqlserver", "$mysqllogin", "$mysqlpassword"); // IF CONNECTION CANNOT BE MADE QUIT AND GIVE AN ERROR MESSAGE if (!$conn) { die('<h4>Could <u>Not</u> Connect To The Database</h4><hr>' . mysql_error()); } mysql_select_db("$mysqldb", $conn); ////////// query the products table $product_query = mysql_query("SELECT * FROM products_table ORDER BY title desc limit 10"); while($info = mysql_fetch_array($product_query)) { $message_body .= "THIS WILL BE YOUR HTML FOR EACH PRODUCT"; } // end the while($info = mysql_fetch_array($product_query)) loop ######################################### ## Now that you have all of your products chosen start looping through the e-mail table... ######################################### $query = mysql_query("SELECT * FROM my_newsletter_table WHERE confirmed='Yes'"); while($row = mysql_fetch_array($query)) { /// to personalize each e-mail create a beginning and an ending for each... $message_begin = "<html><body><p><b>Hello ".$row['name']."</b>,<br>This is our weekly newsletter that you requested..</p>"; $message_end = "</body></html>"; // grab the e-mail address... $email = $row['email']; // create the date... $date = date("m-d-y"); // set up the outgoing e-mail... $to = "$email"; $from = "$email"; $subject = "My Newsletter for $date"; $headers = "From: $from ( My Brand New Newsletter )\r\n"; $headers .= "Content-type: text/html\r\n"; // now set up the message to send combining the Begin - BODY - End $message = "$message_begin $message_body $message_end"; // now send it... mail($to, $subject, $message, $headers); } // end while($row = mysql_fetch_array($query)) // you are finished... messages sent.... ?> Quote Link to comment Share on other sites More sharing options...
PHPNewbie55 Posted December 15, 2007 Author Share Posted December 15, 2007 Dang... put that in a QUOTE instead of a CODE... sorry Just found the edit button... didn't see it before!! LOL Quote Link to comment Share on other sites More sharing options...
Northern Flame Posted December 15, 2007 Share Posted December 15, 2007 ---- One Question... How do you get your code to be COLORED like it is in some posts..?? I found that when you use PHP instead of php it doesnt get colored, example: <?PHP $not = 'This is a non-colored code on PHP FREAKS'; echo $not; ?> <?php $yes = 'This is a colored code on PHP FREAKS'; echo $yes; /* See the difference? */ ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.