Help - Send automails containing info from Database

Discussion in 'PHP' started by rajivv, Mar 2, 2010.

  1. #1
    HI,

    I had run into problems with my site and site was down for 4 months , now have added new features etc.
    Have to send a mail to all users about new features, have around 7500 user
    How do I send a mail which automatically picks up the User Display name from Db
    So the mail reads 'Dear ( User display name) '
    Also in the same mail want to remind the user of his username and password.
    ' You username is (username) and password is (password)

    How to do this automatically for the 7500 users ????
    coded in php and mysql

    Please help

    Regards
    Rajiv
     
    rajivv, Mar 2, 2010 IP
  2. Uninvited

    Uninvited Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi,
    You should connect to the database, select all the users,
    and use a for loop where you add the "Dear {name}" and the reminder to the variable containing the body of the email, and when you have all the variables ready for the mail function, you use it.

    It should be something like this:

    <?php

    $message_body = "I'm glad to inform you ...";
    $subject = "Some news";

    $conn = new mysqli(server_name, db_username, db_password, database_name);

    $result = $conn->query("select * from users_table");

    $num_results = $result->num_rows;

    for ($i=0; $i < $num_results; $i++)
    {
    $row = $result->fetch_assoc();

    $to = $row['email'];

    $message_body = "Dear $row['name'], \n" . $message_body;
    $message_body .= "\n Your username is $row['username'] and password is $row['password']";
    mail($to, $subject, $message_body);
    }
    ?>
     
    Uninvited, Mar 3, 2010 IP