Jump to content


Photo

best way to have a 'send to all members' mail?


  • Please log in to reply
12 replies to this topic

#1 tjhilder

tjhilder
  • Members
  • PipPipPip
  • Advanced Member
  • 136 posts
  • LocationKent, UK

Posted 29 December 2005 - 11:32 PM

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 :)

#2 LazyJones

LazyJones
  • Members
  • PipPipPip
  • Advanced Member
  • 78 posts

Posted 29 December 2005 - 11:39 PM

[!--quoteo(post=331350:date=Dec 29 2005, 06:32 PM:name=tjhilder)--][div class=\'quotetop\']QUOTE(tjhilder @ Dec 29 2005, 06:32 PM) View Post[/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 :)
[/quote]

I'd go for a loop.


#3 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 30 December 2005 - 01:15 AM

I'd go for a loop as well .... but every five emails or so I'd add a brief sleep() so nothing overloaded.
Legend has it that reading the manual never killed anyone.
My site

#4 tjhilder

tjhilder
  • Members
  • PipPipPip
  • Advanced Member
  • 136 posts
  • LocationKent, UK

Posted 30 December 2005 - 09:18 AM

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.";
            }
        }



#5 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 30 December 2005 - 09:34 PM

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.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#6 LazyJones

LazyJones
  • Members
  • PipPipPip
  • Advanced Member
  • 78 posts

Posted 30 December 2005 - 09:38 PM

[!--quoteo(post=331636:date=Dec 30 2005, 04:34 PM:name=fenway)--][div class=\'quotetop\']QUOTE(fenway @ Dec 30 2005, 04:34 PM) View Post[/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.
[/quote]

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)

#7 tjhilder

tjhilder
  • Members
  • PipPipPip
  • Advanced Member
  • 136 posts
  • LocationKent, UK

Posted 31 December 2005 - 08:53 AM

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

#8 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 31 December 2005 - 03:28 PM

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.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#9 tjhilder

tjhilder
  • Members
  • PipPipPip
  • Advanced Member
  • 136 posts
  • LocationKent, UK

Posted 01 January 2006 - 10:16 AM

[!--quoteo(post=331899:date=Dec 31 2005, 03:28 PM:name=fenway)--][div class=\'quotetop\']QUOTE(fenway @ Dec 31 2005, 03:28 PM) View Post[/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.
[/quote]
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 :P

#10 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 01 January 2006 - 06:19 PM

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.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#11 tjhilder

tjhilder
  • Members
  • PipPipPip
  • Advanced Member
  • 136 posts
  • LocationKent, UK

Posted 01 January 2006 - 08:35 PM

[!--quoteo(post=332132:date=Jan 1 2006, 06:19 PM:name=fenway)--][div class=\'quotetop\']QUOTE(fenway @ Jan 1 2006, 06:19 PM) View Post[/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]


#12 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 01 January 2006 - 08:59 PM

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.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#13 thegraingers

thegraingers
  • Members
  • Pip
  • Newbie
  • 9 posts
  • LocationUK

Posted 03 January 2006 - 11:20 PM

[!--quoteo(post=332047:date=Jan 1 2006, 10:16 AM:name=tjhilder)--][div class=\'quotetop\']QUOTE(tjhilder @ Jan 1 2006, 10:16 AM) View Post[/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 :P
[/quote]


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






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users