Sunday, February 22, 2015

How to send large number(10000+) of emails using Php/MySql?

We can do this in two ways:
  1. With Cron
  2. With ajax
1. With cron
  1. Create new table for emails with required fields
  2. Insert all the email ids into table with status as 'not sent'
  3. Write a php function to fetch set of email ids(Ex: get 100 ids of status='not sent' at a time) and send email to them using mail() method. After sending the mail update status to "sent"
  4. Write cron job to run this method every 5 min until all mails sent.
2. With ajax(through browser)
Most of the times your request will be timed out, if you are trying to send large no.of emails through browser. Instead of sending all mails at a time, we can send using pagination concept. So you don't need to worry about request timeouts.
  1. Create Empty div with id
  2. Get all mail ids using ajax and save in javascript variable
  3. Consider sending 100 mails at a time and create no. of pages it will create
    var totalmails = 12000;
    var pagecount = Math.ceil(totalmails/100);
  4. Depends on no.of pages, create select dropdown with page numbers
    var pages = "<select name='pages'>";
    for(var i = 0; i <= pagecount; i ++){
      pages += "<option value='"+i+"'>"+i+"</option>";
    pages += "</select>";
  5. On selection of each page number, split the array and make the ajax call with those mail ids and send mail using php mail() method.
  6. Thats it!

No comments:

Post a Comment