www.webdeveloper.com
Results 1 to 6 of 6

Thread: How to send emails to 190 people in my SQL database using PHP mail()

  1. #1
    Join Date
    Jul 2009
    Posts
    77

    Exclamation How to send emails to 190 people in my SQL database using PHP mail()

    Hello, I'm hoping someone can help me.

    I have a MYSQL Database with 190 records (emails/names/etc). I want to send an email to everyone in that table with a personalized greeting (Dear ______,).

    How do I loop through the records to pull out the names/emails?

    Thank you for any help
    Chris

  2. #2
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    Batch process 40-50 at a time, put the first as "to" and "bcc" the rest assuming no limitations from your host. Lower the number if excessive delays. This is best done via a scheduled cron job at a time when the server has the least amount of usage.

    As to code to make it happen give the first part of what I said a try, post code here after a good attempt and we'll help you out if you need it. Hint, use the limit in your query and keep track of the offset if you're familiar with PHP/MySQL and how to send basic email.

    We don't know your skill level as to PHP, MySQL or cron, let us know.

    -jim
    Last edited by SrWebDeveloper; 02-26-2010 at 01:01 PM. Reason: typos

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,401
    Do a query as you normally would for email address and name, then do a loop on the query result set, prepending the name to the message string and using the email address in the chosen mail function. PHP's native mail() function is rather inefficient for mass mailings, so you might want to consider using the PEAR::Mail_Queue package -- especially if you think there may be a lot of growth to your mail list (or one of the several 3rd-party mass mailing scripts out there on the 'net).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Jul 2009
    Posts
    77

    Question think i got it working--question about what you said

    Why do I need to do this in batches?

    I don't want to do anything as BCC as each message will require a personal Dear XXXX.

    Can I do batches of 50 at a time all using just the to: field?

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,401
    For the custom greeting each would have to be sent separately to one address.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    Quote Originally Posted by realmuffin View Post
    Why do I need to do this in batches?

    I don't want to do anything as BCC as each message will require a personal Dear XXXX.

    Can I do batches of 50 at a time all using just the to: field?
    Ah, I missed the personalization part, my apologies. Yeah, you'll need to send one at a time, if the greeting is crucial to the mailing. Batches are used (when mass mailing the same subject/body to different recipients) to reduce overhead, as sending one email at a time is very resource intensive, i.e. memory, bandwidth, the MTA i.e. sendmail, mail queue, DNS hostname lookup, log updating and so on. There is typically a delay between batches so the server can recover those resources for the next batch. And if the server is used for other things, i.e. web or apps, scheduling the mail to be sent when things are slow is wise.

    -jim
    Last edited by SrWebDeveloper; 02-26-2010 at 06:13 PM. Reason: oops

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles