How do I use PHP to create a mailing list?

I have a database populated with email addresses and its all configured with php files and such on a webserver. What PHP commands do I use to read the emails from the database table and send an email to all of them at once?

One Response to “How do I use PHP to create a mailing list?”

  • Matt:

    The most simple and barebones method of doing what you need done is through a couple php functions. You’ll want to run a MySQL query to create an object from your database that will hold all of the email addresses.

    So you’ll want to connect and select the db first;
    http://ca.php.net/manual/en/function.mysql-connect.php
    http://ca.php.net/manual/en/function.mysql-select-db.php

    And then run the query;
    http://ca.php.net/mysql_query

    Iterate through your query results and mail each of them;
    http://ca.php.net/manual/en/function.mail.php

    It will look like this (Code starting):

    // Connecting to the database
    $db = mysql_connect(‘localhost’, ‘yourdbuser’, ‘yourdbpassword’);
    if (!$db) die(mysql_error());

    // Selecting the database table
    $selectdb = mysql_select_db(‘yourdbname’, $db);
    if (!$selectdb) die(mysql_error());

    // Running the MySQL query
    $results = mysql_query("SELECT * FROM yourtable);

    // Pulling up the results and iterating through them
    while ($row = mysql_fetch_object($results)) {
    // Emailing each member
    $to = $row->email;
    $subject = ‘This is a test’;
    $message = ‘This is a test’;
    $headers = ‘From: youremail@example.com’ . "\r\n" .
    ‘X-Mailer: PHP/’ . phpversion();

    mail($to, $subject, $message, $headers);
    }

Leave a Reply