Matt
Matt

Reputation: 103

Sending emails to multiple recipients (PHP, My SQL)

Hi I am new to this and only have a basic understanding on PHP so any help would be most welcome. Basically I am trying to send an email via PHP that can have multiple recipients. The content also needs to display a list of users following certain criteria. I have got the script to look at the database and send an email however it only sends it to one person and only lists one person in the content. Please could someone advise on how to change that to email / display to multiple recipients. I know that there should be at least three people it is sent to.

Any help / pointers would be most welcome. Thank you.

<?php

session_start();

require_once('../config.php');

$errmsg_arr = array();

$errflag = false;

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}

$db = mysql_select_db(database_name);
if(!$db) {
die("Unable to select database");
}

$result = mysql_query("SELECT username FROM users WHERE user_type='admin'");

while($result_email = mysql_fetch_array($result))

$to = $result_email['username'];

$result2 = mysql_query("SELECT * FROM users
WHERE ticked='1' AND NOT user_type='super'");

while($result2 = mysql_fetch_array($result2))

$body = $result2['fname'] . " " . $result2['lname'] . " , " . $result2['username'];

mail ($to, 'Subject', $body);

?>

Upvotes: 0

Views: 3244

Answers (4)

nathangiesbrecht
nathangiesbrecht

Reputation: 950

Your call to mail() is not in a loop, so it will only send one e-mail.

And your while loop where you're setting $to is simply overwriting the previous value each time the loop iterates.

So in the end $to will be set to the last username the first query returns. And your $body will be the body you want for the last row your second query returns.

Upvotes: 0

Erdem Ece
Erdem Ece

Reputation: 1775

your code is wrong. you would have error if you used it. you need to use implode to add comma between the emails. check your php. i corrected but you have to use while statement right. you have multiple mysql query for one job. also you should not use mysql_query. use PDO instead...

you should use it like this

<?php
    while($row = mysql_fetch_array($result2))
    {
       $addresses[] = $row['username'];
    }

    $to = implode(", ", $addresses);
?>

with your code

            <?php

                session_start();

                require_once('../config.php');

                $errmsg_arr = array();

                $errflag = false;

                $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
                if(!$link) {
                die('Failed to connect to server: ' . mysql_error());
                }

                $db = mysql_select_db(database_name);
                if(!$db) {
                die("Unable to select database");
                }

                $result = mysql_query("SELECT username FROM users WHERE user_type='admin'");

                while($row = mysql_fetch_array($result2))
                {
                    $addresses[] = $row['username'];
                }

                $to = implode(", ", $addresses);

                $result2 = mysql_query("SELECT * FROM users
                WHERE ticked='1' AND NOT user_type='super'");

                $body = mysql_fetch_array($result2);
                $body = $result2['fname'] . " " . $result2['lname'] . " , " . $result2['username'];

                mail ($to, 'Subject', $body);

            ?>

Upvotes: 1

You are overwriting the value of $to repeatedly with the statement $to = $result_email['username'];. You should make a comma-separated list instead. But for goodness' sake,

  1. Consider setting the addresses as BCC, and set TO to something that does not matter, e.g. noreply@[yourdomain] to hide the recipients' addresses from each other.
  2. Use DB-access that is not deprecated: PDO

Upvotes: 0

doitlikejustin
doitlikejustin

Reputation: 6363

You can do that. They just need to be comma separated.

$to = "email@email.com, email2@email2.com";

Reference: http://php.net/manual/en/function.mail.php

Also, your code is a little confusing the way you have pasted it. But if you are loading all the emails in a while loop. Then you want to send it after the while. For example:

You want it to be:

//create an empty $to string
$to = '';

//add all the emails
while($emails as $email)
{
    $to .= $email . ',';
}

//remove the last comma
rtrim($to, ","); 

//send away
mail($to, $subject, $message);

Upvotes: 1

Related Questions