Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8476023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:03:50+00:00 2026-06-10T18:03:50+00:00

Possible Duplicate: Sending mass email using PHP I have a PHP script that sends

  • 0

Possible Duplicate:
Sending mass email using PHP

I have a PHP script that sends an individual email to all users in my DB, such as a monthly / weekly newsletter.

The code I am using goes as follows:

$subject = $_POST['subject'];
$message = $_POST['message'];

// Get all the mailing list subscribers.
$query = $db->prepare("SELECT * FROM maildb");
$query->execute();

// Loop through all susbcribers, and send and individual email.
foreach ($query as $row) {

    // Setting maximum time limit to infinite.
    set_time_limit(0);

    $newMessage = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        </head>
        <body>';

    // Search for the [unsubscribe] tag and replace it with a URL for the user to unsubscribe
    $newMessage .= str_replace("[unsubscribe]", "<a href='".BASE_URL."unsubscribe/".$row['hash']."/".$row['email']."'>unsubscribe</a>", $message);

    $newMessage .= '</body></html>';

    $to = $row['email'];

    // Establish content headers
    $headers = "From: info@domain.com"."\n";
    $headers .= "Reply-To: bounce@domain.com"."\n";
    $headers .= "X-Mailer: PHP v.". phpversion()."\n";
    $headers .= "MIME-Version: 1.0"."\n";  
    $headers .= "Content-Type: text/html; charset=iso-8859-1"."\n";
    $headers .= "Content-Transfer-Encoding: 8bit;";

    mail($to, $subject, $newMessage, $headers); // Send email to each individual user

}

This code works perfectly with a REALLY small database… I recently populated my test db with 200k+ users, and obviously this script fails, gets out of memory, and dies…

I know this is a bad way to send so many emails, thats why I’d like to ask you for much more efficient ways to do this!

Thank you very much!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T18:03:52+00:00Added an answer on June 10, 2026 at 6:03 pm

    See this Url:–

    Sending mass email using PHP

    First off, using the mail() function that comes with PHP is not an optimal solution. It is easily marked as spammed, and you need to set up header to ensure that you are sending HTML emails correctly. As for whether the code snippet will work, it would, but I doubt you will get HTML code inside it correctly without specifying extra headers

    I’ll suggest you take a look at SwiftMailer, which has HTML support, support for different mime types and SMTP authentication (which is less likely to mark your mail as spam).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Sending HTML email from PHP Using PHP to send an email. I
Possible Duplicate: Show progress for PHP long script I have a PHP script that
Possible Duplicate: Sending HTML email from PHP I need to send an html mail
Possible Duplicate: Sending Email in Android using JavaMail API without using the default android
Possible Duplicate: sending mail with php & escaping hotmails junk folder I have been
Possible Duplicate: Sending Email in .NET Through Gmail I have so many problems with
Possible Duplicate: Sending Email in Android using JavaMail API without using the default/built-in app
Possible Duplicate: Sending Email in Android using JavaMail API without using the default Android
Possible Duplicate: Sending large files reliably in PHP I will be distributing a PHP
Possible Duplicate: Sending email in .NET through Gmail I am trying to send a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.