Jump to content

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


impfut

Recommended Posts

Hi all, this forum proved to be awesome for helping me with my previous query so I'm hopeful it can do so again. 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.

 

Anyway... 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

<?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");
?>

Link to comment
Share on other sites

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.

 

Those are the least to be worried about (assuming you have a good server). The thing to be worried about is being blacklisted as a spammer. I would check with your hosting provider and see what rules they have for sending that many emails at once.

 

Keep in mind that processing that many emails is actually "not much work"* for a server and would not take a huge amount of time (never tested this theory on this large of a list), however I have sent out many hundred at once (about 600) and did not have a problem and it did not take very long to do it either.

 

 

So, that being said take a look at http://www.phplist.com/, it is a very robust newsletter system that has many features, but here are a few VERY important ones...

 

  • Batch Processing is useful in shared hosting environments. Set the maximum number of sent messages in a given time period.
  • Throttling can limit the load on your server so it doesn't overload.
  • Domain Throttling limits the number of emails to specific domains to keep on the friendly side of their system administrators

 

* I have not tested the server load on this size of list so I may be full of shit.

 

those are the things I would worry about in the grand scheme of things. From what I understand, getting off of blacklists is not an easy thing to do. Also be aware of anti-spam laws in your area (not sure if your in the USA or not). Even if there are no anti-spam laws, give an easy way to opt out for people. That is just courtesy as we all have gotten those mass-mailed emails with no way to opt-out. :)

 

Nate

Link to comment
Share on other sites

Chronister, thank you for taking the time to write a detailed reply. I have good hosting with a company called Media Temple. I am however on a shared server environment. I'll get in touch with them and see what they say about the situation. Just for the record, I'm not a spammer, the people I'm emailing know my website. It is very unlikely any of them will report me or mark the email as junk so how would I get black listed? Would it be my hosting company?

 

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.

 

 

Link to comment
Share on other sites

No, I was not implying that your a spammer.... I was just saying that a legitimate company, sending emails to folks who have subscribed can be easily blacklisted as a spammer simply because of the amount of emails that get sent at once.

 

It is possible that your hosting company could initiate a blacklist entry, it could be that if enough people "forget" that they subscribed and mark as spam then it could happen that way too.

 

To send out batches, you would basically place something like this in the code...

<?php
include("../../opendb.php");
mysql_select_db($dbname1);

$mailtable = $_POST['table'];
$mailsubject = $_POST['subject'];
$mailmessage = stripslashes($_POST['message']);
$numMailsToSend = 200; // set the number of messages we want to send in this batch.

$sql="SELECT * FROM $table WHERE mail_sent='0' LIMIT $numMailsToSend"; // added a where and limit clause
$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";

  if(mail($to,$subject,$message,$header)
  {
 // if the mail was sent, then update this members sent flag so we dont' send again.
   $query = "UPDATE table SET mail_sent='1' WHERE email = '$to'"
 $result = mysql_query($query);
  }
}
if($sentmail){
header("location:http://www.domain.com/success.html");
}
else {
header("location:http://www.domain.com/failure.html");
}
include("../../closedb.php");
?>

 

This the the basics of what you want to do... then you would make your SELECT code above to include something like WHERE mail_sent='0', and add a limit clause to only get the number of email addresses that you want to send.

 

Create a new field called mail_sent and if it is 1, then this batch has been sent to that user, it is 0, then the latest batch has not.

 

Once all the users are sent the email, then run a query to reset the mail_sent flag to 0, and your ready for the next round of emails when you wanna send em.

 

Hope this helps and makes sense....

 

Nate

Link to comment
Share on other sites

Nate, that is a fantastic reply!!! Thank you so much for taking the time to help me bud. I hope I can return the favor to others once I'm competent. I will give this a good crack tomorrow. Also, I'm sorry if it came across as me talking about the spam thing directly at you. It was just a general comment to all viewers who might have been concerned about helping a potential spammer.

 

Thank you again chap, I am really, REALLY thankful ;-)

 

Link to comment
Share on other sites

it only takes one person to report you for spam and you'll start having trouble with your host and website however reliable the website is. you should use subscription based software such as aweber as they require the the person to opt into your email list so its impossible for anyone to report spam legitimtely as they "opt in"

 

downside is you gotta pay a monthly fee

upside is you have greater control of your email campaign as well as being legit.

 

if aweber is not an option for you then I'd advise that you MUST have your readers opt in your list (by confirming their email address by clicking the link in the email address) and also have an unsubscribe link for all emails.

 

you'll be alright for sending the set of emails to notify your readers that they must opt in to your newsletter.

 

I think theres a script that can do this called phplist.com.... not sure

Link to comment
Share on other sites

Thank you to everyone for your comments. Based on the above I have modified my script. The DB table now has a column to say whether the person wants to receive the newsletter or not and there is also a column to say whether they have already been emailed. I am also now limiting the script to 100 emails at a time.

 

For anyone who is interested, here is the code. I am still open to improvements if anyone can spot any weakness ;-)

 

<?php
include("../../opendb.php");
mysql_select_db($dbname1);

$mailtable = $_POST['table'];
$mailsubject = $_POST['subject'];
$mailmessage = stripslashes($_POST['message']);
$maxsend = 100; 

$sql="SELECT * FROM $mailtable WHERE send='0' AND receive='1' LIMIT $maxsend";
$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.="Name \r\n";
$sentmail=mail($to,$subject,$message,$header);
if($sentmail){
$query = "UPDATE $mailtable SET send='1' WHERE email='$to'";
$result2 = mysql_query($query);
echo "Email sent to: $to</br>";
}
else {
$query2 = "UPDATE $mailtable SET send='0' WHERE email='$to'";
$result3 = mysql_query($query2);
echo "Error sending to: $to</br>";
}
}
include("../../closedb.php");
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.