SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mail multiple from query

    How do I work this out so I can mail to all the matches I get from my database?
    PHP Code:
    $email_list mysql_query("SELECT mail FROM Users WHERE updateme='Yes'");
    $mail_body "New Mail";
    $subject "Hello";

    mail($email_list$subject$mail_body); 

  2. #2
    SitePoint Guru
    Join Date
    Feb 2008
    Posts
    655
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just using mysql_query like that won't yield the expected results.

    You need something like this:

    Code PHP:
    $mail_body = "New Mail";
    $subject = "Hello";
     
    $result = mysql_query("SELECT mail FROM Users WHERE updateme='Yes'");
     
    while($row = mysql_fetch_assoc($result)
    {
         mail($row['mail'], $subject, $mail_body); 
    }

    This also assumes you have set up the MySQL connection correctly.

  3. #3
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    set_time_limit
    (0);
    ini_set("display_errors"1);
    error_reporting(E_ALL);

    mysql_connect("localhost""users""pass") or die(mysql_error());
    mysql_select_db("db") or die(mysql_error());

    $email_list mysql_query("SELECT mail FROM Users WHERE updateme='Yes'") or die(mysql_error());

    function 
    fetch_all_array($query_string)
    {
        
    $out = array();
        while (
    $row mysql_fetch_array($query_string))
        {
            
    $out[] = $row;
        }
        return 
    $out;
    }

    $all_rows fetch_all_array($email_list);

    foreach (
    $all_rows as $key)
    {
        
    $text[] = $key['mail'];
    }

    // send a copy to you
    $to "you@host.com";
    $list implode(","$text);
    $subject "reminder";
    $message "hello!";
    $headers "From: webmaster@host.com \r\n" 
               
    "Reply-To: webmaster@host.com \r\n" 
               
    "Bcc: $list\r\n";
    $headers .= "X-Mailer: PHP/" phpversion();

    mail($to$subject$message$headers);
    my mobile portal
    ghiris.ro


Bookmarks

Posting Permissions

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