Jump to content

Sending multiple emails


timmah1

Recommended Posts

I'm trying to set up a newsletter for a person.

Everything is working good, the insertion of the newsletter and the sending.

Right now there 139 people in the database, running a test with just 2 email address, I'm receiving 139 emails, when in reality, I should only be receiving 2.

 

Can anybody tell me why this is?

 

require("config.php");	
$q = mysql_query("SELECT * FROM users WHERE active = 'yes' ORDER BY user_id"); 
$i = 1;
while ($b = mysql_fetch_array($q)) { 
	$email = $b['email'];
	$to1 = "[email protected],[email protected]";

	$headers  = 'MIME-Version: 1.0' . "\r\n";
	$headers = "From: Solon Sportsmen Club <[email protected]>\n";
	$headers .= "Reply-to: [email protected]\n";
	$headers .= "Bcc: $to1" . ', ';
	$headers .= "Bcc: [email protected]\n";
	$headers .= "Bcc: [email protected]\n";
	$headers .= "Bcc: [email protected]\n";
	$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

	mail($to, $subject, $message, $headers);
		echo "Newsletter has been sent to:<br />$i - $email<br />";
	$i++;
}

 

It sends everything right, it's just sending to each person, the number of registered, that number of emails.

 

Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/149099-sending-multiple-emails/
Share on other sites

Well, you're defining which emails you want to send to inside your loop which runs for every record in the table. In order to only send to those emails, you should specify that in your query and use the email address in the record. Something like:

 

require("config.php");   
   $q = mysql_query("SELECT * FROM users WHERE active = 'yes' AND email IN ('[email protected]','[email protected]') ORDER BY user_id");
   $i = 1;
   while ($b = mysql_fetch_array($q)) {
      $email = $b['email'];
      $to1 = $email;
   
      $headers  = 'MIME-Version: 1.0' . "\r\n";
      $headers = "From: Solon Sportsmen Club <[email protected]>\n";
      $headers .= "Reply-to: [email protected]\n";
      $headers .= "Bcc: $to1" . ', ';
      $headers .= "Bcc: [email protected]\n";
      $headers .= "Bcc: [email protected]\n";
      $headers .= "Bcc: [email protected]\n";
      $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
      
      mail($to, $subject, $message, $headers);
         echo "Newsletter has been sent to:<br />$i - $email<br />";
      $i++;
   }

 

That said, you should check the notes from the php manual about the mail() function:

 

Note: It is worth noting that the mail() function is not suitable for larger volumes of email in a loop. This function opens and closes an SMTP socket for each email, which is not very efficient.

ok, so the emails to send to are in the query, that's great and all, but that was for testing only, to make sure that those 2 emails received the newsletter.

The actual number of people that are going to get this, is 139.

 

So with this code, will all 139 people receive 139 emails or will each one just get one?

<?php
require("config.php"); 
   $to = "Member";  
   $q = mysql_query("SELECT * FROM users WHERE active = 'yes' ORDER BY user_id");
   $i = 1;
   while ($b = mysql_fetch_array($q)) {
      $email = $b['email'];
   
      $headers  = 'MIME-Version: 1.0' . "\r\n";
      $headers = "From: Solon Sportsmen Club <[email protected]>\n";
      $headers .= "Reply-to: [email protected]\n";
      $headers .= "Bcc: $email" . ', ';
      $headers .= "Bcc: [email protected]\n";
      $headers .= "Bcc: [email protected]\n";
      $headers .= "Bcc: [email protected]\n";
      $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
      
      mail($to, $subject, $message, $headers);
         echo "Newsletter has been sent to:<br />$i - $email<br />";
      $i++;
   }
?>

for testing purposes, duplicate the structure of the users table and call it "users_test". then, populate it with 2 or 3 records of fake data with your email addresses. then, change your select statement to pull from that table. run it, tweak it, get it to exactly what you want. then change the table name back to users. run it ONCE to send the emails. make sure you use set_time_limit(0); so the script doesn't time out:

http://us3.php.net/function.set-time-limit

 

after it's done, change it back to users_test to make sure it doesn't send them again (in case you accidentally run the script again)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.