tjhilder Posted December 29, 2005 Share Posted December 29, 2005 Hi, I was wondering what the best way to do a send to all users e-mail? I figured a loop might work, but I figured I'd ask the experienced to see what they do. any information is most welcome Quote Link to comment https://forums.phpfreaks.com/topic/3118-best-way-to-have-a-send-to-all-members-mail/ Share on other sites More sharing options...
LazyJones Posted December 29, 2005 Share Posted December 29, 2005 [!--quoteo(post=331350:date=Dec 29 2005, 06:32 PM:name=tjhilder)--][div class=\'quotetop\']QUOTE(tjhilder @ Dec 29 2005, 06:32 PM) 331350[/snapback][/div][div class=\'quotemain\'][!--quotec--] Hi, I was wondering what the best way to do a send to all users e-mail? I figured a loop might work, but I figured I'd ask the experienced to see what they do. any information is most welcome I'd go for a loop. Quote Link to comment https://forums.phpfreaks.com/topic/3118-best-way-to-have-a-send-to-all-members-mail/#findComment-10475 Share on other sites More sharing options...
AndyB Posted December 30, 2005 Share Posted December 30, 2005 I'd go for a loop as well .... but every five emails or so I'd add a brief sleep() so nothing overloaded. Quote Link to comment https://forums.phpfreaks.com/topic/3118-best-way-to-have-a-send-to-all-members-mail/#findComment-10476 Share on other sites More sharing options...
tjhilder Posted December 30, 2005 Author Share Posted December 30, 2005 I'm not entirely sure how I would put a loop into this code. if (isset ($_POST['submit'])) { // Handle the form. // Define the query. $query = "SELECT username, email FROM members WHERE username='{$_POST['username']}'"; $result = mysql_query ($query); if (mysql_affected_rows() == 1) { // If it ran OK. $row = mysql_fetch_array ($result); // Retrieve the information. // Send the email. $body = "\n\nThe following message has been sent by ".$_POST['sentby']." at www.tjhilder.co.uk/index.php\n\n----------------------------------------------\n\n"; $body .= stripslashes($_POST['message']); $body .= "\n\n----------------------------------------------\n\nIf you believe you got this e-mail by mistake, please ignore this message.\n\nPlease DO NOT reply to this message.\n"; mail($row['email'], $_POST['title'], $body, 'From: tjhilder.co.uk <admin@tjhilder.co.uk>'); // Finish the page. $information = "<br /><br /><span style=\"color : green; font-weight : bold;\">Your E-mail has been Sent!</span>"; } else { // If it did not run OK. $information = "<br /><br /><span style=\"color : red; font-weight : bold;\">Your E-mail could not be sent.</span><br /><br /><b>" . mysql_error() . "</b>. The query was $query."; } } Quote Link to comment https://forums.phpfreaks.com/topic/3118-best-way-to-have-a-send-to-all-members-mail/#findComment-10480 Share on other sites More sharing options...
fenway Posted December 30, 2005 Share Posted December 30, 2005 Two things: first, you're supposed to use mysql_num_rows() to get the rows returned from a SELECT query, not mysql_affected_rows(), which is for INSERT/UPDATE/DELETE statement. Second, you're not sending an e-mail to all members, just the one. So there's no need for any loops. Quote Link to comment https://forums.phpfreaks.com/topic/3118-best-way-to-have-a-send-to-all-members-mail/#findComment-10490 Share on other sites More sharing options...
LazyJones Posted December 30, 2005 Share Posted December 30, 2005 [!--quoteo(post=331636:date=Dec 30 2005, 04:34 PM:name=fenway)--][div class=\'quotetop\']QUOTE(fenway @ Dec 30 2005, 04:34 PM) 331636[/snapback][/div][div class=\'quotemain\'][!--quotec--] Two things: first, you're supposed to use mysql_num_rows() to get the rows returned from a SELECT query, not mysql_affected_rows(), which is for INSERT/UPDATE/DELETE statement. Second, you're not sending an e-mail to all members, just the one. So there's no need for any loops. Very much true. And if you want to send mail to all users, get rid of the WHERE statement and go though the values in a loop (and send the mails) Quote Link to comment https://forums.phpfreaks.com/topic/3118-best-way-to-have-a-send-to-all-members-mail/#findComment-10491 Share on other sites More sharing options...
tjhilder Posted December 31, 2005 Author Share Posted December 31, 2005 Sorry I meant to remove the mysql_affected_rows() but I forgot cos I was using the mail code from my register code. Ok so I changed it to this: (don't know if it's correct.) // Define the query. $send_all = "SELECT username, email FROM members"; if ($result = mysql_query ($send_all)) { // Run the query. $row = mysql_num_rows($result); // Retrieve the information. // Send the email. $body = "\n\nThe following message has been sent by ".$_POST['sentby']." at www.tjhilder.co.uk/index.php\n\n----------------------------------------------\n\n"; $body .= stripslashes($_POST['message']); $body .= "\n\n----------------------------------------------\n\nIf you believe you got this e-mail by mistake, please ignore this message.\n\nPlease DO NOT reply to this message.\n"; mail($row['email'], $_POST['title'], $body, 'From: tjhilder.co.uk <admin@tjhilder.co.uk>'); $information = "<br /><br /><span style=\"color : green; font-weight : bold;\">Your E-mail has been Sent!</span>"; } else { // Query didn't run. $information = "<p>Could not send E-mail because: <b>" . mysql_error() . "</b>. The query was $send_all."; } // End of query IF. does it still need a loop? i'm still not sure how I would add it if I do need it. thanks in advance. -- TJ Quote Link to comment https://forums.phpfreaks.com/topic/3118-best-way-to-have-a-send-to-all-members-mail/#findComment-10507 Share on other sites More sharing options...
fenway Posted December 31, 2005 Share Posted December 31, 2005 Yes, it still needs a loop, and your $row is incorrect now -- it's currently the number of rows (!), not each record as you loop through it. Quote Link to comment https://forums.phpfreaks.com/topic/3118-best-way-to-have-a-send-to-all-members-mail/#findComment-10512 Share on other sites More sharing options...
tjhilder Posted January 1, 2006 Author Share Posted January 1, 2006 [!--quoteo(post=331899:date=Dec 31 2005, 03:28 PM:name=fenway)--][div class=\'quotetop\']QUOTE(fenway @ Dec 31 2005, 03:28 PM) 331899[/snapback][/div][div class=\'quotemain\'][!--quotec--] Yes, it still needs a loop, and your $row is incorrect now -- it's currently the number of rows (!), not each record as you loop through it. Sorry I wasn't with it the other day lol in the form now is: <input type="hidden" name="email_id[]" /> and in the PHP/MySQL part is: if (isset ($_POST['submit'])) // Handle the form. { foreach ($_POST['email_id'] as $email) { // Define the query. $send_all = "SELECT username, email FROM members"; if ($result = mysql_query ($send_all)) { // Run the query. $row = mysql_fetch_array ($result); // Retrieve the information. // Send the email. $body = "\n\nThe following message has been sent by ".$_POST['sentby']." at www.tjhilder.co.uk/index.php\n\n----------------------------------------------\n\n"; $body .= stripslashes($_POST['message']); $body .= "\n\n----------------------------------------------\n\nIf you believe you got this e-mail by mistake, please ignore this message.\n\nPlease DO NOT reply to this message.\n"; mail($row['email'], $_POST['title'], $body, 'From: tjhilder.co.uk <admin@tjhilder.co.uk>'); $information = "<br /><br /><span style=\"color : green; font-weight : bold;\">Your E-mail has been Sent!</span>"; } else { // Query didn't run. $information = "<br /><br />Could not send E-mail because: <b>" . mysql_error() . "</b>. The query was $send_all."; } // End of query IF. } } } think it looks alright, just wanted to be 100% sure before I test it,.. if it goes wrong I may end up annoying alot of people at once Quote Link to comment https://forums.phpfreaks.com/topic/3118-best-way-to-have-a-send-to-all-members-mail/#findComment-10520 Share on other sites More sharing options...
fenway Posted January 1, 2006 Share Posted January 1, 2006 First, I'm not sure what you're actually using email_id for. Second, because you have an if() instead of a while() when you're calling mysql_fetch_array(), you're only going through the result set once -- that is, you're just sending an e-mail to the first record's e-mail. Third, even if you were to put this in a while() loop, you still need make sure you don't overload your mail server, so you'll need to throttle your mail() calls. Quote Link to comment https://forums.phpfreaks.com/topic/3118-best-way-to-have-a-send-to-all-members-mail/#findComment-10523 Share on other sites More sharing options...
tjhilder Posted January 1, 2006 Author Share Posted January 1, 2006 [!--quoteo(post=332132:date=Jan 1 2006, 06:19 PM:name=fenway)--][div class=\'quotetop\']QUOTE(fenway @ Jan 1 2006, 06:19 PM) 332132[/snapback][/div][div class=\'quotemain\'][!--quotec--] First, I'm not sure what you're actually using email_id for. I was using same principle as what someone else showed me when looping to delete multiple fields, dunno if it does the same here. Second, because you have an if() instead of a while() when you're calling mysql_fetch_array(), you're only going through the result set once -- that is, you're just sending an e-mail to the first record's e-mail. Oops, I'm glad you noticed that cos I didn't lol. Third, even if you were to put this in a while() loop, you still need make sure you don't overload your mail server, so you'll need to throttle your mail() calls. i'm not entirely sure how I would do that, any chance you could explain? no worries if you can't, just would help alot. Quote Link to comment https://forums.phpfreaks.com/topic/3118-best-way-to-have-a-send-to-all-members-mail/#findComment-10525 Share on other sites More sharing options...
fenway Posted January 1, 2006 Share Posted January 1, 2006 For deleting, it would make sense, since you would need the UIDs for each record you're about to delete; but in this case, since you're just retrieving all of the records anyway, it's not necessary at all, so you can drop that foreach() loop, and the hidden field as well (unless it's the only field, of course). The truth is that I really don't know how your mail() function works. In the simplest form, you could simply keep a counter, and sleep the script every few hundred e-mails. However, I'm not sure if you'd be holding the user "hostage" during the process. In fact, I don't actually know this works in PHP -- I'm used to Perl, which I can print a location heading, and keep the script running after that, so the user doesn't need to wait until my script is actually complete. Maybe someone else can shed some light on this subject? Moreover, if you're not personalizing the e-mails, then perhaps you can use a "blanket" TO field and then include ~50 members in the BCC field -- this really depends on your mail server config. The idea is that you don't want to hog your mail server, and furthermore, you don't want to send out a lot of messages in a short period of time, because your server could get blacklisted for some time, which is very bad. It's much better to keep a constant amount of mail in the queue -- also helps with load balancing of different services on the server, too. Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/3118-best-way-to-have-a-send-to-all-members-mail/#findComment-10526 Share on other sites More sharing options...
thegraingers Posted January 3, 2006 Share Posted January 3, 2006 [!--quoteo(post=332047:date=Jan 1 2006, 10:16 AM:name=tjhilder)--][div class=\'quotetop\']QUOTE(tjhilder @ Jan 1 2006, 10:16 AM) 332047[/snapback][/div][div class=\'quotemain\'][!--quotec--] Sorry I wasn't with it the other day lol in the form now is: <input type="hidden" name="email_id[]" /> and in the PHP/MySQL part is: if (isset ($_POST['submit'])) // Handle the form. { foreach ($_POST['email_id'] as $email) { // Define the query. $send_all = "SELECT username, email FROM members"; if ($result = mysql_query ($send_all)) { // Run the query. $row = mysql_fetch_array ($result); // Retrieve the information. // Send the email. $body = "\n\nThe following message has been sent by ".$_POST['sentby']." at www.tjhilder.co.uk/index.php\n\n----------------------------------------------\n\n"; $body .= stripslashes($_POST['message']); $body .= "\n\n----------------------------------------------\n\nIf you believe you got this e-mail by mistake, please ignore this message.\n\nPlease DO NOT reply to this message.\n"; mail($row['email'], $_POST['title'], $body, 'From: tjhilder.co.uk <admin@tjhilder.co.uk>'); $information = "<br /><br /><span style=\"color : green; font-weight : bold;\">Your E-mail has been Sent!</span>"; } else { // Query didn't run. $information = "<br /><br />Could not send E-mail because: <b>" . mysql_error() . "</b>. The query was $send_all."; } // End of query IF. } } } think it looks alright, just wanted to be 100% sure before I test it,.. if it goes wrong I may end up annoying alot of people at once If your worried about the debuging stage then you can either put a limit on your select statement so that your tests only send out a few emails and / or set up some dummy emails accounts to your email address and adjust your sql query so those are all it finds and / or comment out the actual email statement and just substitute a display statement containing the email address so you can check your sql is giving you the expected data Quote Link to comment https://forums.phpfreaks.com/topic/3118-best-way-to-have-a-send-to-all-members-mail/#findComment-10576 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.