简体   繁体   中英

How to send e-mail to multiple recipients from database query (PHP)

I'm trying to send an e-mail to multiple e-mail address in my database. Here is my current code. It is only working when I specify a single e-mail address, however, I need to have them query my database and send the e-mail to each e-mail address. Where am I going wrong here?

function sendmail($cat, $user) {
    require_once "Mail.php";
    $elist = mysql_query("SELECT cEmail FROM tblUsers WHERE cAlerts = 'All' AND cEAlerts = 'Yes' AND cPreferences LIKE '%$cat%';");
    $elist = mysql_fetch_array($elist);

    $from = "EMAIL ADDRESS";
    $to = $elist;
    $subject = "SUBJECT";
    $body = "BODY";

    $host = "smtp.domain.com";
    $username = "USERNAME";
    $password = "PASSWORD";

    $headers = array ('From' => $from,
    'To' => $to,
    'Subject' => $subject);
    $smtp = Mail::factory('smtp',
    array ('host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password));

    $mail = $smtp->send($to, $headers, $body);
 }

Try something like this but one point to note is that you should send emails individually ratehr than group all your email addresses in one "to" field. Other users might not like others seeing that. Maybe your smtp function breaks down the array, not sure :-|

function sendmail($cat, $user)
{ 
    require_once "Mail.php"; 
    $elist = mysql_query("SELECT cEmail FROM tblUsers WHERE cAlerts = 'All' AND cEAlerts = 'Yes' AND cPreferences LIKE '%$cat%';"); 

    $from = "EMAIL ADDRESS"; 
    $subject = "SUBJECT"; 
    $body = "BODY"; 

    $host = "smtp.domain.com"; 
    $username = "USERNAME"; 
    $password = "PASSWORD"; 

        if(mysql_num_rows($elist) > 0)
        {
            while($elist_result = mysql_fetch_array($elist))
            {
            $headers = array ('From' => $from, 
            'To' => $elist_result['cEmail'], 
            'Subject' => $subject); 
            $smtp = Mail::factory('smtp', 
            array ('host' => $host, 
            'auth' => true, 
            'username' => $username, 
            'password' => $password)); 

            $mail = $smtp->send($to, $headers, $body); 
            }
        }
 } 

I had a similar issue while trying to loop though my recipients' database table with the aim of sending personalized PEAR emails to each recipient.

The problem I encountered was that the loop would cease after one iteration until I amended $recipients to just send the first item in the array, as illustrated in this code snippet. ( $recipients is an array of IDs for recipients):

/*PEAR mail set-up code included here*/

for ($i = 0; $i < count($recipients); $i++) {

$sql_recipients = "SELECT * FROM $data_table WHERE id = $recipients[$i] ORDER BY id  ASC ";
$res_recipients = mysqli_query($mysqli,$sql_recipients)
or die (mysqli_error($mysqli));

$recipients_info = mysqli_fetch_array($res_recipients);
$recip_id = $recipients_info['id'];
$recip_organisation = $recipients_info['organisation'];
$recip_fname = $recipients_info['fname'];
$recip_lname = $recipients_info['lname'];
$recip_email = $recipients_info['email'];

/*PEAR mail code here */

$recipients[0] = $recip_email;

$mail->send($recipients[0], $hdrs, $body);

}

mysql_fetch_array fetches an array with entries corresponding to each of the columns of a single row of your table. In other words, here it's an array containing one user's cEmail column.

You need to fetch all the values into a single array before calling the mail functions. You could do it like this:

$dest = array();
while ($arr = mysql_fetch_array($elist)) {
   $dest[] = $arr['cEmail'];
}

I learned to this from one of the examples in the book I am learning PHP from, Head First PHP & MySQL .

What you can do is send each e-mail individually, but simultaneously, through a while loop. With a mysqli_query selecting the e-mails, make a while loop that goes through my

while ($row_data = mysqli_fetch_array($your_query_in_a_variable)) {

    // Then you will set your variables for the e-mail using the data 
    // from the array.

    $from = 'you@yoursite.com';
    $to = $row_data['email']; // The column where your e-mail was stored.
    $subject = 'Open Me!';
    $msg = 'Hello world!';
    mail($to, $msg, $from);
}

I hope that is clear. You basically just call the rows you want in your query, then use mysqli_fetch_array , which goes through each row every time it is called (someone correct me, if wrong) and will exit when there are no more rows left that were called.

My suggestion would be use the php mail() function and set the smtp server parameters in your php.ini file.

Then all you have to do is query your db, pull the first record out as your to address and then loop through the result set and put each email address into an array. Then just use the join function to join the array with commas and use it as the BCC for the header string.

Here is what I would use if possible. I've included a couple notes as //comments

function sendmail($cat, $user) {

    $result = mysql_query("SELECT cEmail FROM tblUsers WHERE cAlerts = 'All' AND cEAlerts = 'Yes' AND cPreferences LIKE '%$cat%';");

    //pull out first email as the to address
    $to = mysql_fetch_array($result);

    //create array to store all the emails
    $elist = array();

    //loop through and put each email into an array
    // you might be able to skip this altogether if you can just join $result
    // but I'm to lazy to investigate what all is being returned 
    while($row = mysql_fetch_assoc($result)){
        $elist[] = $row['cEmail'];
    }

    //join the emails into a comma separated string
    $bcc = join($elist, ",");

    $from = "EMAIL ADDRESS";
    $subject = "SUBJECT";
    $body = "BODY";

    $headers  = "From: ". $from ."\r\n";
    $headers  .="BCC: ". $bcc ."\r\n";

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

Hopefully this is useful information.. Feel free to pick it apart and use whatever you may need, like if you can use part to implement your Mail class.

You schould not use the php mail() function. It is dead slow because it does a full connect to the smtp server every single mail you send.
A good Mail library such as Zend_Mail connects only once and delivers the mails in one batch to the smtp server.
You should also not send all your mails via a long BCC-Line. At least if you want your email not marked as spam (See this Question and this Blog-Entry by Jeff).

<?php
function sendmail($cat, $user) {
    // Setup mailer
    require_once "Mail.php";

    $smtp = Mail::factory('smtp', array(
        'host'     => "smtp.domain.com";
        'auth'     => true,
        'username' => "USERNAME";
        'password' => "PASSWORD";
    ));

    // ALWAYS escape your variables!
    $query = sprintf("SELECT cEmail FROM tblUsers WHERE cAlerts = 'All' AND cEAlerts = 'Yes' AND cPreferences LIKE '%s'",
            mysql_real_escape_string($cat),

    // submit the query
    $result = mysql_query($query);

    // iterate over each row
    while ($row = mysql_fetch_assoc($result)) {
        $from    = "EMAIL ADDRESS";
        $to      = $row['cEmail'];
        $subject = "SUBJECT";
        $body    = "BODY";

        $headers = array(
            'From'    => $from,
            'To'      => $to,
            'Subject' => $subject
        );

        // Send mail with individual to-address via smtp 
        $mail = $smtp->send($to, $headers, $body);
    }
}

Use this code to resolve your problem.

  //Connect to database
  mysql_connect("localhost","user","password") or die(mysql_error());
  mysql_select_db("db") or die(mysql_error()); 
  $sql = "SELECT email FROM members";  
  $res = mysql_query($sql) or die(mysql_error());
  while($row = mysql_fetch_assoc($res) )   
  {  
      $area .= $row['email']. ", ";
  }

  // read the list of emails from the file.
  $email_list = explode(',', $area);

  // count how many emails there are.
  $total_emails = count($email_list);

  // go through the list and trim off the newline character.
  for ($counter=0; $counter<$total_emails; $counter++)
  {
      $email_list[$counter] = trim($email_list[$counter]);
  }
  $to = $email_list;
  echo $to;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM