Welcome Guest!
Please login
If you do not have an account yet on The Web Squeeze forums, please Register! It’s FREE and there are many benefits:
- Receive Fast Advice
- Learn Programming Languages
- Get Professional Website Reviews
- Quick Troubleshooting Assistance
|
|
To Loop Or Not To Loop A Mail Process
This is a discussion on To Loop Or Not To Loop A Mail Process, within the MySQL section. This forum and the thread "To Loop Or Not To Loop A Mail Process" are both part of the Programming Your Website category.
![]() ![]() |
Oct 29 2008, 11:20 PM
Post
#1
|
|
![]() Rapid Squeezer ![]() ![]() ![]() ![]() Posts: 146 Joined: 3-July 08 |
So I have a database of some 40,000 bookings we've had over the years.
We do targetted mailouts to these people. As you might be aware, sending mail to this many people can take some time so working out the optimal method would be nice. With the mail() function, currently what I'll do is create a mysql loop CODE $sql = 'select * from table group by email'; $res = mysql_query($sql); while($row = mysql_fetch_array($res)) { // then something like so mail($row['name'] . '<' . $row['email'] . '>', 'blah subject', $msg); } This will go through and then do the mail() command for each person. What I'm wondering however is if it would be faster (I have yet to experiment) and better practice to instead collate all the names and email addresses and initiate just one instance of the mail() function. Kind like so CODE $sql = 'select * from table group by email'; $res = mysql_query($sql); while($row = mysql_fetch_array($res)) { $nameEmail .= $row['name'] . '<' . $row['email'] . '>, '; } // then something like so mail($nameEmail, 'blah subject', $msg); What would you say is better practice? This post has been edited by cosmicbdog: Oct 29 2008, 11:21 PM |
|
|
Oct 29 2008, 11:38 PM
Post
#2
|
|
![]() Squeeze Machine ![]() Posts: 508 Joined: 7-October 08 From: Australia |
Some additional things to consider here:
1) Personalisation for your customers - Looks a bit less personal if the users can see that they're not the only ones getting the email... which leads to the next point... 2) Privacy for your customers - If you DO decide to go with this option, you should consider at least using BCC rather than TO, so you're not just distributing all the email addresses in your database to everyone on your database! I do however think it's a more efficient method than the first one... just pros and cons I guess -------------------- The more you visit, the more I'll post: http://japheththomson.com/
|
|
|
Oct 29 2008, 11:48 PM
Post
#3
|
|
![]() Squeeze Machine ![]() Posts: 766 Joined: 13-February 08 From: Catching the squeezed drips downunder. |
Well, your second option would give every user every other user's email address so I guess that this is not what you're after
Usually such a large processing of emails can be broken down into chunks so that you don't have to do it all at once -- through use of a cron job that runs a php script (I'm not really up with how you'd set the cron job up but I'm sure Japh can help you).. The script could process say 500 at a time and store the current number in a file for the next time it is run. CODE <?php $start_num = mysql_real_escape_string(file_get_contents('email_number.txt')); $num_per_exec = 500; // If this is the first time this script is run, count the records in the database // so we know when to stop (preg match checks to macke sure retrieved value is xx/xx (meaning thismanysent/thismanytotal) if (!$start_num || !preg_match('/^\d+\/\d+/', $start_num)) { $start_num = 0; $sql = "SELECT COUNT(*) as num FROM table group by email"; $result = mysql_query($sql); $num = mysql_fetch_row($result); $num = $num['num']; // total number } else { // Explode into (number to start from and total list($start_num, $num) = explode('/', $start_num); } $sql = 'select * from table group by email LIMIT ' . $start_num . ', ' . $num_per_exec; $res = mysql_query($sql); while($row = mysql_fetch_array($res)) { // then something like so mail($row['name'] . '<' . $row['email'] . '>', 'blah subject', $msg); // Now recalculate where we're up to $start_num += $num_per_exec; if ($start_num > $num) { shell_exec('something in here to delete the cron job you set up); } Still good so write to the file file_put_contents('email_nu,mber.txt', $start_num . '/' . $num); } ?> Something like that would send 500 emails at a time until the there are no more to send where it deletes the cron job. But to answer your question, you would want to go with the first method as the second will expose email addresses to everyone. EDIT : Took so long to reply Japh already put across the most important point of my post. -------------------- |
|
|
Oct 29 2008, 11:53 PM
Post
#4
|
|
![]() Squeeze Machine ![]() Posts: 508 Joined: 7-October 08 From: Australia |
Yeah, absolutely. I'd use a cron job. I even toyed with the idea of doing that very thing with ajax, just to see how that might work... but never bothered to actually try it yet (like so many other half-ideas that coders block has kicked to the curb)
-------------------- The more you visit, the more I'll post: http://japheththomson.com/
|
|
|
Oct 30 2008, 12:26 AM
Post
#5
|
|
![]() Rapid Squeezer ![]() ![]() ![]() ![]() Posts: 146 Joined: 3-July 08 |
Ahh yes. I forgot about the whole sharing of email thing. I remember thats why I originally went with the method of looping and sending to each email individually. BCC's do get picked up as spam too don't they.
I've wondered as well about using a cron to do this... sent to an ajax window so its instant on clicking 'send' and it then doesn't block up the whole browser while it waits for a 30 minute script to run. I think its less likely things can go sour by sending to 500 people chunks. We do personalise each email as well with names in the copy etc. I don't know what I was thinking now thinking that option 2 could possibly work because it leaves no room for configuring each email. I still wonder if there might be a better way. I'm sure mail() has got some dodgy things going on and probably isn't the best function for doing mass emailing. Thats a sweet script rakuli. I've been fantasising about doing something like that for some time. I've yet to make the leap of creating a cron job via a php command although we run many cron jobs. Thanks heaps guys. |
|
|
Oct 30 2008, 01:26 AM
Post
#6
|
|
![]() Squeeze Machine ![]() Posts: 766 Joined: 13-February 08 From: Catching the squeezed drips downunder. |
Well servers will be banning PHP's use of the mail() function at some point in the furture due to abuse by spammers.
I suggest you take a look at the PHP mail package from PEAR
Reason for edit: Was in coding mode, changed <a href to [url=
-------------------- |
|
|
Oct 30 2008, 01:34 AM
Post
#7
|
|
![]() Squeeze Machine ![]() Posts: 508 Joined: 7-October 08 From: Australia |
Yeah, this package in particular, and using SMTP to send emails: PEAR :: Package :: Mail
-------------------- The more you visit, the more I'll post: http://japheththomson.com/
|
|
|
Nov 1 2008, 11:52 AM
Post
#8
|
|
![]() Rapid Squeezer ![]() Posts: 239 Joined: 14-February 08 From: NY, USA |
Hey guys,
I've been down this road a couple times and I would highly suggest using a 3rd party to manage your email campaigns. There are so many different advantages to this including not having to write any additional code, built-in double opt-in, statistics and no risk of having your server blacklisted. Rich -------------------- |
|
|
If you found The Web Squeeze to be helpful, please donate so we can keep this site FREE, FRESH, and fortified with Web Design & Development info!
![]() ![]() |
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:
Similar Topics
| Topic Title | Replies | Topic Starter | Views | Last Action | |||
|---|---|---|---|---|---|---|---|
![]() |
9 | caminowebmaster | 241 | 15th February 2008 - 06:24 PM Last post by: Jacob |
|||
![]() |
3 | c010depunkk | 452 | 21st February 2008 - 09:41 AM Last post by: c010depunkk |
|||
![]() |
9 | thesealportalteam | 329 | 17th March 2008 - 12:24 PM Last post by: thesealportalteam |
|||
![]() |
1 | graisbeck | 253 | 8th April 2008 - 09:45 AM Last post by: JustinStudios |
|||
![]() |
3 | mv08jml | 181 | 27th June 2008 - 09:19 AM Last post by: Craigboy87 |
|||






Oct 29 2008, 11:20 PM












