Mass mailing method?

I have a database with the following columns:
email
location

and there are 3 different locations and I would like to be able to mail everyone in each location as well as being able to mail every person in the DB how can i do this?

How many records you have in your database? If it’s huge, it might cause your php execution to time out. To go around this, you can call send mail php script for each (or few hundreds) email. You can use ajax to help with this.


<?php
function get_user_emails($location = null){
  
  $sql = 'SELECT email FROM table';
  if(null !== $location){
    $sql = sprintf(
      " WHERE location = '&#37;s'",
      mysql_real_escape_string($location)
    );
  }
  
  $result = mysql_query($sql);
  
  $records = array();
  
  if(true === is_resource($result)){
    while($record = mysql_fetch_assoc($result)){
      array_push($records, $record);
    }
  }
  
  return $records;
}

$emails = get_user_emails(); #returns ALL users
$emails = get_user_emails('denver'); #returns emails where location is denver
?>

What you do not want to do is loop over the list and send an individual message to each recipient. The mail() function opens a new socket for each message sent.

Select the e-mail address of all of the recipients you want to send a message to, join (or “implode”) them into a comma-separated list, and pass that list as the “To:” parameter to mail(). The following example assumes Zend_Db is being used.


$sql = "SELECT `email` FROM `table_name` WHERE `location` = 'whatever'";
$emails = $db-&gt;fetchCol($sql);
$to = implode(",", $emails);
@mail($to, 'Test message', 'This is a test message...');

EDIT: Actually, you wouldn’t want to put all those names into the “To:” field. You should send the message to yourself with the other recipients’ e-mail address in the “BCC:” field so that they can’t see everyone else’s address that you’re sending it to.

bump, i need this asap please :slight_smile:

i just solved it thanks for all the responses.

Please.

“how can i integrate that into the sending form here, please:rolleyes:

of course!!! the please was implied sorry. I am extremely thankful for both of your responses :slight_smile:

how can i integrate that into the sending form here

			             <form name="massmail" method="post" action="./massmail-exe.php">
<table width="450px">
<tr>
 <td valign="top">
  <label for="first_name">To Whom:</label>
 </td>
 <td valign="top">
  <input  type="text" name="first_name" maxlength="50" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="subject">Subject</label>
 </td>
 <td valign="top">
  <input  type="text" name="first_name" maxlength="50" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="content">Email Content</label>
 </td>
 <td valign="top">
  <textarea  name="comments" maxlength="9001" cols="100" rows="18"></textarea>
 </td>

</tr>
<tr>
 <td colspan="2" style="text-align:center">
  <input type="submit" value="Submit">
 </td>
</tr>
</table>
</form>