Quote:
Originally Posted by Shoe Lace
When you say message marketing system does this involve sending newsletter style e-mails out?
If so, do you have things setup to handle incomplete blasts? It's an edge case but really important for such a tool. You don't want to send out duplicate e-mails if you restart the process.
Yeah, it's for newsletter style emails. I have something in place to help that. Well...
kind of. But not really. It's PHP (of course), and the script that actually sends blasts has this at the top:
[PHP]ignore_user_abort(true);
set_time_limit(0);[/PHP]
If a user closes their browser mid-blast, the script will continue executing. Also, if there are a ton of emails, it won't timeout like scripts ordinarily would do after like 30 seconds or whatever the default is. Hopefully that will handle a lot of the issue.
At the moment, it records each individual email that's sent, but it doesn't insert into the database each time. A database query for every single e-mail sent would be very slow for lots of emails. For 1000 people, each blast would require 1000 separate DB queries, minimum.
That didn't seem like a good idea, so I'm doing multiple inserts within single queries like:
INSERT INTO msg_history (a, b) VALUES (x, y), (c, d), (d, e)
I did a bit of reading, and there are varying limits on the number of records that can be inserted in a single statement. Since a very large insert statement could also be slow/resource intensive, I came up with something like this (trimmed for space):
[PHP]$query = "INSERT INTO msg_history (user_id, email_id) VALUES ";
$total = 0; // for counting to 100 DB inserts
for ($i=0; $i<$num_recipients; $i++) { // e-mail blast loop
$mail->Send(); // prepare and send email
// add to insert query, increment counter
$query .= "($user_id, $email_id),";
$total++;
if ($total >= 100) {
$query = substr($query, 0, -1); // remove last comma
mysqli_query($link, $query);
// create new query and reset counter
$query = "INSERT INTO msg_history (user_id, email_id) VALUES ";
$total = 0;
}
}
if ($total > 0) {
// insert remaining e-mails
}
[/PHP]
This way, the total # of DB queries are reduced, and there are never more than 100 records inserted at once, so we'll be safely within the limits (which vary from server to server).
I'm assuming the only way to accurately handle incomplete blasts would be to insert each e-mail separately though, right?
Last edited by sdturner02; 02-10-2013 at 10:43 AM.