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 = "tpatterson@cheezyfries.net,traey@hxcteam.com";

	$headers  = 'MIME-Version: 1.0' . "\r\n";
	$headers = "From: Solon Sportsmen Club <noreply@solonsportsmen.org>\n";
	$headers .= "Reply-to: noreply@solonsportsmen.org\n";
	$headers .= "Bcc: $to1" . ', ';
	$headers .= "Bcc: noreply@solonsportsmen.org\n";
	$headers .= "Bcc: noreply@solonsportsmen.org\n";
	$headers .= "Bcc: noreply@solonsportsmen.org\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
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 ('tpatterson@cheezyfries.net','traey@hxcteam.com') 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 <noreply@solonsportsmen.org>\n";
      $headers .= "Reply-to: noreply@solonsportsmen.org\n";
      $headers .= "Bcc: $to1" . ', ';
      $headers .= "Bcc: noreply@solonsportsmen.org\n";
      $headers .= "Bcc: noreply@solonsportsmen.org\n";
      $headers .= "Bcc: noreply@solonsportsmen.org\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.

Link to comment
Share on other sites

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 <noreply@solonsportsmen.org>\n";
      $headers .= "Reply-to: noreply@solonsportsmen.org\n";
      $headers .= "Bcc: $email" . ', ';
      $headers .= "Bcc: noreply@solonsportsmen.org\n";
      $headers .= "Bcc: noreply@solonsportsmen.org\n";
      $headers .= "Bcc: noreply@solonsportsmen.org\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++;
   }
?>

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.