Page 1 of 1

Newsletter Script Help: How to send 2000+ emails safely?

Posted: Thu Dec 25, 2008 7:48 pm
by impfut
Hi all,

I'm a newbie to PHP and it's a lot to take in but I'm really enjoying it so far. It opens up so many possibilities compared to a html only site. I have managed to put together a simple email script which is tested and working. The thing is, I'm currently using my test table which has 3 email addresses in it. The real table has over 2000 members. I've never emailed them in one go before but when I do, I want to make sure the script is going to work! The last thing I want is for it to crash or time out and some members get one email, some get two and some don't get anything. I have read that I may have a problem with using mail() for large lists.

Should I modify my current script? If so, in what way? I'm really dumb at PHP at the moment so please be gentle. Any help would be much appreciated!


Current Script

Code: Select all

 <?php
include("../../opendb.php");
mysql_select_db($dbname1);
 
$mailtable = $_POST['table'];
$mailsubject = $_POST['subject'];
$mailmessage = stripslashes($_POST['message']);
 
$sql="SELECT * FROM $table";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
$name = $rows['name'];
$to = $rows['email'];
$subject = "$mailsubject";
$header = "from: Me <me@domain.com>";
$message = "Dear $name \r\n";
$message.= " \r\n";
$message.= "$mailmessage \r\n";
$message.=" \r\n";
$message.="Warmest Regards \r\n";
$message.="Me \r\n";
$sentmail=mail($to,$subject,$message,$header);
}
if($sentmail){
header("location:http://www.domain.com/success.html");
}
else {
header("location:http://www.domain.com/failure.html");
}
include("../../closedb.php");
?>

Re: Newsletter Script Help: How to send 2000+ emails safely?

Posted: Fri Dec 26, 2008 4:19 am
by impfut
Just for the record, I'm not a spammer, the people I'm emailing know my website. Am I asking the wrong questions? Is my code so bad no one can face getting involved lol. Please help if you can. Even if it's to tell me my script is carp and I should stick with html hehe.

If I wanted to mail in batches, what would I need to do? Say for example I wanted to take 100 at a time, what would I need to change in my code? Thanks for any help guys.

Re: Newsletter Script Help: How to send 2000+ emails safely?

Posted: Fri Dec 26, 2008 12:37 pm
by icesolid
Are you having a problem?

I don't see a problem with your code above.

Re: Newsletter Script Help: How to send 2000+ emails safely?

Posted: Fri Dec 26, 2008 4:50 pm
by impfut
icesolid wrote:Are you having a problem?

I don't see a problem with your code above.
Code is working fine. The problem is will it be fine for sending 2000+ emails 8O

Re: Newsletter Script Help: How to send 2000+ emails safely?

Posted: Fri Dec 26, 2008 6:15 pm
by John Cartwright
That is entirely dependant on your server, and it's defined limits.

Generally when sending large amounts of email, it is better to break it up into around chunks of 100 every minute (or few) using a cron job.

I would recommend you take a look at http://swiftmailer.org (which was created and is supported by a member here -- Chris Corbyn) instead the the regular mail() function. Night and day.

Re: Newsletter Script Help: How to send 2000+ emails safely?

Posted: Sat Dec 27, 2008 12:17 pm
by cptnwinky
the mail() function should easily be able to handle hundreds of emails at a time but you may want to create your script so that the emails are sent out in batches so that you don't run into the problem of an error screwing up your whole operation.

In theory this is pretty easy. You will need an offset (where to start from after the last batch stops) that can easily be a $_GET variable.

In psuedo code:

Code: Select all

 
if $offset is not set
    start from beginning
    grab email addy from db using LIMIT 0,30 (or whatever number you want to process at once)
    loop through emails
        send (checking for errors)
        if there is an error don't let the script die (in other words a custom error handler)
            take the current offset number (say $i) and redirect the script to start from where it left off
    send redirect header to self with $offset as $_GET variable
if $offset is set
    grab email addy from db using LIMIT $offset (in this case from the previous operation it could be 31,60)
    repeat as above
 

Re: Newsletter Script Help: How to send 2000+ emails safely?

Posted: Sun Dec 28, 2008 9:55 am
by John Cartwright
cptnwinky wrote:the mail() function should easily be able to handle hundreds of emails at a time but you may want to create your script so that the emails are sent out in batches so that you don't run into the problem of an error screwing up your whole operation.

In theory this is pretty easy. You will need an offset (where to start from after the last batch stops) that can easily be a $_GET variable.

In psuedo code:

Code: Select all

 
if $offset is not set
    start from beginning
    grab email addy from db using LIMIT 0,30 (or whatever number you want to process at once)
    loop through emails
        send (checking for errors)
        if there is an error don't let the script die (in other words a custom error handler)
            take the current offset number (say $i) and redirect the script to start from where it left off
    send redirect header to self with $offset as $_GET variable
if $offset is set
    grab email addy from db using LIMIT $offset (in this case from the previous operation it could be 31,60)
    repeat as above
 
The problem isn't with PHP being able to handle the calls. The problem is with the mail server being overloaded or exceeding it's limit of sending mail.

And as a side note, there is little point redirecting back to itself when a simple loop would suffice. In this case, it doesn't matter since it doesn't fix the underlying issues.

Re: Newsletter Script Help: How to send 2000+ emails safely?

Posted: Sun Dec 28, 2008 10:17 am
by cptnwinky
PHP's mail function does not use a mail server it uses the linux sendmail script. What I meant was essentially that the sendmail script should be able to handle a few hundred emails per minute or so.

The point of redirecting back to itself and not just using a loop is to avoid running up against php's maximum execution time. Sure, the person could set ridiculously high limits in php.ini but then if there is an error in the script it could spiral out of control taking down the whole server and you wouldn't have a clue because you have no idea if the script is still running or if the loop becomes infinite. Doing it in small batches like this helps decrease the likelyhood of duplicate emails due to script failure since on failure of sending the emails the script can handle it gracefully, redirect back to itself starting with the email addy that failed last time or even put it in a log and continue on to the next one.