Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2003
    Montreal, Canada

    [PHP] Send email to every address in a table?

    I'd like to send an email (static) to every email stored in a table.
    I've already managed to delete all duplicate content in there, but now im stuck.

    table = emails
    field = id, field = AuthorEmail
    id is unique but it's missing numbers, from where there was duplicate entries

    How would I select an email one by one and mail() them the email?

    php snippet would be greatly appreciated
    Thank You

  2. #2
    Join Date
    Mar 2006
    Perhaps with phpmailer, it is possible to send messages to multiple email addresses. Althought, if it doesn't work, you should do this from a class function.

  3. #3
    Join Date
    Feb 2006
    Lancashire, UK
    PHP Code:

    "SELECT * FROM email ORDER BY id";
    $result mysql_query($sql);
    $row=mysql_fetch_array($result) {

    $email $myrow["email"];

    $message "Hello whatever your name is. your message here";


    "mail sent to $email<br>";

    i dont think i wrote the mail() part right.....i couldnt remember it all...

    but there is the sql query

    the $email variable is selected one by it will fire out emails to people in your table

    hope that helps


  4. #4
    Quote Originally Posted by horizon
    Perhaps with phpmailer, it is possible to send messages to multiple email addresses. Althought, if it doesn't work, you should do this from a class function.
    It is indeed possible to send multiple emails using phpmailer. What "class function" are you talking about? Please stop making disinformative posts.

    An addon to mikey1090's post and a change of the method. To avoid multiple mail() function calls, you can specify a list of mails which should recieve the mail.

    Downside: everybody that recieve email can see the list of emails in the list.
    Fix: do a research of email headers ( is good for a start).

    Sample code, might contain errors:

    PHP Code:

    = @mysql_query('SELECT AuthorEmail FROM emails');

    $email_to = @implode(','mysql_fetch_array($query)); // untested, beware before copypasting :)

    $subject 'Subject';
    $message 'Email message';

    'An error occured: 'mysql_error());

  5. #5
    Join Date
    Nov 2001
    Why not use Bcc: header?

    Test first in your environment, but the following in a Postfix environment will result in mail without BCC being advertised to recipients.

    From: Foo Bar <[email protected]>
    To: Info <[email protected]>
    Bcc: [email protected], [email protected],
           [email protected], 
    Subject: test
    Hey, you didn't see the bcc list.
    Populate the above with valid email addresses and senders, save it in a text file, and try this:

    sendmail -t < testemail.txt

    (Postfix takes over "sendmail" command). Once you've ascertained that your smtpd behaves properly, then you can code a solution in PHP.

    edit: be sure to include addresses inside and outside of your smtp server's span of control.
    “Even those who arrange and design shrubberies are under
    considerable economic stress at this period in history.”

  6. #6
    The reason you may not want to use BCC is that with multiple mail calls you can personalize the email. This is a snippet from my personal site I use to spam my friends about events and updates.

    PHP Code:

    /// basic mysql query function - returns an array of results or false on failure
    function query($q) {

    $link mysql_connect($settings['dbhost'], $settings['dbuser'], $settings['dbpass']);
        if (!
    $link) {
    log("Could not connect to database.");
        if (!
    is_resource($result)) return $result;
        else { 
    // only select should return a resource
    $return = array();
            while (
    $row mysql_fetch_assoc($result)) array_push($return,$row);

    /// sends a personalized email to everyone in the peeps table
    function mass_mail() {

    $subject trim($_POST['subject']);
    $content stripslashes($_POST['content']);
    $from $settings['mailbox'];
    $mailheaders "From: $from\nReply-To: $from\n";

    $list query("SELECT * FROM peeps");
        if (!
    $listlog("Something screwy with the list?");
        foreach (
    $list as $l) {
    'Dear '.$l['name'].',


    .    "If you wish to remove yourself from the NeoHz mailing list follow this link:\n".
    mail($l['email'], $subject$content$mailheaders);
    log("Spammed ".$l['name']." on ".date("r"));

    This is a fairly small list though (roughly 100 and change). A larger list would require more error checking and or splitting the job into sections. missing is the log function (which simply writes to an array which gets dumped it into a file) and the settings array where I keep all the db info, addresses etc... There may be other stuff missing aswell seeing as it is a cut and paste but gives you an idea.

Posting Permissions

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